source: Main/trunk/contenttype/contenttype_test.go

Last change on this file was 4a5f10f, checked in by www <www@…>, 8 weeks ago

Mirrored from https://git.chaotic.ninja/git/yakumo_izuru/yukari

git-svn-id: https://svn.chaotic.ninja/svn/yukari-yakumo.izuru@1 ee30ecee-8fc8-4245-a645-e3b2cc3919a6

  • Property mode set to 100644
File size: 8.4 KB
Line 
1package contenttype
2
3import (
4 "bytes"
5 "fmt"
6 "testing"
7)
8
9type ParseContentTypeTestCase struct {
10 Input string
11 ExpectedOutput *ContentType /* or nil if an error is expected */
12 ExpectedString *string /* or nil if equals to Input */
13}
14
15var parseContentTypeTestCases []ParseContentTypeTestCase = []ParseContentTypeTestCase{
16 ParseContentTypeTestCase{
17 "text/html",
18 &ContentType{"text", "html", "", map[string]string{}},
19 nil,
20 },
21 ParseContentTypeTestCase{
22 "text/svg+xml; charset=UTF-8",
23 &ContentType{"text", "svg", "xml", map[string]string{"charset": "UTF-8"}},
24 nil,
25 },
26 ParseContentTypeTestCase{
27 "text/",
28 nil,
29 nil,
30 },
31 ParseContentTypeTestCase{
32 "text; charset=UTF-8",
33 &ContentType{"text", "", "", map[string]string{"charset": "UTF-8"}},
34 nil,
35 },
36 ParseContentTypeTestCase{
37 "text/+xml; charset=UTF-8",
38 &ContentType{"text", "", "xml", map[string]string{"charset": "UTF-8"}},
39 nil,
40 },
41}
42
43type ContentTypeEqualsTestCase struct {
44 A, B ContentType
45 Equals bool
46}
47
48var Map_Empty map[string]string = map[string]string{}
49var Map_A map[string]string = map[string]string{"a": "value_a"}
50var Map_B map[string]string = map[string]string{"b": "value_b"}
51var Map_AB map[string]string = map[string]string{"a": "value_a", "b": "value_b"}
52
53var ContentType_E ContentType = ContentType{"a", "b", "c", Map_Empty}
54var ContentType_A ContentType = ContentType{"a", "b", "c", Map_A}
55var ContentType_B ContentType = ContentType{"a", "b", "c", Map_B}
56var ContentType_AB ContentType = ContentType{"a", "b", "c", Map_AB}
57
58var contentTypeEqualsTestCases []ContentTypeEqualsTestCase = []ContentTypeEqualsTestCase{
59 // TopLevelType, SubType, Suffix
60 ContentTypeEqualsTestCase{ContentType_E, ContentType{"a", "b", "c", Map_Empty}, true},
61 ContentTypeEqualsTestCase{ContentType_E, ContentType{"o", "b", "c", Map_Empty}, false},
62 ContentTypeEqualsTestCase{ContentType_E, ContentType{"a", "o", "c", Map_Empty}, false},
63 ContentTypeEqualsTestCase{ContentType_E, ContentType{"a", "b", "o", Map_Empty}, false},
64 // Parameters
65 ContentTypeEqualsTestCase{ContentType_A, ContentType_A, true},
66 ContentTypeEqualsTestCase{ContentType_B, ContentType_B, true},
67 ContentTypeEqualsTestCase{ContentType_AB, ContentType_AB, true},
68 ContentTypeEqualsTestCase{ContentType_A, ContentType_E, false},
69 ContentTypeEqualsTestCase{ContentType_A, ContentType_B, false},
70 ContentTypeEqualsTestCase{ContentType_B, ContentType_A, false},
71 ContentTypeEqualsTestCase{ContentType_AB, ContentType_A, false},
72 ContentTypeEqualsTestCase{ContentType_AB, ContentType_E, false},
73 ContentTypeEqualsTestCase{ContentType_A, ContentType_AB, false},
74}
75
76type FilterTestCase struct {
77 Description string
78 Input Filter
79 TrueValues []ContentType
80 FalseValues []ContentType
81}
82
83var filterTestCases []FilterTestCase = []FilterTestCase{
84 FilterTestCase{
85 "contains xml",
86 NewFilterContains("xml"),
87 []ContentType{
88 ContentType{"xml", "", "", Map_Empty},
89 ContentType{"text", "xml", "", Map_Empty},
90 ContentType{"text", "html", "xml", Map_Empty},
91 },
92 []ContentType{
93 ContentType{"text", "svg", "", map[string]string{"script": "javascript"}},
94 ContentType{"java", "script", "", Map_Empty},
95 },
96 },
97 FilterTestCase{
98 "equals applications/xhtml",
99 NewFilterEquals("application", "xhtml", "*"),
100 []ContentType{
101 ContentType{"application", "xhtml", "xml", Map_Empty},
102 ContentType{"application", "xhtml", "", Map_Empty},
103 ContentType{"application", "xhtml", "zip", Map_Empty},
104 ContentType{"application", "xhtml", "zip", Map_AB},
105 },
106 []ContentType{
107 ContentType{"application", "javascript", "", Map_Empty},
108 ContentType{"text", "xhtml", "", Map_Empty},
109 },
110 },
111 FilterTestCase{
112 "equals application/*",
113 NewFilterEquals("application", "*", ""),
114 []ContentType{
115 ContentType{"application", "xhtml", "", Map_Empty},
116 ContentType{"application", "javascript", "", Map_Empty},
117 },
118 []ContentType{
119 ContentType{"text", "xhtml", "", Map_Empty},
120 ContentType{"text", "xhtml", "xml", Map_Empty},
121 },
122 },
123 FilterTestCase{
124 "equals applications */javascript",
125 NewFilterEquals("*", "javascript", ""),
126 []ContentType{
127 ContentType{"application", "javascript", "", Map_Empty},
128 ContentType{"text", "javascript", "", Map_Empty},
129 },
130 []ContentType{
131 ContentType{"text", "html", "", Map_Empty},
132 ContentType{"text", "javascript", "zip", Map_Empty},
133 },
134 },
135 FilterTestCase{
136 "equals applications/* or */javascript",
137 NewFilterOr([]Filter{
138 NewFilterEquals("application", "*", ""),
139 NewFilterEquals("*", "javascript", ""),
140 }),
141 []ContentType{
142 ContentType{"application", "javascript", "", Map_Empty},
143 ContentType{"text", "javascript", "", Map_Empty},
144 ContentType{"application", "xhtml", "", Map_Empty},
145 },
146 []ContentType{
147 ContentType{"text", "html", "", Map_Empty},
148 ContentType{"application", "xhtml", "xml", Map_Empty},
149 },
150 },
151}
152
153type FilterParametersTestCase struct {
154 Input map[string]string
155 Filter map[string]bool
156 Output map[string]string
157}
158
159var filterParametersTestCases []FilterParametersTestCase = []FilterParametersTestCase{
160 FilterParametersTestCase{
161 map[string]string{},
162 map[string]bool{"A": true, "B": true},
163 map[string]string{},
164 },
165 FilterParametersTestCase{
166 map[string]string{"A": "value_A", "B": "value_B"},
167 map[string]bool{},
168 map[string]string{},
169 },
170 FilterParametersTestCase{
171 map[string]string{"A": "value_A", "B": "value_B"},
172 map[string]bool{"A": true},
173 map[string]string{"A": "value_A"},
174 },
175 FilterParametersTestCase{
176 map[string]string{"A": "value_A", "B": "value_B"},
177 map[string]bool{"A": true, "B": true},
178 map[string]string{"A": "value_A", "B": "value_B"},
179 },
180}
181
182func TestContentTypeEquals(t *testing.T) {
183 for _, testCase := range contentTypeEqualsTestCases {
184 if !testCase.A.Equals(testCase.B) && testCase.Equals {
185 t.Errorf(`Must be equals "%s"="%s"`, testCase.A, testCase.B)
186 } else if testCase.A.Equals(testCase.B) && !testCase.Equals {
187 t.Errorf(`Mustn't be equals "%s"!="%s"`, testCase.A, testCase.B)
188 }
189 }
190}
191
192func TestParseContentType(t *testing.T) {
193 for _, testCase := range parseContentTypeTestCases {
194 // test ParseContentType
195 contentType, err := ParseContentType(testCase.Input)
196 if testCase.ExpectedOutput == nil {
197 // error expected
198 if err == nil {
199 // but there is no error
200 t.Errorf(`Expecting error for "%s"`, testCase.Input)
201 }
202 } else {
203 // no expected error
204 if err != nil {
205 t.Errorf(`Unexpecting error for "%s" : %s`, testCase.Input, err)
206 } else if !contentType.Equals(*testCase.ExpectedOutput) {
207 // the parsed contentType doesn't matched
208 t.Errorf(`Unexpecting result for "%s", instead got "%s"`, testCase.ExpectedOutput.String(), contentType.String())
209 } else {
210 // ParseContentType is fine, checking String()
211 contentTypeString := contentType.String()
212 expectedString := testCase.Input
213 if testCase.ExpectedString != nil {
214 expectedString = *testCase.ExpectedString
215 }
216 if contentTypeString != expectedString {
217 t.Errorf(`Error with String() output of "%s", got "%s", ContentType{"%s", "%s", "%s", "%s"}`, expectedString, contentTypeString, contentType.TopLevelType, contentType.SubType, contentType.Suffix, contentType.Parameters)
218 }
219 }
220 }
221 }
222}
223
224func FilterToString(m map[string]bool) string {
225 b := new(bytes.Buffer)
226 for key, value := range m {
227 if value {
228 fmt.Fprintf(b, "'%s'=true;", key)
229 } else {
230 fmt.Fprintf(b, "'%s'=false;", key)
231 }
232 }
233 return b.String()
234}
235
236func TestFilters(t *testing.T) {
237 for _, testCase := range filterTestCases {
238 for _, contentType := range testCase.TrueValues {
239 if !testCase.Input(contentType) {
240 t.Errorf(`Filter "%s" must accept the value "%s"`, testCase.Description, contentType)
241 }
242 }
243 for _, contentType := range testCase.FalseValues {
244 if testCase.Input(contentType) {
245 t.Errorf(`Filter "%s" mustn't accept the value "%s"`, testCase.Description, contentType)
246 }
247 }
248 }
249}
250
251func TestFilterParameters(t *testing.T) {
252 for _, testCase := range filterParametersTestCases {
253 // copy Input since the map will be modified
254 InputCopy := make(map[string]string)
255 for k, v := range testCase.Input {
256 InputCopy[k] = v
257 }
258 // apply filter
259 contentType := ContentType{"", "", "", InputCopy}
260 contentType.FilterParameters(testCase.Filter)
261 // test
262 contentTypeOutput := ContentType{"", "", "", testCase.Output}
263 if !contentTypeOutput.Equals(contentType) {
264 t.Errorf(`FilterParameters error : %s becomes %s with this filter %s`, testCase.Input, contentType.Parameters, FilterToString(testCase.Filter))
265 }
266 }
267}
Note: See TracBrowser for help on using the repository browser.