-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapitest_test.go
204 lines (168 loc) · 6.04 KB
/
apitest_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package apitest
import (
"io/ioutil"
"net/http"
"testing"
"gopkg.in/yaml.v2"
"github.com/go-openapi/loads"
"github.com/go-openapi/loads/fmts"
"github.com/go-openapi/spec"
"github.com/go-openapi/strfmt"
"github.com/go-openapi/validate"
"github.com/jarcoal/httpmock"
"github.com/seesawlabs/raml"
"github.com/stretchr/testify/assert"
)
func TestRunApi(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
setupMock()
tests := getTests()
runner := NewRunner("http://testapi.my", RunnerConfig{})
runner.Run(t, tests...)
}
func TestGenerateSwaggerYAML(t *testing.T) {
seed := spec.Swagger{}
seed.Host = "testapi.my"
seed.Produces = []string{"application/json"}
seed.Consumes = []string{"application/json"}
seed.Schemes = []string{"http"}
seed.Info = &spec.Info{}
seed.Info.Description = "Our very little example API with 2 endpoints"
seed.Info.Title = "Example API"
seed.Info.Version = "0.1"
seed.BasePath = "/"
generator := NewSwaggerGeneratorYAML(seed)
tests := getTests()
doc, err := generator.Generate(tests)
assert.NoError(t, err, "could not generate docs")
// checking validity of generated swagger doc
yamlMap := map[interface{}]interface{}{}
err = yaml.Unmarshal(doc, &yamlMap)
assert.NoError(t, err, "could not unmarshal generated doc into map")
rawJSON, err := fmts.YAMLToJSON(yamlMap)
assert.NoError(t, err)
swaggerDoc, err := loads.Analyzed(rawJSON, "")
assert.NoError(t, err)
err = validate.Spec(swaggerDoc, strfmt.Default)
assert.NoError(t, err)
// checking equality of generated and expected doc
actual := map[interface{}]interface{}{}
err = yaml.Unmarshal(doc, &actual)
assert.NoError(t, err, "could not unmarshal generated doc into map")
fixture, err := ioutil.ReadFile("fixtures/swagger/swagger.yml")
assert.NoError(t, err, "could not read fixture file")
expected := map[interface{}]interface{}{}
err = yaml.Unmarshal(fixture, &expected)
assert.NoError(t, err, "could not unmarshal fixture into map")
assert.Equal(t, expected, actual)
}
func TestGenerateRaml(t *testing.T) {
seed := raml.APIDefinition{}
seed.Version = "0.1"
seed.Title = "Example API"
seed.BaseUri = "http://testapi.my/"
seed.Protocols = []string{"HTTP", "HTTPS"}
seed.MediaType = "application/json"
seed.Title = "Example API"
generator := NewRamlGenerator(seed)
tests := getTests()
doc, err := generator.Generate(tests)
assert.NoError(t, err, "could not generate docs")
assert.Equal(t, "#%RAML 0.8", string(doc[0:10]), "Specific RAML header is expected")
// checking equality of generated and expected doc
actual := map[interface{}]interface{}{}
err = yaml.Unmarshal(doc, &actual)
assert.NoError(t, err, "could not unmarshal generated doc into map")
fixture, err := ioutil.ReadFile("fixtures/raml/raml.yml")
assert.NoError(t, err, "could not read fixture file")
expected := map[interface{}]interface{}{}
err = yaml.Unmarshal(fixture, &expected)
assert.NoError(t, err, "could not unmarshal fixture into map")
assert.Equal(t, expected, actual)
}
func getTests() []IApiTest {
return []IApiTest{
&HelloTest{},
&GetUserTest{},
&CreateUserTest{},
&UpdateUserTest{},
&DeleteUserTest{},
}
}
func setupMock() {
testUser := User{
EventsURL: "https://api.github.com/users/octocat/events{/privacy}",
Followers: 20,
FollowersURL: "https://api.github.com/users/octocat/followers",
Following: 0,
FollowingURL: "https://api.github.com/users/octocat/following{/other_user}",
GistsURL: "https://api.github.com/users/octocat/gists{/gist_id}",
Hireable: false,
HTMLURL: "https://github.com/octocat",
Location: "San Francisco",
Login: "octocat",
Name: "monalisa octocat",
OrganizationsURL: "https://api.github.com/users/octocat/orgs",
PublicRepos: 2,
ReceivedEventsURL: "https://api.github.com/users/octocat/received_events",
ReposURL: "https://api.github.com/users/octocat/repos",
StarredURL: "https://api.github.com/users/octocat/starred{/owner}{/repo}",
SubscriptionsURL: "https://api.github.com/users/octocat/subscriptions",
Type: "User",
URL: "https://api.github.com/users/octocat",
}
httpmock.RegisterResponder("GET", "http://testapi.my/hello",
func(req *http.Request) (*http.Response, error) {
resp := httpmock.NewStringResponse(200, "Hello World!")
return resp, nil
},
)
httpmock.RegisterResponder("GET", "http://testapi.my/user/octocat",
func(req *http.Request) (*http.Response, error) {
return httpmock.NewJsonResponse(200, testUser)
},
)
httpmock.RegisterResponder("GET", "http://testapi.my/user/someveryunknown",
func(req *http.Request) (*http.Response, error) {
resp := httpmock.NewStringResponse(404, "user someveryunknown not found")
return resp, nil
},
)
httpmock.RegisterResponder("GET", "http://testapi.my/user/BadGuy",
func(req *http.Request) (*http.Response, error) {
resp := httpmock.NewStringResponse(500, "BadGuy failed me :(")
return resp, nil
},
)
httpmock.RegisterResponder("POST", "http://testapi.my/user",
func(req *http.Request) (*http.Response, error) {
return httpmock.NewJsonResponse(201, testUser)
},
)
httpmock.RegisterResponder("PATCH", "http://testapi.my/user/octocat",
func(req *http.Request) (*http.Response, error) {
patchedUser := testUser
patchedUser.Name = "I Am Updated!"
return httpmock.NewJsonResponse(200, patchedUser)
},
)
httpmock.RegisterResponder("DELETE", "http://testapi.my/user/octocat",
func(req *http.Request) (*http.Response, error) {
resp := httpmock.NewBytesResponse(204, nil)
return resp, nil
},
)
httpmock.RegisterResponder("DELETE", "http://testapi.my/user/someveryunknown",
func(req *http.Request) (*http.Response, error) {
resp := httpmock.NewStringResponse(404, "user someveryunknown not found")
return resp, nil
},
)
httpmock.RegisterResponder("DELETE", "http://testapi.my/user/BadGuy",
func(req *http.Request) (*http.Response, error) {
resp := httpmock.NewStringResponse(500, "BadGuy failed me :(")
return resp, nil
},
)
}