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'; |
| 11 import 'package:unittest/unittest.dart'; |
| 12 |
| 13 void main() { |
| 14 group("readAsString", () { |
| 15 test("supports a null body", () { |
| 16 var response = new Response(200); |
| 17 expect(response.readAsString(), completion(equals(""))); |
| 18 }); |
| 19 |
| 20 test("supports a String body", () { |
| 21 var response = new Response.ok("hello, world"); |
| 22 expect(response.readAsString(), completion(equals("hello, world"))); |
| 23 }); |
| 24 |
| 25 test("supports a Stream<List<int>> body", () { |
| 26 var controller = new StreamController(); |
| 27 var response = new Response.ok(controller.stream); |
| 28 expect(response.readAsString(), completion(equals("hello, world"))); |
| 29 |
| 30 controller.add([104, 101, 108, 108, 111, 44]); |
| 31 return new Future(() { |
| 32 controller |
| 33 ..add([32, 119, 111, 114, 108, 100]) |
| 34 ..close(); |
| 35 }); |
| 36 }); |
| 37 |
| 38 test("defaults to UTF-8", () { |
| 39 var response = new Response.ok( |
| 40 new Stream.fromIterable([[195, 168]])); |
| 41 expect(response.readAsString(), completion(equals("è"))); |
| 42 }); |
| 43 |
| 44 test("the content-type header overrides the default", () { |
| 45 var response = new Response.ok( |
| 46 new Stream.fromIterable([[195, 168]]), |
| 47 headers: {'content-type': 'text/plain; charset=iso-8859-1'}); |
| 48 expect(response.readAsString(), completion(equals("è"))); |
| 49 }); |
| 50 |
| 51 test("an explicit encoding overrides the content-type header", () { |
| 52 var response = new Response.ok( |
| 53 new Stream.fromIterable([[195, 168]]), |
| 54 headers: {'content-type': 'text/plain; charset=utf-8'}); |
| 55 expect(response.readAsString(LATIN1), completion(equals("è"))); |
| 56 }); |
| 57 }); |
| 58 |
| 59 group("read", () { |
| 60 test("supports a null body", () { |
| 61 var response = new Response(200); |
| 62 expect(response.read().toList(), completion(isEmpty)); |
| 63 }); |
| 64 |
| 65 test("supports a String body", () { |
| 66 var response = new Response.ok("hello, world"); |
| 67 expect(response.read().toList(), completion(equals([[ |
| 68 104, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100 |
| 69 ]]))); |
| 70 }); |
| 71 |
| 72 test("supports a Stream<List<int>> body", () { |
| 73 var controller = new StreamController(); |
| 74 var response = new Response.ok(controller.stream); |
| 75 expect(response.read().toList(), completion(equals([ |
| 76 [104, 101, 108, 108, 111, 44], |
| 77 [32, 119, 111, 114, 108, 100] |
| 78 ]))); |
| 79 |
| 80 controller.add([104, 101, 108, 108, 111, 44]); |
| 81 return new Future(() { |
| 82 controller |
| 83 ..add([32, 119, 111, 114, 108, 100]) |
| 84 ..close(); |
| 85 }); |
| 86 }); |
| 87 }); |
| 88 |
| 89 group("new Response", () { |
| 90 test("defaults to encoding a String as UTF-8", () { |
| 91 expect(new Response.ok("è").read().toList(), |
| 92 completion(equals([[195, 168]]))); |
| 93 }); |
| 94 |
| 95 test("uses the explicit encoding if available", () { |
| 96 expect(new Response.ok("è", encoding: LATIN1).read().toList(), |
| 97 completion(equals([[232]]))); |
| 98 }); |
| 99 |
| 100 test("adds an explicit encoding to the content-type", () { |
| 101 var response = new Response.ok("è", |
| 102 encoding: LATIN1, |
| 103 headers: {'content-type': 'text/plain'}); |
| 104 expect(response.headers, |
| 105 containsPair('content-type', 'text/plain; charset=iso-8859-1')); |
| 106 }); |
| 107 |
| 108 test("sets an absent content-type to application/octet-stream in order to " |
| 109 "set the charset", () { |
| 110 var response = new Response.ok("è", encoding: LATIN1); |
| 111 expect(response.headers, containsPair('content-type', |
| 112 'application/octet-stream; charset=iso-8859-1')); |
| 113 }); |
| 114 |
| 115 test("overwrites an existing charset if given an explicit encoding", () { |
| 116 var response = new Response.ok("è", |
| 117 encoding: LATIN1, |
| 118 headers: {'content-type': 'text/plain; charset=whatever'}); |
| 119 expect(response.headers, |
| 120 containsPair('content-type', 'text/plain; charset=iso-8859-1')); |
| 121 }); |
| 122 }); |
| 123 |
| 124 group("new Response.internalServerError without a body", () { |
| 125 test('sets the body to "Internal Server Error"', () { |
| 126 var response = new Response.internalServerError(); |
| 127 expect(response.readAsString(), |
| 128 completion(equals("Internal Server Error"))); |
| 129 }); |
| 130 |
| 131 test('sets the content-type header to text/plain', () { |
| 132 var response = new Response.internalServerError(); |
| 133 expect(response.headers, containsPair('content-type', 'text/plain')); |
| 134 }); |
| 135 |
| 136 test('preserves content-type parameters', () { |
| 137 var response = new Response.internalServerError(headers: { |
| 138 'content-type': 'application/octet-stream; param=whatever' |
| 139 }); |
| 140 expect(response.headers, |
| 141 containsPair('content-type', 'text/plain; param=whatever')); |
| 142 }); |
| 143 }); |
| 144 |
| 145 group("Response redirect", () { |
| 146 test("sets the location header for a String", () { |
| 147 var response = new Response.found('/foo'); |
| 148 expect(response.headers, containsPair('location', '/foo')); |
| 149 }); |
| 150 |
| 151 test("sets the location header for a Uri", () { |
| 152 var response = new Response.found(new Uri(path: '/foo')); |
| 153 expect(response.headers, containsPair('location', '/foo')); |
| 154 }); |
| 155 }); |
| 156 |
| 157 group("mimeType", () { |
| 158 test("is null without a content-type header", () { |
| 159 expect(new Response.ok("okay!").mimeType, isNull); |
| 160 }); |
| 161 |
| 162 test("comes from the content-type header", () { |
| 163 expect(new Response.ok("okay!", headers: { |
| 164 'content-type': 'text/plain' |
| 165 }).mimeType, equals('text/plain')); |
| 166 }); |
| 167 |
| 168 test("doesn't include parameters", () { |
| 169 expect(new Response.ok("okay!", headers: { |
| 170 'content-type': 'text/plain; foo=bar; bar=baz' |
| 171 }).mimeType, equals('text/plain')); |
| 172 }); |
| 173 }); |
| 174 |
| 175 group("encoding", () { |
| 176 test("is null without a content-type header", () { |
| 177 expect(new Response.ok("okay!").encoding, isNull); |
| 178 }); |
| 179 |
| 180 test("is null without a charset parameter", () { |
| 181 expect(new Response.ok("okay!", headers: { |
| 182 'content-type': 'text/plain' |
| 183 }).encoding, isNull); |
| 184 }); |
| 185 |
| 186 test("is null with an unrecognized charset parameter", () { |
| 187 expect(new Response.ok("okay!", headers: { |
| 188 'content-type': 'text/plain; charset=fblthp' |
| 189 }).encoding, isNull); |
| 190 }); |
| 191 |
| 192 test("comes from the content-type charset parameter", () { |
| 193 expect(new Response.ok("okay!", headers: { |
| 194 'content-type': 'text/plain; charset=iso-8859-1' |
| 195 }).encoding, equals(LATIN1)); |
| 196 }); |
| 197 }); |
| 198 |
| 199 group("expires", () { |
| 200 test("is null without an Expires header", () { |
| 201 expect(new Response.ok("okay!").expires, isNull); |
| 202 }); |
| 203 |
| 204 test("comes from the Expires header", () { |
| 205 expect(new Response.ok("okay!", headers: { |
| 206 'expires': 'Sun, 06 Nov 1994 08:49:37 GMT' |
| 207 }).expires, equals(DateTime.parse("1994-11-06 08:49:37z"))); |
| 208 }); |
| 209 }); |
| 210 |
| 211 group("lastModified", () { |
| 212 test("is null without a Last-Modified header", () { |
| 213 expect(new Response.ok("okay!").lastModified, isNull); |
| 214 }); |
| 215 |
| 216 test("comes from the Last-Modified header", () { |
| 217 expect(new Response.ok("okay!", headers: { |
| 218 'last-modified': 'Sun, 06 Nov 1994 08:49:37 GMT' |
| 219 }).lastModified, equals(DateTime.parse("1994-11-06 08:49:37z"))); |
| 220 }); |
| 221 }); |
| 222 |
| 223 group("contentLength", () { |
| 224 test("is null without a content-length header", () { |
| 225 expect(new Response.ok("okay!").contentLength, isNull); |
| 226 }); |
| 227 |
| 228 test("comes from the content-length header", () { |
| 229 expect(new Response.ok("okay!", headers: { |
| 230 'content-length': '42' |
| 231 }).contentLength, equals(42)); |
| 232 }); |
| 233 }); |
| 234 } |
OLD | NEW |