OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library shelf.media_type_test; |
| 6 |
| 7 import 'package:shelf/src/media_type.dart'; |
| 8 import 'package:unittest/unittest.dart'; |
| 9 |
| 10 void main() { |
| 11 group("parse", () { |
| 12 test("parses a simple MIME type", () { |
| 13 var type = new MediaType.parse("text/plain"); |
| 14 expect(type.type, equals("text")); |
| 15 expect(type.subtype, equals("plain")); |
| 16 }); |
| 17 |
| 18 test("allows leading whitespace", () { |
| 19 expect(new MediaType.parse(" text/plain").mimeType, equals("text/plain")); |
| 20 expect(new MediaType.parse("\ttext/plain").mimeType, |
| 21 equals("text/plain")); |
| 22 }); |
| 23 |
| 24 test("allows trailing whitespace", () { |
| 25 expect(new MediaType.parse("text/plain ").mimeType, equals("text/plain")); |
| 26 expect(new MediaType.parse("text/plain\t").mimeType, |
| 27 equals("text/plain")); |
| 28 }); |
| 29 |
| 30 test("disallows separators in the MIME type", () { |
| 31 expect(() => new MediaType.parse("te(xt/plain"), throwsFormatException); |
| 32 expect(() => new MediaType.parse("text/pla=in"), throwsFormatException); |
| 33 }); |
| 34 |
| 35 test("disallows whitespace around the slash", () { |
| 36 expect(() => new MediaType.parse("text /plain"), throwsFormatException); |
| 37 expect(() => new MediaType.parse("text/ plain"), throwsFormatException); |
| 38 }); |
| 39 |
| 40 test("parses parameters", () { |
| 41 var type = new MediaType.parse("text/plain;foo=bar;baz=bang"); |
| 42 expect(type.mimeType, equals("text/plain")); |
| 43 expect(type.parameters, equals({"foo": "bar", "baz": "bang"})); |
| 44 }); |
| 45 |
| 46 test("allows whitespace around the semicolon", () { |
| 47 var type = new MediaType.parse("text/plain ; foo=bar ; baz=bang"); |
| 48 expect(type.mimeType, equals("text/plain")); |
| 49 expect(type.parameters, equals({"foo": "bar", "baz": "bang"})); |
| 50 }); |
| 51 |
| 52 test("disallows whitespace around the equals", () { |
| 53 expect(() => new MediaType.parse("text/plain; foo =bar"), |
| 54 throwsFormatException); |
| 55 expect(() => new MediaType.parse("text/plain; foo= bar"), |
| 56 throwsFormatException); |
| 57 }); |
| 58 |
| 59 test("disallows separators in the parameters", () { |
| 60 expect(() => new MediaType.parse("text/plain; fo:o=bar"), |
| 61 throwsFormatException); |
| 62 expect(() => new MediaType.parse("text/plain; foo=b@ar"), |
| 63 throwsFormatException); |
| 64 }); |
| 65 |
| 66 test("parses quoted parameters", () { |
| 67 var type = new MediaType.parse( |
| 68 'text/plain; foo="bar space"; baz="bang\\\\escape"'); |
| 69 expect(type.mimeType, equals("text/plain")); |
| 70 expect(type.parameters, equals({ |
| 71 "foo": "bar space", |
| 72 "baz": "bang\\escape" |
| 73 })); |
| 74 }); |
| 75 }); |
| 76 |
| 77 group("change", () { |
| 78 var type; |
| 79 setUp(() { |
| 80 type = new MediaType.parse("text/plain; foo=bar; baz=bang"); |
| 81 }); |
| 82 |
| 83 test("uses the existing fields by default", () { |
| 84 var newType = type.change(); |
| 85 expect(newType.type, equals("text")); |
| 86 expect(newType.subtype, equals("plain")); |
| 87 expect(newType.parameters, equals({"foo": "bar", "baz": "bang"})); |
| 88 }); |
| 89 |
| 90 test("[type] overrides the existing type", () { |
| 91 expect(type.change(type: "new").type, equals("new")); |
| 92 }); |
| 93 |
| 94 test("[subtype] overrides the existing subtype", () { |
| 95 expect(type.change(subtype: "new").subtype, equals("new")); |
| 96 }); |
| 97 |
| 98 test("[mimeType] overrides the existing type and subtype", () { |
| 99 var newType = type.change(mimeType: "image/png"); |
| 100 expect(newType.type, equals("image")); |
| 101 expect(newType.subtype, equals("png")); |
| 102 }); |
| 103 |
| 104 test("[parameters] overrides and adds to existing parameters", () { |
| 105 expect(type.change(parameters: { |
| 106 "foo": "zap", |
| 107 "qux": "fblthp" |
| 108 }).parameters, equals({ |
| 109 "foo": "zap", |
| 110 "baz": "bang", |
| 111 "qux": "fblthp" |
| 112 })); |
| 113 }); |
| 114 |
| 115 test("[clearParameters] removes existing parameters", () { |
| 116 expect(type.change(clearParameters: true).parameters, isEmpty); |
| 117 }); |
| 118 |
| 119 test("[clearParameters] with [parameters] removes before adding", () { |
| 120 var newType = type.change( |
| 121 parameters: {"foo": "zap"}, |
| 122 clearParameters: true); |
| 123 expect(newType.parameters, equals({"foo": "zap"})); |
| 124 }); |
| 125 |
| 126 test("[type] with [mimeType] is illegal", () { |
| 127 expect(() => type.change(type: "new", mimeType: "image/png"), |
| 128 throwsArgumentError); |
| 129 }); |
| 130 |
| 131 test("[subtype] with [mimeType] is illegal", () { |
| 132 expect(() => type.change(subtype: "new", mimeType: "image/png"), |
| 133 throwsArgumentError); |
| 134 }); |
| 135 }); |
| 136 |
| 137 group("toString", () { |
| 138 test("serializes a simple MIME type", () { |
| 139 expect(new MediaType("text", "plain").toString(), equals("text/plain")); |
| 140 }); |
| 141 |
| 142 test("serializes a token parameter as a token", () { |
| 143 expect(new MediaType("text", "plain", {"foo": "bar"}).toString(), |
| 144 equals("text/plain; foo=bar")); |
| 145 }); |
| 146 |
| 147 test("serializes a non-token parameter as a quoted string", () { |
| 148 expect(new MediaType("text", "plain", {"foo": "bar baz"}).toString(), |
| 149 equals('text/plain; foo="bar baz"')); |
| 150 }); |
| 151 |
| 152 test("escapes a quoted string as necessary", () { |
| 153 expect(new MediaType("text", "plain", {"foo": 'bar"\x7Fbaz'}).toString(), |
| 154 equals('text/plain; foo="bar\\"\\\x7Fbaz"')); |
| 155 }); |
| 156 |
| 157 test("serializes multiple parameters", () { |
| 158 expect(new MediaType("text", "plain", { |
| 159 "foo": "bar", "baz": "bang" |
| 160 }).toString(), equals("text/plain; foo=bar; baz=bang")); |
| 161 }); |
| 162 }); |
| 163 } |
OLD | NEW |