-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathschema_test.go
119 lines (98 loc) · 3.4 KB
/
schema_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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package luddite
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/dimfeld/httptreemux"
"golang.org/x/tools/godoc/vfs/httpfs"
"golang.org/x/tools/godoc/vfs/mapfs"
)
const (
sampleJSONSchema = "{\"type\": \"object\", \"properties\": { \"name\": { \"type\": \"string\" }, \"birthday\": { \"type\": \"string\", \"format\": \"date\"}"
sampleYAMLSchema = "type: object\nproperties:\n name: {type: string}\n birthday: {type: string, format: date}"
)
func TestSchemaHandlerDefaultContentType(t *testing.T) {
fakeFS := httpfs.New(mapfs.New(map[string]string{
"v1/schema.json": sampleJSONSchema,
}))
v := make(map[string]string)
v["version"] = "v1"
v["filepath"] = "schema.json"
ctx := httptreemux.AddParamsToContext(context.Background(), v)
req, _ := http.NewRequest("GET", "/", nil)
req = req.WithContext(ctx)
rw := httptest.NewRecorder()
rw.Header().Set(HeaderContentType, ContentTypeJson)
s := newSchemaHandler(fakeFS)
s.ServeHTTP(rw, req)
if ct := rw.Header().Get(HeaderContentType); ct != ContentTypeJson {
t.Errorf("incorrrect content type negotiated: %s", ct)
}
if body := string(rw.Body.String()); body != sampleJSONSchema {
t.Errorf("JSON serialization failed, got: %s, expected: %s\n", body, sampleJSONSchema)
}
}
func TestSchemaHandlerOctetStreamContentType(t *testing.T) {
fakeFS := httpfs.New(mapfs.New(map[string]string{
"v1/schema.yml": sampleYAMLSchema,
}))
v := make(map[string]string)
v["version"] = "v1"
v["filepath"] = "schema.yml"
ctx := httptreemux.AddParamsToContext(context.Background(), v)
req, _ := http.NewRequest("GET", "/", nil)
req = req.WithContext(ctx)
rw := httptest.NewRecorder()
rw.Header().Set(HeaderContentType, ContentTypeJson)
s := newSchemaHandler(fakeFS)
s.ServeHTTP(rw, req)
if ct := rw.Header().Get(HeaderContentType); ct != ContentTypeOctetStream {
t.Errorf("incorrrect content type negotiated: %s", ct)
}
if body := string(rw.Body.String()); body != sampleYAMLSchema {
t.Errorf("YAML serialization failed, got: %s, expected: %s\n", body, sampleYAMLSchema)
}
}
func TestSchemaHandlerGivenInvalidVersionStringLength(t *testing.T) {
v := make(map[string]string)
v["version"] = "v"
ctx := httptreemux.AddParamsToContext(context.Background(), v)
req, _ := http.NewRequest("GET", "/", nil)
req = req.WithContext(ctx)
rw := httptest.NewRecorder()
rw.Header().Set(HeaderContentType, ContentTypeJson)
s := newSchemaHandler(nil)
s.ServeHTTP(rw, req)
if rw.Code != http.StatusNotFound {
t.Error("expected 400/Not found")
}
}
func TestSchemaHandlerGivenInvalidVersionValue(t *testing.T) {
v := make(map[string]string)
v["version"] = "w1"
ctx := httptreemux.AddParamsToContext(context.Background(), v)
req, _ := http.NewRequest("GET", "/", nil)
req = req.WithContext(ctx)
rw := httptest.NewRecorder()
rw.Header().Set(HeaderContentType, ContentTypeJson)
s := newSchemaHandler(nil)
s.ServeHTTP(rw, req)
if rw.Code != http.StatusNotFound {
t.Error("expected 400/Not found")
}
}
func TestSchemaHandlerGivenInvalidVersionNumber(t *testing.T) {
v := make(map[string]string)
v["version"] = "v0"
ctx := httptreemux.AddParamsToContext(context.Background(), v)
req, _ := http.NewRequest("GET", "/", nil)
req = req.WithContext(ctx)
rw := httptest.NewRecorder()
rw.Header().Set(HeaderContentType, ContentTypeJson)
s := newSchemaHandler(nil)
s.ServeHTTP(rw, req)
if rw.Code != http.StatusNotFound {
t.Error("expected 400/Not found")
}
}