OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library http_parser.media_type_test; | 5 library http_parser.media_type_test; |
6 | 6 |
7 import 'package:http_parser/http_parser.dart'; | 7 import 'package:http_parser/http_parser.dart'; |
8 import 'package:unittest/unittest.dart'; | 8 import 'package:unittest/unittest.dart'; |
9 | 9 |
10 void main() { | 10 void main() { |
11 group("parse", () { | 11 group("parse", () { |
12 test("parses a simple MIME type", () { | 12 test("parses a simple MIME type", () { |
13 var type = new MediaType.parse("text/plain"); | 13 var type = new MediaType.parse("text/plain"); |
14 expect(type.type, equals("text")); | 14 expect(type.type, equals("text")); |
15 expect(type.subtype, equals("plain")); | 15 expect(type.subtype, equals("plain")); |
16 }); | 16 }); |
17 | 17 |
18 test("allows leading whitespace", () { | 18 test("allows leading whitespace", () { |
19 expect(new MediaType.parse(" text/plain").mimeType, equals("text/plain")); | 19 expect(new MediaType.parse(" text/plain").mimeType, equals("text/plain")); |
20 expect(new MediaType.parse("\ttext/plain").mimeType, | 20 expect( |
21 equals("text/plain")); | 21 new MediaType.parse("\ttext/plain").mimeType, equals("text/plain")); |
22 }); | 22 }); |
23 | 23 |
24 test("allows trailing whitespace", () { | 24 test("allows trailing whitespace", () { |
25 expect(new MediaType.parse("text/plain ").mimeType, equals("text/plain")); | 25 expect(new MediaType.parse("text/plain ").mimeType, equals("text/plain")); |
26 expect(new MediaType.parse("text/plain\t").mimeType, | 26 expect( |
27 equals("text/plain")); | 27 new MediaType.parse("text/plain\t").mimeType, equals("text/plain")); |
28 }); | 28 }); |
29 | 29 |
30 test("disallows separators in the MIME type", () { | 30 test("disallows separators in the MIME type", () { |
31 expect(() => new MediaType.parse("te(xt/plain"), throwsFormatException); | 31 expect(() => new MediaType.parse("te(xt/plain"), throwsFormatException); |
32 expect(() => new MediaType.parse("text/pla=in"), throwsFormatException); | 32 expect(() => new MediaType.parse("text/pla=in"), throwsFormatException); |
33 }); | 33 }); |
34 | 34 |
35 test("disallows whitespace around the slash", () { | 35 test("disallows whitespace around the slash", () { |
36 expect(() => new MediaType.parse("text /plain"), throwsFormatException); | 36 expect(() => new MediaType.parse("text /plain"), throwsFormatException); |
37 expect(() => new MediaType.parse("text/ plain"), throwsFormatException); | 37 expect(() => new MediaType.parse("text/ plain"), throwsFormatException); |
(...skipping 22 matching lines...) Expand all Loading... |
60 expect(() => new MediaType.parse("text/plain; fo:o=bar"), | 60 expect(() => new MediaType.parse("text/plain; fo:o=bar"), |
61 throwsFormatException); | 61 throwsFormatException); |
62 expect(() => new MediaType.parse("text/plain; foo=b@ar"), | 62 expect(() => new MediaType.parse("text/plain; foo=b@ar"), |
63 throwsFormatException); | 63 throwsFormatException); |
64 }); | 64 }); |
65 | 65 |
66 test("parses quoted parameters", () { | 66 test("parses quoted parameters", () { |
67 var type = new MediaType.parse( | 67 var type = new MediaType.parse( |
68 'text/plain; foo="bar space"; baz="bang\\\\escape"'); | 68 'text/plain; foo="bar space"; baz="bang\\\\escape"'); |
69 expect(type.mimeType, equals("text/plain")); | 69 expect(type.mimeType, equals("text/plain")); |
70 expect(type.parameters, equals({ | 70 expect( |
71 "foo": "bar space", | 71 type.parameters, equals({"foo": "bar space", "baz": "bang\\escape"})); |
72 "baz": "bang\\escape" | |
73 })); | |
74 }); | 72 }); |
75 }); | 73 }); |
76 | 74 |
77 group("change", () { | 75 group("change", () { |
78 var type; | 76 var type; |
79 setUp(() { | 77 setUp(() { |
80 type = new MediaType.parse("text/plain; foo=bar; baz=bang"); | 78 type = new MediaType.parse("text/plain; foo=bar; baz=bang"); |
81 }); | 79 }); |
82 | 80 |
83 test("uses the existing fields by default", () { | 81 test("uses the existing fields by default", () { |
(...skipping 11 matching lines...) Expand all Loading... |
95 expect(type.change(subtype: "new").subtype, equals("new")); | 93 expect(type.change(subtype: "new").subtype, equals("new")); |
96 }); | 94 }); |
97 | 95 |
98 test("[mimeType] overrides the existing type and subtype", () { | 96 test("[mimeType] overrides the existing type and subtype", () { |
99 var newType = type.change(mimeType: "image/png"); | 97 var newType = type.change(mimeType: "image/png"); |
100 expect(newType.type, equals("image")); | 98 expect(newType.type, equals("image")); |
101 expect(newType.subtype, equals("png")); | 99 expect(newType.subtype, equals("png")); |
102 }); | 100 }); |
103 | 101 |
104 test("[parameters] overrides and adds to existing parameters", () { | 102 test("[parameters] overrides and adds to existing parameters", () { |
105 expect(type.change(parameters: { | 103 expect( |
106 "foo": "zap", | 104 type.change(parameters: {"foo": "zap", "qux": "fblthp"}).parameters, |
107 "qux": "fblthp" | 105 equals({"foo": "zap", "baz": "bang", "qux": "fblthp"})); |
108 }).parameters, equals({ | |
109 "foo": "zap", | |
110 "baz": "bang", | |
111 "qux": "fblthp" | |
112 })); | |
113 }); | 106 }); |
114 | 107 |
115 test("[clearParameters] removes existing parameters", () { | 108 test("[clearParameters] removes existing parameters", () { |
116 expect(type.change(clearParameters: true).parameters, isEmpty); | 109 expect(type.change(clearParameters: true).parameters, isEmpty); |
117 }); | 110 }); |
118 | 111 |
119 test("[clearParameters] with [parameters] removes before adding", () { | 112 test("[clearParameters] with [parameters] removes before adding", () { |
120 var newType = type.change( | 113 var newType = |
121 parameters: {"foo": "zap"}, | 114 type.change(parameters: {"foo": "zap"}, clearParameters: true); |
122 clearParameters: true); | |
123 expect(newType.parameters, equals({"foo": "zap"})); | 115 expect(newType.parameters, equals({"foo": "zap"})); |
124 }); | 116 }); |
125 | 117 |
126 test("[type] with [mimeType] is illegal", () { | 118 test("[type] with [mimeType] is illegal", () { |
127 expect(() => type.change(type: "new", mimeType: "image/png"), | 119 expect(() => type.change(type: "new", mimeType: "image/png"), |
128 throwsArgumentError); | 120 throwsArgumentError); |
129 }); | 121 }); |
130 | 122 |
131 test("[subtype] with [mimeType] is illegal", () { | 123 test("[subtype] with [mimeType] is illegal", () { |
132 expect(() => type.change(subtype: "new", mimeType: "image/png"), | 124 expect(() => type.change(subtype: "new", mimeType: "image/png"), |
(...skipping 15 matching lines...) Expand all Loading... |
148 expect(new MediaType("text", "plain", {"foo": "bar baz"}).toString(), | 140 expect(new MediaType("text", "plain", {"foo": "bar baz"}).toString(), |
149 equals('text/plain; foo="bar baz"')); | 141 equals('text/plain; foo="bar baz"')); |
150 }); | 142 }); |
151 | 143 |
152 test("escapes a quoted string as necessary", () { | 144 test("escapes a quoted string as necessary", () { |
153 expect(new MediaType("text", "plain", {"foo": 'bar"\x7Fbaz'}).toString(), | 145 expect(new MediaType("text", "plain", {"foo": 'bar"\x7Fbaz'}).toString(), |
154 equals('text/plain; foo="bar\\"\\\x7Fbaz"')); | 146 equals('text/plain; foo="bar\\"\\\x7Fbaz"')); |
155 }); | 147 }); |
156 | 148 |
157 test("serializes multiple parameters", () { | 149 test("serializes multiple parameters", () { |
158 expect(new MediaType("text", "plain", { | 150 expect(new MediaType("text", "plain", {"foo": "bar", "baz": "bang"}) |
159 "foo": "bar", "baz": "bang" | 151 .toString(), equals("text/plain; foo=bar; baz=bang")); |
160 }).toString(), equals("text/plain; foo=bar; baz=bang")); | |
161 }); | 152 }); |
162 }); | 153 }); |
163 } | 154 } |
OLD | NEW |