forked from CrunchyData/pg_featureserv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_test.go
486 lines (393 loc) · 14.9 KB
/
handler_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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
package main
/*
Copyright 2019 Crunchy Data Solutions, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"reflect"
"runtime"
"testing"
"github.com/CrunchyData/pg_featureserv/api"
"github.com/CrunchyData/pg_featureserv/conf"
"github.com/CrunchyData/pg_featureserv/data"
)
// Define a FeatureCollection structure for parsing test data
type Feature struct {
Type string `json:"type"`
ID string `json:"id,omitempty"`
Geom *json.RawMessage `json:"geometry"`
Props map[string]interface{} `json:"properties"`
}
type FeatureCollection struct {
Type string `json:"type"`
Features []*Feature `json:"features"`
NumberMatched uint `json:"numberMatched,omitempty"`
NumberReturned uint `json:"numberReturned"`
TimeStamp string `json:"timeStamp,omitempty"`
Links []*api.Link `json:"links"`
}
const urlBase = "http://test"
// testConfig is a config spec for using in running tests
var testConfig conf.Config = conf.Config{
Server: conf.Server{
HttpHost: "0.0.0.0",
HttpPort: 9000,
UrlBase: urlBase,
AssetsPath: "./assets",
TransformFunctions: []string{
"ST_Centroid",
"ST_PointOnSurface",
},
},
Paging: conf.Paging{
LimitDefault: 10,
LimitMax: 1000,
},
Metadata: conf.Metadata{
Title: "test",
Description: "test",
},
}
var catalogMock *data.CatalogMock
func TestMain(m *testing.M) {
catalogMock = data.CatMockInstance()
catalogInstance = catalogMock
router = initRouter()
conf.Configuration = testConfig
initTransforms(conf.Configuration.Server.TransformFunctions)
os.Exit(m.Run())
}
func TestRoot(t *testing.T) {
resp := doRequest(t, "/")
body, _ := ioutil.ReadAll(resp.Body)
var v api.RootInfo
json.Unmarshal(body, &v)
checkLink(t, v.Links[0], api.RelSelf, api.ContentTypeJSON, urlBase+"/"+api.RootPageName+".json")
checkLink(t, v.Links[1], api.RelAlt, api.ContentTypeHTML, urlBase+"/"+api.RootPageName+".html")
checkLink(t, v.Links[2], api.RelData, api.ContentTypeJSON, urlBase+"/collections.json")
checkLink(t, v.Links[3], api.RelFunctions, api.ContentTypeJSON, urlBase+"/functions.json")
/*
fmt.Println("Response ==>")
fmt.Println(v.Title)
fmt.Println(v.Description)
*/
}
func TestCollectionsResponse(t *testing.T) {
path := "/collections"
resp := doRequest(t, path)
body, _ := ioutil.ReadAll(resp.Body)
var v api.CollectionsInfo
json.Unmarshal(body, &v)
checkLink(t, v.Links[0], api.RelSelf, api.ContentTypeJSON, urlBase+path+".json")
checkLink(t, v.Links[1], api.RelAlt, api.ContentTypeHTML, urlBase+path+".html")
checkCollection(t, v.Collections[0], "mock_a", "Mock A")
checkCollection(t, v.Collections[1], "mock_b", "Mock B")
checkCollection(t, v.Collections[2], "mock_c", "Mock C")
}
func TestCollectionResponse(t *testing.T) {
path := "/collections/mock_a"
resp := doRequest(t, path)
body, _ := ioutil.ReadAll(resp.Body)
var v api.CollectionInfo
json.Unmarshal(body, &v)
// use mock data as expected
tbl := catalogMock.TableDefs[0]
equals(t, tbl.ID, v.Name, "Name")
equals(t, tbl.Title, v.Title, "Title")
equals(t, tbl.Description, v.Description, "Description")
// check properties
equals(t, len(tbl.Columns), len(v.Properties), "Properties len")
for i := 0; i < len(v.Properties); i++ {
equals(t, tbl.Columns[i], v.Properties[i].Name, "Properties[].Name")
equals(t, tbl.JSONTypes[i], v.Properties[i].Type, "Properties[].Type")
equals(t, tbl.ColDesc[i], v.Properties[i].Description, "Properties[].Description")
}
checkLink(t, v.Links[0], api.RelSelf, api.ContentTypeJSON, urlBase+path+".json")
checkLink(t, v.Links[1], api.RelAlt, api.ContentTypeHTML, urlBase+path+".html")
checkLink(t, v.Links[2], api.RelItems, api.ContentTypeGeoJSON, urlBase+path+"/items.json")
}
func TestCollectionItemsResponse(t *testing.T) {
path := "/collections/mock_a/items"
resp := doRequest(t, path)
body, _ := ioutil.ReadAll(resp.Body)
var v api.FeatureCollectionRaw
json.Unmarshal(body, &v)
equals(t, 9, len(v.Features), "# features")
checkLink(t, v.Links[0], api.RelSelf, api.ContentTypeJSON, urlBase+path+".json")
checkLink(t, v.Links[1], api.RelAlt, api.ContentTypeHTML, urlBase+path+".html")
}
func TestFilterB(t *testing.T) {
rr := doRequest(t, "/collections/mock_a/items?prop_b=1")
var v FeatureCollection
json.Unmarshal(readBody(rr), &v)
equals(t, 1, len(v.Features), "# features")
}
func TestFilterD(t *testing.T) {
rr := doRequest(t, "/collections/mock_c/items?prop_d=1")
var v FeatureCollection
json.Unmarshal(readBody(rr), &v)
equals(t, 10, len(v.Features), "# features")
}
func TestFilterBD(t *testing.T) {
rr := doRequest(t, "/collections/mock_c/items?prop_b=2&prop_d=2")
var v FeatureCollection
json.Unmarshal(readBody(rr), &v)
equals(t, 1, len(v.Features), "# features")
}
func TestFilterBDNone(t *testing.T) {
rr := doRequest(t, "/collections/mock_c/items?prop_b=1&prop_d=2")
var v FeatureCollection
json.Unmarshal(readBody(rr), &v)
equals(t, 0, len(v.Features), "# features")
}
func TestLimit(t *testing.T) {
rr := doRequest(t, "/collections/mock_a/items?limit=3")
var v FeatureCollection
json.Unmarshal(readBody(rr), &v)
equals(t, 3, len(v.Features), "# features")
equals(t, "1", v.Features[0].ID, "feature 1 id")
equals(t, "2", v.Features[1].ID, "feature 2 id")
equals(t, "3", v.Features[2].ID, "feature 3 id")
}
func TestLimitInvalid(t *testing.T) {
doRequestStatus(t, "/collections/mock_a/items?limit=x", http.StatusBadRequest)
}
func TestQueryParamCase(t *testing.T) {
rr := doRequest(t, "/collections/mock_a/items?LIMIT=2&Offset=4")
var v FeatureCollection
json.Unmarshal(readBody(rr), &v)
equals(t, 2, len(v.Features), "# features")
equals(t, "5", v.Features[0].ID, "feature 5 id")
equals(t, "6", v.Features[1].ID, "feature 6 id")
}
func TestOffset(t *testing.T) {
rr := doRequest(t, "/collections/mock_a/items?limit=2&offset=4")
var v FeatureCollection
json.Unmarshal(readBody(rr), &v)
equals(t, 2, len(v.Features), "# features")
equals(t, "5", v.Features[0].ID, "feature 5 id")
equals(t, "6", v.Features[1].ID, "feature 6 id")
}
func TestOffsetInvalid(t *testing.T) {
doRequestStatus(t, "/collections/mock_a/items?offset=x", http.StatusBadRequest)
}
func TestTransformValid(t *testing.T) {
doRequest(t, "/collections/mock_a/items?transform=centroid")
doRequest(t, "/collections/mock_a/items?transform=ST_centroid")
doRequest(t, "/collections/mock_a/items?transform=st_centroid")
doRequest(t, "/collections/mock_a/items?transform=pointonsurface")
doRequest(t, "/collections/mock_a/items?transform=pointonsurface|centroid")
}
func TestTransformInvalid(t *testing.T) {
// envelope is not defined as a transform function
doRequestStatus(t, "/collections/mock_a/items?transform=envelope", http.StatusBadRequest)
doRequestStatus(t, "/collections/mock_a/items?transform=centroid|envelope", http.StatusBadRequest)
}
func TestBBox(t *testing.T) {
doRequest(t, "/collections/mock_a/items?bbox=1,2,3,4")
// TODO: add some tests
}
func TestBBoxInvalid(t *testing.T) {
doRequestStatus(t, "/collections/mock_a/items?bbox=1,2,3,x", http.StatusBadRequest)
}
func TestProperties(t *testing.T) {
// Tests:
// - property names are non-case-sensitive
// - names are made unique (properties only include once)
// - non-existing names are ignored
rr := doRequest(t, "/collections/mock_a/items?limit=2&properties=PROP_A,prop_C,prop_a,not_prop")
var v FeatureCollection
json.Unmarshal(readBody(rr), &v)
equals(t, 2, len(v.Features), "# features")
equals(t, 2, len(v.Features[0].Props), "feature 1 # properties")
equals(t, "propA", v.Features[0].Props["prop_a"], "feature 1 # property A")
equals(t, "propC", v.Features[0].Props["prop_c"], "feature 1 # property C")
}
// TestPropertiesAll tests that no properties parameter returns all props
func TestPropertiesAll(t *testing.T) {
rr := doRequest(t, "/collections/mock_a/items?limit=2")
var v FeatureCollection
json.Unmarshal(readBody(rr), &v)
// Note that JSON numbers are read as float64
equals(t, 2, len(v.Features), "# features")
equals(t, 4, len(v.Features[0].Props), "feature 1 # properties")
equals(t, "propA", v.Features[0].Props["prop_a"], "feature 1 # property A")
equals(t, 1.0, v.Features[0].Props["prop_b"], "feature 1 # property B")
equals(t, "propC", v.Features[0].Props["prop_c"], "feature 1 # property C")
equals(t, 1.0, v.Features[0].Props["prop_d"], "feature 1 # property D")
}
func TestCollectionNoFound(t *testing.T) {
doRequestStatus(t, "/collections/missing", http.StatusNotFound)
}
func TestCollectionItemsNoFound(t *testing.T) {
doRequestStatus(t, "/collections/missing/items", http.StatusNotFound)
}
func TestFeatureNotFound(t *testing.T) {
doRequestStatus(t, "/collections/mock_a/items/999", http.StatusNotFound)
}
//============= Test functions
func TestFunctionsJSON(t *testing.T) {
path := "/functions"
resp := doRequest(t, path)
body, _ := ioutil.ReadAll(resp.Body)
var v api.FunctionsInfo
json.Unmarshal(body, &v)
checkLink(t, v.Links[0], api.RelSelf, api.ContentTypeJSON, urlBase+path+".json")
checkLink(t, v.Links[1], api.RelAlt, api.ContentTypeHTML, urlBase+path+".html")
for i, fun := range catalogMock.FunctionDefs {
checkFunctionSummary(t, v.Functions[i], fun)
}
}
func TestFunctionJSON(t *testing.T) {
for _, fun := range catalogMock.FunctionDefs {
//fun := catalogMock.FunctionDefs[1]
checkFunction(t, fun)
}
}
func TestFunctionNoFound(t *testing.T) {
doRequestStatus(t, "/functions/missing", http.StatusNotFound)
}
func TestFunctionItemsNoFound(t *testing.T) {
doRequestStatus(t, "/functions/missing/items", http.StatusNotFound)
}
//============ Test HTML generation
// For now these just test that the template executes correctly
// correctness/completess of HTML is not tested
func TestHTMLRoot(t *testing.T) {
doRequest(t, "/index.html")
}
func TestHTMLConformance(t *testing.T) {
doRequest(t, "/conformance.html")
}
func TestHTMLCollections(t *testing.T) {
doRequest(t, "/collections.html")
}
func TestHTMLCollection(t *testing.T) {
doRequest(t, "/collections/mock_a.html")
}
func TestHTMLItems(t *testing.T) {
doRequest(t, "/collections/mock_a/items.html")
}
func TestHTMLItem(t *testing.T) {
doRequest(t, "/collections/mock_a/items/1.html")
}
func TestHTMLFunctions(t *testing.T) {
doRequest(t, "/functions.html")
}
func TestHTMLFunction(t *testing.T) {
doRequest(t, "/functions/fun_a.html")
}
//===================================================
func readBody(resp *httptest.ResponseRecorder) []byte {
body, _ := ioutil.ReadAll(resp.Body)
return body
}
func doRequest(t *testing.T, url string) *httptest.ResponseRecorder {
return doRequestStatus(t, url, http.StatusOK)
}
func doRequestStatus(t *testing.T, url string,
statusExpected int) *httptest.ResponseRecorder {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
router.ServeHTTP(rr, req)
// Check the status code
//fmt.Println("Status:", rr.Code)
if status := rr.Code; status != statusExpected {
t.Errorf("handler returned wrong status code: got %v want %v",
status, statusExpected)
}
return rr
}
func checkCollection(tb testing.TB, coll *api.CollectionInfo, name string, title string) {
equals(tb, name, coll.Name, "Collection name")
equals(tb, title, coll.Title, "Collection title")
path := "/collections/" + name
checkLink(tb, coll.Links[0], api.RelSelf, api.ContentTypeJSON, urlBase+path+".json")
checkLink(tb, coll.Links[1], api.RelAlt, api.ContentTypeHTML, urlBase+path+".html")
pathItems := path + "/items"
checkLink(tb, coll.Links[2], api.RelItems, api.ContentTypeGeoJSON, urlBase+pathItems+".json")
}
func checkLink(tb testing.TB, link *api.Link, rel string, conType string, href string) {
equals(tb, rel, link.Rel, "Link rel")
equals(tb, conType, link.Type, "Link type")
equals(tb, href, link.Href, "Link href")
}
func checkFunctionSummary(tb testing.TB, v *api.FunctionSummary, fun *data.Function) {
equals(tb, fun.Name, v.Name, "Function name")
equals(tb, fun.Description, v.Description, "Function description")
path := "/functions/" + fun.Name
checkLink(tb, v.Links[0], api.RelSelf, api.ContentTypeJSON, urlBase+path+".json")
checkLink(tb, v.Links[1], api.RelAlt, api.ContentTypeHTML, urlBase+path+".html")
pathItems := path + "/items"
itemsType := api.ContentTypeJSON
if fun.IsGeometryFunction() {
itemsType = api.ContentTypeGeoJSON
}
checkLink(tb, v.Links[2], api.RelItems, itemsType, urlBase+pathItems+".json")
}
func checkFunction(t *testing.T, fun *data.Function) {
path := "/functions/" + fun.ID
resp := doRequest(t, path)
body, _ := ioutil.ReadAll(resp.Body)
var v api.FunctionInfo
json.Unmarshal(body, &v)
equals(t, fun.ID, v.Name, "Name")
equals(t, fun.Description, v.Description, "Description")
//--- check parameters
assert(t, v.Parameters != nil, "Parameters property must be present")
equals(t, len(fun.InNames), len(v.Parameters), "Parameters len")
for i := 0; i < len(v.Parameters); i++ {
equals(t, fun.InNames[i], v.Parameters[i].Name, "Parameters[].Name")
equals(t, fun.InDbTypes[i], v.Parameters[i].Type, "Parameters[].Type")
}
//--- check properties
assert(t, v.Properties != nil, "Properties property must be present")
equals(t, len(fun.OutNames), len(v.Properties), "Properties len")
for i := 0; i < len(v.Properties); i++ {
equals(t, fun.OutNames[i], v.Properties[i].Name, "Properties[].Name")
equals(t, fun.OutJSONTypes[i], v.Properties[i].Type, "Properties[].Type")
}
//--- check links
checkLink(t, v.Links[0], api.RelSelf, api.ContentTypeJSON, urlBase+path+".json")
checkLink(t, v.Links[1], api.RelAlt, api.ContentTypeHTML, urlBase+path+".html")
itemsType := api.ContentTypeJSON
if fun.IsGeometryFunction() {
itemsType = api.ContentTypeGeoJSON
}
checkLink(t, v.Links[2], api.RelItems, itemsType, urlBase+path+"/items.json")
}
//---- testing utilities from https://github.com/benbjohnson/testing
// assert fails the test if the condition is false.
func assert(tb testing.TB, condition bool, msg string, v ...interface{}) {
if !condition {
_, file, line, _ := runtime.Caller(1)
fmt.Printf("\033[31m%s:%d: "+msg+"\033[39m\n\n", append([]interface{}{filepath.Base(file), line}, v...)...)
tb.FailNow()
}
}
// equals fails the test if exp is not equal to act.
func equals(tb testing.TB, exp, act interface{}, msg string) {
if !reflect.DeepEqual(exp, act) {
_, file, line, _ := runtime.Caller(1)
fmt.Printf("%s:%d: %s - expected: %#v; got: %#v\n", filepath.Base(file), line, msg, exp, act)
tb.FailNow()
}
}