-
Notifications
You must be signed in to change notification settings - Fork 1
/
server_test.go
245 lines (180 loc) · 7.18 KB
/
server_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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package main
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/go-pg/pg/v9"
)
func BuildTestServer(t *testing.T) (*Server, func() error, func() error) {
t.Log("init new repository")
repository := NewRepository(&pg.Options{
User: getenv("DB_USER", "postgres"),
Password: getenv("DB_PASS", "postgres"),
Database: getenv("DB_NAME", "vehicles"),
Addr: getenv("DB_ADDR", "localhost:5432"),
})
t.Log("init new service")
service := NewService(repository)
t.Log("init new server and add routes")
server := NewServer()
server.Get("/", service.GetRoot)
server.Get("/manufacturers", service.GetManufacturers)
server.Get("/manufacturers/{hsn}", service.GetManufacturer)
server.Get("/manufacturers/{hsn}/vehicles", service.GetVehicles)
server.Get("/manufacturers/{hsn}/vehicles/{tsn}", service.GetVehicle)
server.Get("/powerSources", service.GetPowerSources)
server.Get("/powerSources/{id}", service.GetPowerSource)
return server, repository.Close, service.Close
}
func TestServerRoot(t *testing.T) {
server, repositoryClose, serviceClose := BuildTestServer(t)
defer repositoryClose()
defer serviceClose()
// Create a request to pass to our handler. We don't have any query parameters for now, so we'll
// pass 'nil' as the third parameter.
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
req.Host = "localhost"
req.Host = "processing.envirocar.org"
req.Header.Add("Host", "processing.envirocar.org")
req.Header.Add("X-Forwarded-Proto", "https")
req.Header.Add("X-Forwarded-Prefix", "/vehicles")
req.Header.Add("X-Forwarded-Port", "443")
req.Header.Add("accept", "application/json")
// We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
rr := httptest.NewRecorder()
t.Log("get root")
server.ServeHTTP(rr, req)
AssertOkStatusCode(t, rr.Code)
want := `{"links":[{"href":"https://processing.envirocar.org/vehicles/manufacturers","type":"application/json","title":"Manufacturers","rel":"manufacturers"},{"href":"https://processing.envirocar.org/vehicles/powerSources","type":"application/json","title":"Power Sources","rel":"powerSources"}]}`
AssertResponseBody(t, rr.Body.String(), want)
t.Logf("response body: %v", rr.Body.String())
}
func TestServerGetManufacturerById(t *testing.T) {
server, repositoryClose, serviceClose := BuildTestServer(t)
defer repositoryClose()
defer serviceClose()
req, err := http.NewRequest("GET", "/manufacturers/0005", nil)
if err != nil {
t.Fatal(err)
}
req.Host = "localhost"
req.Host = "processing.envirocar.org"
req.Header.Add("Host", "processing.envirocar.org")
req.Header.Add("accept", "application/json")
rr := httptest.NewRecorder()
t.Log("get manufacturer by id")
server.ServeHTTP(rr, req)
AssertOkStatusCode(t, rr.Code)
want := `{"links":[{"href":"http://processing.envirocar.org/manufacturers/0005/vehicles","type":"application/json","rel":"vehicles"},{"href":"http://processing.envirocar.org/manufacturers/0005","type":"application/json","title":"BMW","rel":"self"}],"hsn":"0005","name":"BMW"}`
AssertResponseBody(t, rr.Body.String(), want)
t.Logf("response body: %v", rr.Body.String())
}
func TestServerGetPowerSourceById(t *testing.T) {
server, repositoryClose, serviceClose := BuildTestServer(t)
defer repositoryClose()
defer serviceClose()
req, err := http.NewRequest("GET", "/powerSources/14", nil)
if err != nil {
t.Fatal(err)
}
req.Host = "localhost"
req.Host = "processing.envirocar.org"
req.Header.Add("Host", "processing.envirocar.org")
req.Header.Add("accept", "application/json")
rr := httptest.NewRecorder()
t.Log("get power source by id")
server.ServeHTTP(rr, req)
AssertOkStatusCode(t, rr.Code)
want := `{"id":14,"name":"Wasserst./Benzin/E","description":"Bivalenter Betrieb mit Wasserstoff oder Benzin kombiniert mit Elektromotor"}`
AssertResponseBody(t, rr.Body.String(), want)
t.Logf("response body: %v", rr.Body.String())
}
func TestServerGetVehicleByManufacturer(t *testing.T) {
server, repositoryClose, serviceClose := BuildTestServer(t)
defer repositoryClose()
defer serviceClose()
req, err := http.NewRequest("GET", "/manufacturers/0005/vehicles/155", nil)
if err != nil {
t.Fatal(err)
}
req.Host = "localhost"
req.Host = "processing.envirocar.org"
req.Header.Add("Host", "processing.envirocar.org")
req.Header.Add("accept", "application/json")
rr := httptest.NewRecorder()
t.Log("get vehicle by manufacturer and vehicle id")
server.ServeHTTP(rr, req)
AssertOkStatusCode(t, rr.Code)
want := `{"links":[{"href":"http://processing.envirocar.org/manufacturers/0005/vehicles/155","type":"application/json","title":"645CI","rel":"self"},{"href":"http://processing.envirocar.org/powerSources/1","type":"application/json","title":"Benzin","rel":"powerSource"},{"href":"http://processing.envirocar.org/manufacturers/0005","type":"application/json","title":"BMW","rel":"manufacturer"}],"tsn":"155","commercialName":"645CI","allotmentDate":"2003-07-01","category":"01","bodywork":"0200","power":245,"engineCapacity":4398,"axles":2,"poweredAxles":1,"seats":4,"maximumMass":2070}`
AssertResponseBody(t, rr.Body.String(), want)
t.Logf("response body: %v", rr.Body.String())
}
func TestServerGetManufacturers(t *testing.T) {
server, repositoryClose, serviceClose := BuildTestServer(t)
defer repositoryClose()
defer serviceClose()
req, err := http.NewRequest("GET", "/manufacturers", nil)
if err != nil {
t.Fatal(err)
}
req.Host = "localhost"
req.Host = "processing.envirocar.org"
req.Header.Add("Host", "processing.envirocar.org")
req.Header.Add("accept", "application/json")
rr := httptest.NewRecorder()
t.Log("get manufacturers")
server.ServeHTTP(rr, req)
AssertOkStatusCode(t, rr.Code)
}
func TestServerGetVehiclesByManufacturer(t *testing.T) {
server, repositoryClose, serviceClose := BuildTestServer(t)
defer repositoryClose()
defer serviceClose()
req, err := http.NewRequest("GET", "/manufacturers/0005/vehicles", nil)
if err != nil {
t.Fatal(err)
}
req.Host = "localhost"
req.Host = "processing.envirocar.org"
req.Header.Add("Host", "processing.envirocar.org")
req.Header.Add("accept", "application/json")
rr := httptest.NewRecorder()
t.Log("get vehicles by manufacturer")
server.ServeHTTP(rr, req)
AssertOkStatusCode(t, rr.Code)
}
func TestServerGetPowerSources(t *testing.T) {
server, repositoryClose, serviceClose := BuildTestServer(t)
defer repositoryClose()
defer serviceClose()
req, err := http.NewRequest("GET", "/powerSources", nil)
if err != nil {
t.Fatal(err)
}
req.Host = "localhost"
req.Host = "processing.envirocar.org"
req.Header.Add("Host", "processing.envirocar.org")
req.Header.Add("accept", "application/json")
rr := httptest.NewRecorder()
t.Log("get power sources")
server.ServeHTTP(rr, req)
AssertOkStatusCode(t, rr.Code)
}
func AssertOkStatusCode(t *testing.T, code int) {
// Check the status code is what we expect.
if code != http.StatusOK {
t.Fatalf("handler returned wrong status code: got %v want %v",
code, http.StatusOK)
}
}
func AssertResponseBody(t *testing.T, got, want string) {
// Check the response body is what we expect.
if strings.TrimSpace(got) != want {
t.Fatalf("handler returned unexpected body: got %v want %v",
got, want)
}
}