-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathversion_test.go
62 lines (53 loc) · 1.7 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
56
57
58
59
60
61
62
package luddite
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestNonPositiveApiVersionConstraint(t *testing.T) {
req, _ := http.NewRequest("GET", "/", nil)
req.Header.Add(HeaderSpirentApiVersion, "0")
rw := httptest.NewRecorder()
rw.Header().Set(HeaderContentType, ContentTypeJson)
v := newVersionHandler(2, 42)
v.ServeHTTP(rw, req)
if rw.Code != http.StatusBadRequest {
t.Error("expected 400/Bad request")
}
}
func TestMinApiVersionConstraint(t *testing.T) {
req, _ := http.NewRequest("GET", "/", nil)
req.Header.Add(HeaderSpirentApiVersion, "1")
rw := httptest.NewRecorder()
rw.Header().Set(HeaderContentType, ContentTypeJson)
v := newVersionHandler(2, 42)
v.ServeHTTP(rw, req)
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()
rw.Header().Set(HeaderContentType, ContentTypeJson)
v := newVersionHandler(2, 42)
v.ServeHTTP(rw, req)
if rw.Code != http.StatusNotImplemented {
t.Error("expected 501/Not Implemented response for future version")
}
}
func TestApiVersionContext(t *testing.T) {
req, _ := http.NewRequest("GET", "/", nil)
req.Header.Add(HeaderSpirentApiVersion, "1")
req = req.WithContext(withHandlerDetails(req.Context(), &handlerDetails{}))
rw := httptest.NewRecorder()
v := newVersionHandler(1, 1)
v.ServeHTTP(rw, req)
if ContextApiVersion(req.Context()) != 1 {
t.Error("missing API version in request context")
}
if _, ok := rw.HeaderMap[HeaderSpirentApiVersion]; !ok {
t.Errorf("missing %s header in response", HeaderSpirentApiVersion)
}
}