forked from go-spatial/tegola
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_cors_test.go
60 lines (47 loc) · 1.41 KB
/
server_cors_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
package server_test
import (
"net/http"
"net/http/httptest"
"reflect"
"testing"
"github.com/go-spatial/tegola/server"
)
const (
DefaultCORSAllowedOrigin = "*"
DefaultCORSAllowedMethods = "GET, OPTIONS"
)
type CORSTestCase struct {
hostname string
port string
uri string
}
func CORSTest(tc CORSTestCase) func(*testing.T) {
return func(t *testing.T) {
var err error
server.HostName = tc.hostname
server.Port = tc.port
server.URIPrefix = "/"
// setup a new router. this handles parsing our URL wildcards (i.e. :map_name, :z, :x, :y)
router := server.NewRouter(nil)
r, err := http.NewRequest(http.MethodOptions, tc.uri, nil)
if err != nil {
t.Fatal(err)
return
}
w := httptest.NewRecorder()
router.ServeHTTP(w, r)
if w.Code != http.StatusOK {
t.Errorf("wrong status code: expected %v, got %v", http.StatusOK, w.Code)
return
}
headers := w.Header()
if !reflect.DeepEqual(headers["Access-Control-Allow-Origin"], []string{DefaultCORSAllowedOrigin}) {
t.Errorf("wrong header for Access-Control-Allow-Origin. expected %v got %v", DefaultCORSAllowedOrigin, headers["Access-Control-Allow-Origin"])
return
}
if !reflect.DeepEqual(headers["Access-Control-Allow-Methods"], []string{"GET, OPTIONS"}) {
t.Errorf("wrong header for Access-Control-Allow-Methods. expected %v got %v", []string{"GET", "OPTIONS"}, headers["Access-Control-Allow-Methods"])
return
}
}
}