forked from StationA/tilenol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.go
134 lines (124 loc) · 3.35 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
package tilenol
import (
"bytes"
"context"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
func TestFilterLayersByName(t *testing.T) {
layers := []Layer{
Layer{Name: "a"},
Layer{Name: "b"},
Layer{Name: "c"},
}
filtered := filterLayersByNames(layers, []string{"a", "c", "doesntexist"})
if len(filtered) != 2 {
t.Errorf("Filtered layers should have been length 2, instead was: %d", len(filtered))
}
if filtered[0].Name != "a" {
t.Errorf("Expected first layer to be 'a', got: %s", filtered[0].Name)
}
if filtered[1].Name != "c" {
t.Errorf("Expected second layer to be 'c', got: %s", filtered[1].Name)
}
}
func TestFilterLayersByZoom(t *testing.T) {
layers := []Layer{
Layer{Name: "a", Minzoom: 10},
Layer{Name: "b", Maxzoom: 10},
Layer{Name: "c", Minzoom: 5, Maxzoom: 15},
}
filteredZ0 := filterLayersByZoom(layers, 0)
if len(filteredZ0) != 1 {
t.Error("z = 0")
}
filteredZ5 := filterLayersByZoom(layers, 5)
if len(filteredZ5) != 2 {
t.Error("z = 5")
}
filteredZ10 := filterLayersByZoom(layers, 10)
if len(filteredZ10) != 3 {
t.Error("z = 10")
}
filteredZ20 := filterLayersByZoom(layers, 20)
if len(filteredZ20) != 1 {
t.Error("z = 20")
}
}
func TestCalculateSimplificationThreshold(t *testing.T) {
if calculateSimplificationThreshold(0, 20, 0) > MaxSimplify {
t.Error("Simplification exceeds MaxSimplify")
}
if calculateSimplificationThreshold(0, 20, 20) < MinSimplify {
t.Error("Simplification is below MinSimplify")
}
}
func TestCachedHandler(t *testing.T) {
server := &Server{Cache: NewInMemoryCache()}
var requests []interface{}
handler := func(context.Context, io.Writer, *http.Request) error {
requests = append(requests, nil)
return nil
}
cachedHandler := server.cached(handler)
for i := 0; i < 100; i++ {
body := ioutil.NopCloser(bytes.NewReader([]byte{}))
r := httptest.NewRequest("GET", "/_all/0/0/0.mvt", body)
w := httptest.NewRecorder()
cachedHandler.ServeHTTP(w, r)
res := w.Result()
if res.StatusCode != 200 {
t.Error("Unsuccessful status code")
}
}
if len(requests) != 1 {
t.Error("Request not cached")
}
}
func TestUnCachedHandler(t *testing.T) {
server := &Server{Cache: &NilCache{}}
var requests []interface{}
handler := func(context.Context, io.Writer, *http.Request) error {
requests = append(requests, nil)
return nil
}
cachedHandler := server.cached(handler)
for i := 0; i < 100; i++ {
body := ioutil.NopCloser(bytes.NewReader([]byte{}))
r := httptest.NewRequest("GET", "/_all/0/0/0.mvt", body)
w := httptest.NewRecorder()
cachedHandler.ServeHTTP(w, r)
res := w.Result()
if res.StatusCode != 200 {
t.Error("Unsuccessful status code")
}
}
if len(requests) != 100 {
t.Error("Requests should not be cached")
}
}
func TestAPI(t *testing.T) {
server := &Server{Cache: &NilCache{}}
api, internal := server.setupRoutes()
// Test tile endpoint
body := ioutil.NopCloser(bytes.NewReader([]byte{}))
r := httptest.NewRequest("GET", "/_all/0/0/0.mvt", body)
w := httptest.NewRecorder()
api.ServeHTTP(w, r)
res := w.Result()
if res.StatusCode != 200 {
t.Error("Non-200 tile response")
}
// Test healthcheck
body = ioutil.NopCloser(bytes.NewReader([]byte{}))
r = httptest.NewRequest("GET", "/healthcheck", body)
w = httptest.NewRecorder()
internal.ServeHTTP(w, r)
res = w.Result()
if res.StatusCode != 200 {
t.Error("Non-200 healthcheck response")
}
}