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 shelf.message_test; | 5 library shelf.message_test; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:convert'; | 8 import 'dart:convert'; |
9 | 9 |
10 import 'package:shelf/src/message.dart'; | 10 import 'package:shelf/src/message.dart'; |
11 import 'package:unittest/unittest.dart'; | 11 import 'package:unittest/unittest.dart'; |
12 | 12 |
13 import 'test_util.dart'; | 13 import 'test_util.dart'; |
14 | 14 |
15 class _TestMessage extends Message { | 15 class _TestMessage extends Message { |
16 _TestMessage(Map<String, String> headers, Map<String, Object> context, | 16 _TestMessage(Map<String, String> headers, Map<String, Object> context, body, |
17 Stream<List<int>> body) : super(body, headers: headers, context: context); | 17 Encoding encoding) |
| 18 : super(body, headers: headers, context: context, encoding: encoding); |
18 | 19 |
19 Message change({Map<String, String> headers, Map<String, Object> context}) { | 20 Message change({Map<String, String> headers, Map<String, Object> context}) { |
20 throw new UnimplementedError(); | 21 throw new UnimplementedError(); |
21 } | 22 } |
22 } | 23 } |
23 | 24 |
24 Message _createMessage({Map<String, String> headers, | 25 Message _createMessage({Map<String, String> headers, |
25 Map<String, Object> context, Stream<List<int>> body}) { | 26 Map<String, Object> context, body, Encoding encoding}) { |
26 if (body == null) body = new Stream.fromIterable([]); | 27 return new _TestMessage(headers, context, body, encoding); |
27 return new _TestMessage(headers, context, body); | |
28 } | 28 } |
29 | 29 |
30 void main() { | 30 void main() { |
31 group('headers', () { | 31 group('headers', () { |
32 test('message headers are case insensitive', () { | 32 test('message headers are case insensitive', () { |
33 var message = _createMessage(headers: {'foo': 'bar'}); | 33 var message = _createMessage(headers: {'foo': 'bar'}); |
34 | 34 |
35 expect(message.headers, containsPair('foo', 'bar')); | 35 expect(message.headers, containsPair('foo', 'bar')); |
36 expect(message.headers, containsPair('Foo', 'bar')); | 36 expect(message.headers, containsPair('Foo', 'bar')); |
37 expect(message.headers, containsPair('FOO', 'bar')); | 37 expect(message.headers, containsPair('FOO', 'bar')); |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 headers: {'content-type': 'text/plain; charset=fblthp'}).encoding, | 195 headers: {'content-type': 'text/plain; charset=fblthp'}).encoding, |
196 isNull); | 196 isNull); |
197 }); | 197 }); |
198 | 198 |
199 test("comes from the content-type charset parameter", () { | 199 test("comes from the content-type charset parameter", () { |
200 expect(_createMessage( | 200 expect(_createMessage( |
201 headers: { | 201 headers: { |
202 'content-type': 'text/plain; charset=iso-8859-1' | 202 'content-type': 'text/plain; charset=iso-8859-1' |
203 }).encoding, equals(LATIN1)); | 203 }).encoding, equals(LATIN1)); |
204 }); | 204 }); |
| 205 |
| 206 test("defaults to encoding a String as UTF-8", () { |
| 207 expect(_createMessage(body: "è").read().toList(), |
| 208 completion(equals([[195, 168]]))); |
| 209 }); |
| 210 |
| 211 test("uses the explicit encoding if available", () { |
| 212 expect(_createMessage(body: "è", encoding: LATIN1).read().toList(), |
| 213 completion(equals([[232]]))); |
| 214 }); |
| 215 |
| 216 test("adds an explicit encoding to the content-type", () { |
| 217 var request = _createMessage( |
| 218 body: "è", encoding: LATIN1, headers: {'content-type': 'text/plain'}); |
| 219 expect(request.headers, |
| 220 containsPair('content-type', 'text/plain; charset=iso-8859-1')); |
| 221 }); |
| 222 |
| 223 test("sets an absent content-type to application/octet-stream in order to " |
| 224 "set the charset", () { |
| 225 var request = _createMessage(body: "è", encoding: LATIN1); |
| 226 expect(request.headers, containsPair( |
| 227 'content-type', 'application/octet-stream; charset=iso-8859-1')); |
| 228 }); |
| 229 |
| 230 test("overwrites an existing charset if given an explicit encoding", () { |
| 231 var request = _createMessage( |
| 232 body: "è", |
| 233 encoding: LATIN1, |
| 234 headers: {'content-type': 'text/plain; charset=whatever'}); |
| 235 expect(request.headers, |
| 236 containsPair('content-type', 'text/plain; charset=iso-8859-1')); |
| 237 }); |
205 }); | 238 }); |
206 } | 239 } |
OLD | NEW |