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.response_test; | 5 library shelf.response_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/shelf.dart' hide Request; | 10 import 'package:shelf/shelf.dart' hide Request; |
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 void main() { | 15 void main() { |
16 group("supports a String body", () { | 16 group("supports a String body", () { |
17 test("readAsString", () { | 17 test("readAsString", () { |
18 var response = new Response.ok("hello, world"); | 18 var response = new Response.ok("hello, world"); |
19 expect(response.readAsString(), completion(equals("hello, world"))); | 19 expect(response.readAsString(), completion(equals("hello, world"))); |
20 }); | 20 }); |
21 | 21 |
22 test("read", () { | 22 test("read", () { |
23 var helloWorldBytes = new List.from(HELLO_BYTES)..addAll(WORLD_BYTES); | 23 var helloWorldBytes = new List.from(HELLO_BYTES)..addAll(WORLD_BYTES); |
24 | 24 |
25 var response = new Response.ok("hello, world"); | 25 var response = new Response.ok("hello, world"); |
26 expect(response.read().toList(), completion(equals([helloWorldBytes]))); | 26 expect(response.read().toList(), completion(equals([helloWorldBytes]))); |
27 }); | 27 }); |
28 }); | 28 }); |
29 | 29 |
30 group("new Response", () { | |
31 test("defaults to encoding a String as UTF-8", () { | |
32 expect(new Response.ok("è").read().toList(), | |
33 completion(equals([[195, 168]]))); | |
34 }); | |
35 | |
36 test("uses the explicit encoding if available", () { | |
37 expect(new Response.ok("è", encoding: LATIN1).read().toList(), | |
38 completion(equals([[232]]))); | |
39 }); | |
40 | |
41 test("adds an explicit encoding to the content-type", () { | |
42 var response = new Response.ok("è", | |
43 encoding: LATIN1, headers: {'content-type': 'text/plain'}); | |
44 expect(response.headers, | |
45 containsPair('content-type', 'text/plain; charset=iso-8859-1')); | |
46 }); | |
47 | |
48 test("sets an absent content-type to application/octet-stream in order to " | |
49 "set the charset", () { | |
50 var response = new Response.ok("è", encoding: LATIN1); | |
51 expect(response.headers, containsPair( | |
52 'content-type', 'application/octet-stream; charset=iso-8859-1')); | |
53 }); | |
54 | |
55 test("overwrites an existing charset if given an explicit encoding", () { | |
56 var response = new Response.ok("è", | |
57 encoding: LATIN1, | |
58 headers: {'content-type': 'text/plain; charset=whatever'}); | |
59 expect(response.headers, | |
60 containsPair('content-type', 'text/plain; charset=iso-8859-1')); | |
61 }); | |
62 }); | |
63 | |
64 group("new Response.internalServerError without a body", () { | 30 group("new Response.internalServerError without a body", () { |
65 test('sets the body to "Internal Server Error"', () { | 31 test('sets the body to "Internal Server Error"', () { |
66 var response = new Response.internalServerError(); | 32 var response = new Response.internalServerError(); |
67 expect( | 33 expect( |
68 response.readAsString(), completion(equals("Internal Server Error"))); | 34 response.readAsString(), completion(equals("Internal Server Error"))); |
69 }); | 35 }); |
70 | 36 |
71 test('sets the content-type header to text/plain', () { | 37 test('sets the content-type header to text/plain', () { |
72 var response = new Response.internalServerError(); | 38 var response = new Response.internalServerError(); |
73 expect(response.headers, containsPair('content-type', 'text/plain')); | 39 expect(response.headers, containsPair('content-type', 'text/plain')); |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 | 107 |
142 controller.add(HELLO_BYTES); | 108 controller.add(HELLO_BYTES); |
143 return new Future(() { | 109 return new Future(() { |
144 controller | 110 controller |
145 ..add(WORLD_BYTES) | 111 ..add(WORLD_BYTES) |
146 ..close(); | 112 ..close(); |
147 }); | 113 }); |
148 }); | 114 }); |
149 }); | 115 }); |
150 } | 116 } |
OLD | NEW |