-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
vulcain_test.go
113 lines (96 loc) · 2.82 KB
/
vulcain_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
package vulcain
import (
"net/http"
"net/url"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNew(t *testing.T) {
g := New()
assert.NotNil(t, g)
}
func TestParseRelation(t *testing.T) {
v := New(WithOpenAPIFile(openapiFixture))
u, _ := url.Parse("/oa/books/123")
u, _, _ = v.parseRelation("/author", "123", v.getOpenAPIRoute(u, nil, false))
assert.Equal(t, "/oa/authors/123", u.String())
u, _, _ = v.parseRelation("/invalid", " http://foo.com", nil)
assert.Nil(t, u)
}
func TestIsValidRequest(t *testing.T) {
v := New()
assert.False(t, v.IsValidRequest(&http.Request{URL: &url.URL{}}))
assert.True(t, v.IsValidRequest(&http.Request{URL: &url.URL{RawQuery: `preload="/foo"`}}))
assert.True(t, v.IsValidRequest(&http.Request{URL: &url.URL{RawQuery: `fields="/foo"`}}))
assert.True(t, v.IsValidRequest(&http.Request{
Header: http.Header{"Preload": []string{`"/foo"`}},
URL: &url.URL{},
}))
assert.True(t, v.IsValidRequest(&http.Request{
Header: http.Header{"Fields": []string{`"/foo"`}},
URL: &url.URL{},
}))
}
func TestIsValidResponse(t *testing.T) {
v := New()
assert.False(t, v.IsValidResponse(
&http.Request{URL: &url.URL{}},
200,
http.Header{"Content-Type": []string{"text/xml"}, "Cache-Control": []string{"no-transform"}},
))
assert.False(t, v.IsValidResponse(
&http.Request{
URL: &url.URL{},
Header: http.Header{"Preload": []string{`"/foo"`}, "Prefer": []string{"selector=css"}},
},
200,
http.Header{"Content-Type": []string{"application/json"}},
))
assert.False(t, v.IsValidResponse(
&http.Request{
URL: &url.URL{},
Header: http.Header{"Preload": []string{`"/foo"`}, "Prefer": []string{"selector=css"}},
},
500,
http.Header{"Content-Type": []string{"application/json"}},
))
assert.False(t, v.IsValidResponse(
&http.Request{
URL: &url.URL{},
Header: http.Header{"Preload": []string{`"/foo"`}, "Prefer": []string{"selector=json-pointer"}},
},
200,
http.Header{"Cache-Control": []string{"no-transform"}},
))
assert.True(t, v.IsValidResponse(
&http.Request{
URL: &url.URL{},
Header: http.Header{"Preload": []string{`"/foo"`}, "Prefer": []string{"selector=json-pointer"}},
},
200,
http.Header{"Content-Type": []string{"application/json"}},
))
assert.True(t, v.IsValidResponse(
&http.Request{
URL: &url.URL{},
Header: http.Header{"Preload": []string{`"/foo"`}},
},
200,
http.Header{"Content-Type": []string{"application/ld+json"}},
))
assert.True(t, v.IsValidResponse(
&http.Request{
URL: &url.URL{},
Header: http.Header{"Preload": []string{`"/foo"`}},
},
200,
http.Header{"Content-Type": []string{"application/ld+json"}},
))
assert.True(t, v.IsValidResponse(
&http.Request{
URL: &url.URL{RawQuery: `preload="/foo"`},
},
200,
http.Header{"Content-Type": []string{"application/ld+json"}},
))
}