Chromium Code Reviews| 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.request_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/shelf.dart'; | 10 import 'package:shelf/src/message.dart'; |
| 11 import 'package:unittest/unittest.dart'; | 11 import 'package:unittest/unittest.dart'; |
| 12 | 12 |
| 13 Request _request([Map<String, String> headers, Stream<List<int>> body]) { | 13 Message _createMessage({Map<String, String> headers, Stream<List<int>> body}) { |
| 14 if (body == null) body = new Stream.fromIterable([]); | |
| 14 if (headers == null) headers = {}; | 15 if (headers == null) headers = {}; |
| 15 return new Request("/", "", "GET", "", "1.1", Uri.parse('http://localhost/'), | 16 return new Message(headers, body); |
|
nweiz
2014/04/08 20:23:09
It seems like Message should be an abstract class,
kevmoo
2014/04/08 21:41:13
Done.
| |
| 16 headers, body: body); | |
| 17 } | 17 } |
| 18 | 18 |
| 19 void main() { | 19 void main() { |
| 20 group("contentLength", () { | 20 test('message headers are case insensitive', () { |
| 21 test("is null without a content-length header", () { | 21 var message = _createMessage(headers: { 'foo': 'bar' }); |
|
nweiz
2014/04/08 20:23:09
Nit: "{'foo': 'bar'}"
kevmoo
2014/04/08 21:41:13
Done.
| |
| 22 var request = _request(); | |
| 23 expect(request.contentLength, isNull); | |
| 24 }); | |
| 25 | 22 |
| 26 test("comes from the content-length header", () { | 23 expect(message.headers, containsPair('foo', 'bar')); |
| 27 var request = _request({ | 24 expect(message.headers, containsPair('Foo', 'bar')); |
| 28 'content-length': '42' | 25 expect(message.headers, containsPair('FOO', 'bar')); |
| 29 }); | |
| 30 expect(request.contentLength, 42); | |
| 31 }); | |
| 32 }); | |
| 33 | |
| 34 group("ifModifiedSince", () { | |
| 35 test("is null without an If-Modified-Since header", () { | |
| 36 var request = _request(); | |
| 37 expect(request.ifModifiedSince, isNull); | |
| 38 }); | |
| 39 | |
| 40 test("comes from the Last-Modified header", () { | |
| 41 var request = _request({ | |
| 42 'if-modified-since': 'Sun, 06 Nov 1994 08:49:37 GMT' | |
| 43 }); | |
| 44 expect(request.ifModifiedSince, | |
| 45 equals(DateTime.parse("1994-11-06 08:49:37z"))); | |
| 46 }); | |
| 47 }); | 26 }); |
| 48 | 27 |
| 49 group("readAsString", () { | 28 group("readAsString", () { |
| 50 test("supports a null body", () { | 29 test("supports a null body", () { |
| 51 var request = _request(); | 30 var request = _createMessage(); |
| 52 expect(request.readAsString(), completion(equals(""))); | 31 expect(request.readAsString(), completion(equals(""))); |
| 53 }); | 32 }); |
| 54 | 33 |
| 55 test("supports a Stream<List<int>> body", () { | 34 test("supports a Stream<List<int>> body", () { |
| 56 var controller = new StreamController(); | 35 var controller = new StreamController(); |
| 57 var request = _request({}, controller.stream); | 36 var request = _createMessage(body: controller.stream); |
| 58 expect(request.readAsString(), completion(equals("hello, world"))); | 37 expect(request.readAsString(), completion(equals("hello, world"))); |
| 59 | 38 |
| 60 controller.add([104, 101, 108, 108, 111, 44]); | 39 controller.add([104, 101, 108, 108, 111, 44]); |
| 61 return new Future(() { | 40 return new Future(() { |
| 62 controller | 41 controller |
| 63 ..add([32, 119, 111, 114, 108, 100]) | 42 ..add([32, 119, 111, 114, 108, 100]) |
| 64 ..close(); | 43 ..close(); |
| 65 }); | 44 }); |
| 66 }); | 45 }); |
| 67 | 46 |
| 68 test("defaults to UTF-8", () { | 47 test("defaults to UTF-8", () { |
| 69 var request = _request({}, new Stream.fromIterable([[195, 168]])); | 48 var request = _createMessage(body: new Stream.fromIterable([[195, 168]])); |
| 70 expect(request.readAsString(), completion(equals("è"))); | 49 expect(request.readAsString(), completion(equals("è"))); |
| 71 }); | 50 }); |
| 72 | 51 |
| 73 test("the content-type header overrides the default", () { | 52 test("the content-type header overrides the default", () { |
| 74 var request = _request({'content-type': 'text/plain; charset=iso-8859-1'}, | 53 var request = _createMessage( |
| 75 new Stream.fromIterable([[195, 168]])); | 54 headers: {'content-type': 'text/plain; charset=iso-8859-1'}, |
| 55 body: new Stream.fromIterable([[195, 168]])); | |
| 76 expect(request.readAsString(), completion(equals("è"))); | 56 expect(request.readAsString(), completion(equals("è"))); |
| 77 }); | 57 }); |
| 78 | 58 |
| 79 test("an explicit encoding overrides the content-type header", () { | 59 test("an explicit encoding overrides the content-type header", () { |
| 80 var request = _request({'content-type': 'text/plain; charset=iso-8859-1'}, | 60 var request = _createMessage( |
| 81 new Stream.fromIterable([[195, 168]])); | 61 headers: {'content-type': 'text/plain; charset=iso-8859-1'}, |
| 62 body: new Stream.fromIterable([[195, 168]])); | |
| 82 expect(request.readAsString(LATIN1), completion(equals("è"))); | 63 expect(request.readAsString(LATIN1), completion(equals("è"))); |
| 83 }); | 64 }); |
| 84 }); | 65 }); |
| 85 | 66 |
| 86 group("read", () { | 67 group("read", () { |
| 87 test("supports a null body", () { | 68 test("supports a null body", () { |
| 88 var request = _request(); | 69 var request = _createMessage(); |
| 89 expect(request.read().toList(), completion(isEmpty)); | 70 expect(request.read().toList(), completion(isEmpty)); |
| 90 }); | 71 }); |
| 91 | 72 |
| 92 test("supports a Stream<List<int>> body", () { | 73 test("supports a Stream<List<int>> body", () { |
| 93 var controller = new StreamController(); | 74 var controller = new StreamController(); |
| 94 var request = _request({}, controller.stream); | 75 var request = _createMessage(body: controller.stream); |
| 95 expect(request.read().toList(), completion(equals([ | 76 expect(request.read().toList(), completion(equals([ |
| 96 [104, 101, 108, 108, 111, 44], | 77 [104, 101, 108, 108, 111, 44], |
| 97 [32, 119, 111, 114, 108, 100] | 78 [32, 119, 111, 114, 108, 100] |
| 98 ]))); | 79 ]))); |
| 99 | 80 |
| 100 controller.add([104, 101, 108, 108, 111, 44]); | 81 controller.add([104, 101, 108, 108, 111, 44]); |
| 101 return new Future(() { | 82 return new Future(() { |
| 102 controller | 83 controller |
| 103 ..add([32, 119, 111, 114, 108, 100]) | 84 ..add([32, 119, 111, 114, 108, 100]) |
| 104 ..close(); | 85 ..close(); |
| 105 }); | 86 }); |
| 106 }); | 87 }); |
| 107 }); | 88 }); |
| 108 | 89 |
| 90 group("contentLength", () { | |
| 91 test("is null without a content-length header", () { | |
| 92 var request = _createMessage(); | |
| 93 expect(request.contentLength, isNull); | |
| 94 }); | |
| 95 | |
| 96 test("comes from the content-length header", () { | |
| 97 var request = _createMessage(headers: { | |
| 98 'content-length': '42' | |
| 99 }); | |
| 100 expect(request.contentLength, 42); | |
| 101 }); | |
| 102 }); | |
| 103 | |
| 109 group("mimeType", () { | 104 group("mimeType", () { |
| 110 test("is null without a content-type header", () { | 105 test("is null without a content-type header", () { |
| 111 expect(_request().mimeType, isNull); | 106 expect(_createMessage().mimeType, isNull); |
| 112 }); | 107 }); |
| 113 | 108 |
| 114 test("comes from the content-type header", () { | 109 test("comes from the content-type header", () { |
| 115 expect(_request({ | 110 expect(_createMessage(headers: { |
| 116 'content-type': 'text/plain' | 111 'content-type': 'text/plain' |
| 117 }).mimeType, equals('text/plain')); | 112 }).mimeType, equals('text/plain')); |
| 118 }); | 113 }); |
| 119 | 114 |
| 120 test("doesn't include parameters", () { | 115 test("doesn't include parameters", () { |
| 121 expect(_request({ | 116 expect(_createMessage(headers: { |
| 122 'content-type': 'text/plain; foo=bar; bar=baz' | 117 'content-type': 'text/plain; foo=bar; bar=baz' |
| 123 }).mimeType, equals('text/plain')); | 118 }).mimeType, equals('text/plain')); |
| 124 }); | 119 }); |
| 125 }); | 120 }); |
| 126 | 121 |
| 127 group("encoding", () { | 122 group("encoding", () { |
| 128 test("is null without a content-type header", () { | 123 test("is null without a content-type header", () { |
| 129 expect(_request().encoding, isNull); | 124 expect(_createMessage().encoding, isNull); |
| 130 }); | 125 }); |
| 131 | 126 |
| 132 test("is null without a charset parameter", () { | 127 test("is null without a charset parameter", () { |
| 133 expect(_request({ | 128 expect(_createMessage(headers: { |
| 134 'content-type': 'text/plain' | 129 'content-type': 'text/plain' |
| 135 }).encoding, isNull); | 130 }).encoding, isNull); |
| 136 }); | 131 }); |
| 137 | 132 |
| 138 test("is null with an unrecognized charset parameter", () { | 133 test("is null with an unrecognized charset parameter", () { |
| 139 expect(_request({ | 134 expect(_createMessage(headers: { |
| 140 'content-type': 'text/plain; charset=fblthp' | 135 'content-type': 'text/plain; charset=fblthp' |
| 141 }).encoding, isNull); | 136 }).encoding, isNull); |
| 142 }); | 137 }); |
| 143 | 138 |
| 144 test("comes from the content-type charset parameter", () { | 139 test("comes from the content-type charset parameter", () { |
| 145 expect(_request({ | 140 expect(_createMessage(headers: { |
| 146 'content-type': 'text/plain; charset=iso-8859-1' | 141 'content-type': 'text/plain; charset=iso-8859-1' |
| 147 }).encoding, equals(LATIN1)); | 142 }).encoding, equals(LATIN1)); |
| 148 }); | 143 }); |
| 149 }); | 144 }); |
| 150 } | 145 } |
| OLD | NEW |