This repository has been archived by the owner on Feb 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathprimary_test.go
81 lines (61 loc) · 1.49 KB
/
primary_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
package api
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/gorilla/mux"
)
func TestRequest(t *testing.T) {
t.Parallel()
context := &context{}
r := mux.NewRouter()
setupPrimaryRouter(r, context, false)
w := httptest.NewRecorder()
req, e := http.NewRequest("GET", "/version", nil)
if nil != e {
t.Fatalf("couldn't set up test request")
}
r.ServeHTTP(w, req)
t.Logf("%d - %s", w.Code, w.Body.String())
t.Log("Expecting no extra Headers")
for k, v := range w.Header() {
t.Log(k, " : ", v)
}
if w.Code == 404 {
t.Fatalf("failed not found")
}
}
func TestCorsRequest(t *testing.T) {
t.Parallel()
context := &context{}
primary := mux.NewRouter()
setupPrimaryRouter(primary, context, true)
w := httptest.NewRecorder()
// test an OPTIONS request when cors enabled
r, e := http.NewRequest("OPTIONS", "/version", nil)
if nil != e {
t.Fatalf("couldn't set up test request")
}
primary.ServeHTTP(w, r)
t.Logf("%d - %s", w.Code, w.Body.String())
t.Log("Expecting extra cors Headers")
for k, v := range w.Header() {
t.Log(k, " : ", v)
}
if w.Code == 404 {
t.Fatalf("failed not found")
}
// test a normal request ( GET /_ping ) when cors enabled
w2 := httptest.NewRecorder()
r2, e2 := http.NewRequest("GET", "/_ping", nil)
if nil != e2 {
t.Fatalf("couldn't set up test request")
}
primary.ServeHTTP(w2, r2)
if w2.Body.String() != "OK" {
t.Fatalf("couldn't get body content when cors enabled")
}
if w2.Code == 404 {
t.Fatalf("failed not found")
}
}