-
Notifications
You must be signed in to change notification settings - Fork 9
/
version_test.go
55 lines (44 loc) · 1.38 KB
/
version_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
package luddite
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestMinApiVersionConstraint(t *testing.T) {
req, _ := http.NewRequest("GET", "/", nil)
req.Header.Add(HeaderSpirentApiVersion, "1")
rw := httptest.NewRecorder()
v := NewVersion(2, 42)
v.HandleHTTP(rw, req, func(http.ResponseWriter, *http.Request) {
if rw.Code != http.StatusGone {
t.Error("expected 410/Gone response for outdated version")
}
})
}
func TestMaxApiVersionConstraint(t *testing.T) {
req, _ := http.NewRequest("GET", "/", nil)
req.Header.Add(HeaderSpirentApiVersion, "43")
rw := httptest.NewRecorder()
v := NewVersion(2, 42)
v.HandleHTTP(rw, req, func(http.ResponseWriter, *http.Request) {
if rw.Code != http.StatusNotImplemented {
t.Error("expected 501/Not Implemented response for future version")
}
})
}
func TestApiVersionContext(t *testing.T) {
req0, _ := http.NewRequest("GET", "/", nil)
req0.Header.Add(HeaderSpirentApiVersion, "1")
req0 = req0.WithContext(withHandlerDetails(req0.Context(), &handlerDetails{}))
rw := httptest.NewRecorder()
v := NewVersion(1, 1)
v.HandleHTTP(rw, req0, func(_ http.ResponseWriter, req1 *http.Request) {
ctx := req1.Context()
if ContextApiVersion(ctx) != 1 {
t.Error("missing API version in context")
}
})
if _, ok := rw.HeaderMap[HeaderSpirentApiVersion]; !ok {
t.Errorf("missing %s header in response", HeaderSpirentApiVersion)
}
}