-
Notifications
You must be signed in to change notification settings - Fork 157
/
main_test.go
166 lines (143 loc) · 4.95 KB
/
main_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
package main
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)
var db *pgxpool.Pool
var dbsetup = false
func TestMain(m *testing.M) {
viper.Set("DbConnection", os.Getenv("TEST_DATABASE_URL"))
// viper.Set("DbConnection", os.Getenv("dbname=ts"))
db, err := dbConnect()
if err != nil {
os.Exit(1)
}
sql := "CREATE EXTENSION IF NOT EXISTS postgis"
_, err = db.Exec(context.Background(), sql)
if err != nil {
fmt.Printf("Error creating extension: %s", err)
os.Exit(1)
}
dbsetup = true
os.Exit(m.Run())
}
func TestDBNoTables(t *testing.T) {
if !dbsetup {
t.Skip("DB integration test suite setup failed, skipping")
}
r := tileRouter()
request, _ := http.NewRequest("GET", "/index.json", nil)
response := httptest.NewRecorder()
r.ServeHTTP(response, request)
assert.Equal(t, 200, response.Code, "OK response is expected")
jsonResult := strings.TrimSpace(response.Body.String())
jsonExpect := "{}"
assert.Equal(t, jsonExpect, jsonResult, "empty json response is expected")
}
// TestBasePath sets an alternate base path to check that handlers are
// mounted at the specified path
func TestBasePath(t *testing.T) {
if !dbsetup {
t.Skip("DB integration test suite setup failed, skipping")
}
// paths to check
paths := []string{"/test", "/test/"}
for _, path := range paths {
viper.Set("BasePath", path)
r := tileRouter()
request, _ := http.NewRequest("GET", "/test/index.json", nil)
response := httptest.NewRecorder()
r.ServeHTTP(response, request)
assert.Equal(t, 200, response.Code, "OK response is expected")
request, _ = http.NewRequest("GET", "/test/health", nil)
response = httptest.NewRecorder()
r.ServeHTTP(response, request)
assert.Equal(t, 200, response.Code, "OK response is expected")
}
// cleanup
viper.Set("BasePath", "/")
}
// Test that the preview endpoints are hidden or shown according to the config
func TestShowPreview(t *testing.T) {
viper.Set("ShowPreview", true)
r := tileRouter()
request, _ := http.NewRequest("GET", "/index.json", nil)
response := httptest.NewRecorder()
r.ServeHTTP(response, request)
assert.Equal(t, 200, response.Code, "OK response is expected")
request, _ = http.NewRequest("GET", "/index.html", nil)
response = httptest.NewRecorder()
r.ServeHTTP(response, request)
assert.Equal(t, 200, response.Code, "OK response is expected")
}
// the current default behavior is to show the preview
func TestShowPreviewDefault(t *testing.T) {
r := tileRouter()
request, _ := http.NewRequest("GET", "/index.json", nil)
response := httptest.NewRecorder()
r.ServeHTTP(response, request)
assert.Equal(t, 200, response.Code, "OK response is expected")
request, _ = http.NewRequest("GET", "/index.html", nil)
response = httptest.NewRecorder()
r.ServeHTTP(response, request)
assert.Equal(t, 200, response.Code, "OK response is expected")
}
func TestHidePreview(t *testing.T) {
viper.Set("ShowPreview", false)
r := tileRouter()
request, _ := http.NewRequest("GET", "/index.json", nil)
response := httptest.NewRecorder()
r.ServeHTTP(response, request)
assert.Equal(t, 404, response.Code, "Not Found response is expected")
request, _ = http.NewRequest("GET", "/index.html", nil)
response = httptest.NewRecorder()
r.ServeHTTP(response, request)
assert.Equal(t, 404, response.Code, "Not Found response is expected")
// cleanup
viper.Set("ShowPreview", true)
}
// Test that the health endpoint gives a 200 if the server is running
func TestHealth(t *testing.T) {
r := tileRouter()
request, _ := http.NewRequest("GET", "/health", nil)
response := httptest.NewRecorder()
r.ServeHTTP(response, request)
assert.Equal(t, 200, response.Code, "OK response is expected")
assert.Equal(t, "200 OK", string(response.Result().Status), "Response status should say ok")
}
func TestHealthCustomUrl(t *testing.T) {
viper.Set("HealthEndpoint", "/testHealthABC")
r := tileRouter()
request, _ := http.NewRequest("GET", "/health", nil)
response := httptest.NewRecorder()
r.ServeHTTP(response, request)
assert.Equal(t, 404, response.Code, "Not Found response is expected")
request, _ = http.NewRequest("GET", "/testHealthABC", nil)
response = httptest.NewRecorder()
r.ServeHTTP(response, request)
assert.Equal(t, 200, response.Code, "OK response is expected")
assert.Equal(t, "200 OK", string(response.Result().Status), "Response status should say ok")
// cleanup
viper.Set("HealthEndpoint", "/health")
}
func TestHealthCustomUrlWithBasePath(t *testing.T) {
viper.Set("BasePath", "/foo")
viper.Set("HealthEndpoint", "/bar")
r := tileRouter()
request, _ := http.NewRequest("GET", "/foo/bar", nil)
response := httptest.NewRecorder()
r.ServeHTTP(response, request)
assert.Equal(t, 200, response.Code, "OK response is expected")
assert.Equal(t, "200 OK", string(response.Result().Status), "Response status should say ok")
// cleanup
viper.Set("HealthEndpoint", "/health")
viper.Set("BasePath", "/")
}