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.response_test; | |
6 | |
7 import 'dart:async'; | |
8 import 'dart:convert'; | |
9 | |
10 import 'package:shelf/shelf.dart' hide Request; | |
11 import 'package:unittest/unittest.dart'; | |
12 | |
13 import 'test_util.dart'; | |
14 | |
15 void main() { | |
16 group("supports a String body", () { | |
17 test("readAsString", () { | |
18 var response = new Response.ok("hello, world"); | |
19 expect(response.readAsString(), completion(equals("hello, world"))); | |
20 }); | |
21 | |
22 test("read", () { | |
23 var helloWorldBytes = new List.from(HELLO_BYTES)..addAll(WORLD_BYTES); | |
24 | |
25 var response = new Response.ok("hello, world"); | |
26 expect(response.read().toList(), completion(equals([helloWorldBytes]))); | |
27 }); | |
28 }); | |
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, | |
44 headers: {'content-type': 'text/plain'}); | |
45 expect(response.headers, | |
46 containsPair('content-type', 'text/plain; charset=iso-8859-1')); | |
47 }); | |
48 | |
49 test("sets an absent content-type to application/octet-stream in order to " | |
50 "set the charset", () { | |
51 var response = new Response.ok("è", encoding: LATIN1); | |
52 expect(response.headers, containsPair('content-type', | |
53 'application/octet-stream; charset=iso-8859-1')); | |
54 }); | |
55 | |
56 test("overwrites an existing charset if given an explicit encoding", () { | |
57 var response = new Response.ok("è", | |
58 encoding: LATIN1, | |
59 headers: {'content-type': 'text/plain; charset=whatever'}); | |
60 expect(response.headers, | |
61 containsPair('content-type', 'text/plain; charset=iso-8859-1')); | |
62 }); | |
63 }); | |
64 | |
65 group("new Response.internalServerError without a body", () { | |
66 test('sets the body to "Internal Server Error"', () { | |
67 var response = new Response.internalServerError(); | |
68 expect(response.readAsString(), | |
69 completion(equals("Internal Server Error"))); | |
70 }); | |
71 | |
72 test('sets the content-type header to text/plain', () { | |
73 var response = new Response.internalServerError(); | |
74 expect(response.headers, containsPair('content-type', 'text/plain')); | |
75 }); | |
76 | |
77 test('preserves content-type parameters', () { | |
78 var response = new Response.internalServerError(headers: { | |
79 'content-type': 'application/octet-stream; param=whatever' | |
80 }); | |
81 expect(response.headers, | |
82 containsPair('content-type', 'text/plain; param=whatever')); | |
83 }); | |
84 }); | |
85 | |
86 group("Response redirect", () { | |
87 test("sets the location header for a String", () { | |
88 var response = new Response.found('/foo'); | |
89 expect(response.headers, containsPair('location', '/foo')); | |
90 }); | |
91 | |
92 test("sets the location header for a Uri", () { | |
93 var response = new Response.found(new Uri(path: '/foo')); | |
94 expect(response.headers, containsPair('location', '/foo')); | |
95 }); | |
96 }); | |
97 | |
98 group("expires", () { | |
99 test("is null without an Expires header", () { | |
100 expect(new Response.ok("okay!").expires, isNull); | |
101 }); | |
102 | |
103 test("comes from the Expires header", () { | |
104 expect(new Response.ok("okay!", headers: { | |
105 'expires': 'Sun, 06 Nov 1994 08:49:37 GMT' | |
106 }).expires, equals(DateTime.parse("1994-11-06 08:49:37z"))); | |
107 }); | |
108 }); | |
109 | |
110 group("lastModified", () { | |
111 test("is null without a Last-Modified header", () { | |
112 expect(new Response.ok("okay!").lastModified, isNull); | |
113 }); | |
114 | |
115 test("comes from the Last-Modified header", () { | |
116 expect(new Response.ok("okay!", headers: { | |
117 'last-modified': 'Sun, 06 Nov 1994 08:49:37 GMT' | |
118 }).lastModified, equals(DateTime.parse("1994-11-06 08:49:37z"))); | |
119 }); | |
120 }); | |
121 | |
122 group('change', () { | |
123 test('with no arguments returns instance with equal values', () { | |
124 var controller = new StreamController(); | |
125 | |
126 var request = new Response(345, body: 'hèllo, world', encoding: LATIN1, | |
127 headers: {'header1': 'header value 1'}, | |
128 context: {'context1': 'context value 1'}); | |
129 | |
130 var copy = request.change(); | |
131 | |
132 expect(copy.statusCode, request.statusCode); | |
133 expect(copy.readAsString(), completion('hèllo, world')); | |
134 expect(copy.headers, same(request.headers)); | |
135 expect(copy.encoding, request.encoding); | |
136 expect(copy.context, same(request.context)); | |
137 | |
138 controller.add(HELLO_BYTES); | |
139 return new Future(() { | |
140 controller | |
141 ..add(WORLD_BYTES) | |
142 ..close(); | |
143 }); | |
144 }); | |
145 }); | |
146 } | |
OLD | NEW |