-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcors_test.go
More file actions
35 lines (32 loc) · 899 Bytes
/
cors_test.go
File metadata and controls
35 lines (32 loc) · 899 Bytes
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
package httpserver
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestCORSAll_Options(t *testing.T) {
called := false
h := corsAll(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { called = true; w.WriteHeader(200) }))
rr := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodOptions, "/x", nil)
h.ServeHTTP(rr, r)
if called {
t.Fatalf("inner handler should not run for OPTIONS")
}
if rr.Code != http.StatusNoContent {
t.Fatalf("code: %d", rr.Code)
}
}
func TestCORSAll_Passthrough(t *testing.T) {
called := false
h := corsAll(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { called = true; w.WriteHeader(201) }))
rr := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/x", nil)
h.ServeHTTP(rr, r)
if !called {
t.Fatalf("inner handler not called")
}
if rr.Code != 201 {
t.Fatalf("code: %d", rr.Code)
}
}