Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(55)

Side by Side Diff: pkg/shelf/test/message_test.dart

Issue 227563010: pkg/shelf: case-insensitive headers, cleaner Request ctor, a lot more tests (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: fixing dependent code, changelog Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/shelf/pubspec.yaml ('k') | pkg/shelf/test/request_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 class _TestMessage extends Message {
14 if (headers == null) headers = {}; 14 _TestMessage(Map<String, String> headers, Stream<List<int>> body)
15 return new Request("/", "", "GET", "", "1.1", Uri.parse('http://localhost/'), 15 : super(body, headers: headers);
16 headers, body: body); 16 }
17
18 Message _createMessage({Map<String, String> headers, Stream<List<int>> body}) {
19 if (body == null) body = new Stream.fromIterable([]);
20 return new _TestMessage(headers, body);
17 } 21 }
18 22
19 void main() { 23 void main() {
20 group("contentLength", () { 24 group('headers', () {
21 test("is null without a content-length header", () { 25 test('message headers are case insensitive', () {
22 var request = _request(); 26 var message = _createMessage(headers: {'foo': 'bar'});
23 expect(request.contentLength, isNull); 27
28 expect(message.headers, containsPair('foo', 'bar'));
29 expect(message.headers, containsPair('Foo', 'bar'));
30 expect(message.headers, containsPair('FOO', 'bar'));
24 }); 31 });
25 32
26 test("comes from the content-length header", () { 33 test('null header value becomes empty, immutable', () {
27 var request = _request({ 34 var message = _createMessage();
28 'content-length': '42' 35 expect(message.headers, isEmpty);
29 }); 36 expect(() => message.headers['h1'] = 'value1', throwsUnsupportedError);
30 expect(request.contentLength, 42); 37 });
38
39 test('headers are immutable', () {
40 var message = _createMessage(headers: {'h1': 'value1'});
41 expect(() => message.headers['h1'] = 'value1', throwsUnsupportedError);
42 expect(() => message.headers['h1'] = 'value2', throwsUnsupportedError);
43 expect(() => message.headers['h2'] = 'value2', throwsUnsupportedError);
31 }); 44 });
32 }); 45 });
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 });
48
49 group("readAsString", () { 46 group("readAsString", () {
50 test("supports a null body", () { 47 test("supports a null body", () {
51 var request = _request(); 48 var request = _createMessage();
52 expect(request.readAsString(), completion(equals(""))); 49 expect(request.readAsString(), completion(equals("")));
53 }); 50 });
54 51
55 test("supports a Stream<List<int>> body", () { 52 test("supports a Stream<List<int>> body", () {
56 var controller = new StreamController(); 53 var controller = new StreamController();
57 var request = _request({}, controller.stream); 54 var request = _createMessage(body: controller.stream);
58 expect(request.readAsString(), completion(equals("hello, world"))); 55 expect(request.readAsString(), completion(equals("hello, world")));
59 56
60 controller.add([104, 101, 108, 108, 111, 44]); 57 controller.add([104, 101, 108, 108, 111, 44]);
61 return new Future(() { 58 return new Future(() {
62 controller 59 controller
63 ..add([32, 119, 111, 114, 108, 100]) 60 ..add([32, 119, 111, 114, 108, 100])
64 ..close(); 61 ..close();
65 }); 62 });
66 }); 63 });
67 64
68 test("defaults to UTF-8", () { 65 test("defaults to UTF-8", () {
69 var request = _request({}, new Stream.fromIterable([[195, 168]])); 66 var request = _createMessage(body: new Stream.fromIterable([[195, 168]]));
70 expect(request.readAsString(), completion(equals("è"))); 67 expect(request.readAsString(), completion(equals("è")));
71 }); 68 });
72 69
73 test("the content-type header overrides the default", () { 70 test("the content-type header overrides the default", () {
74 var request = _request({'content-type': 'text/plain; charset=iso-8859-1'}, 71 var request = _createMessage(
75 new Stream.fromIterable([[195, 168]])); 72 headers: {'content-type': 'text/plain; charset=iso-8859-1'},
73 body: new Stream.fromIterable([[195, 168]]));
76 expect(request.readAsString(), completion(equals("è"))); 74 expect(request.readAsString(), completion(equals("è")));
77 }); 75 });
78 76
79 test("an explicit encoding overrides the content-type header", () { 77 test("an explicit encoding overrides the content-type header", () {
80 var request = _request({'content-type': 'text/plain; charset=iso-8859-1'}, 78 var request = _createMessage(
81 new Stream.fromIterable([[195, 168]])); 79 headers: {'content-type': 'text/plain; charset=iso-8859-1'},
80 body: new Stream.fromIterable([[195, 168]]));
82 expect(request.readAsString(LATIN1), completion(equals("è"))); 81 expect(request.readAsString(LATIN1), completion(equals("è")));
83 }); 82 });
84 }); 83 });
85 84
86 group("read", () { 85 group("read", () {
87 test("supports a null body", () { 86 test("supports a null body", () {
88 var request = _request(); 87 var request = _createMessage();
89 expect(request.read().toList(), completion(isEmpty)); 88 expect(request.read().toList(), completion(isEmpty));
90 }); 89 });
91 90
92 test("supports a Stream<List<int>> body", () { 91 test("supports a Stream<List<int>> body", () {
93 var controller = new StreamController(); 92 var controller = new StreamController();
94 var request = _request({}, controller.stream); 93 var request = _createMessage(body: controller.stream);
95 expect(request.read().toList(), completion(equals([ 94 expect(request.read().toList(), completion(equals([
96 [104, 101, 108, 108, 111, 44], 95 [104, 101, 108, 108, 111, 44],
97 [32, 119, 111, 114, 108, 100] 96 [32, 119, 111, 114, 108, 100]
98 ]))); 97 ])));
99 98
100 controller.add([104, 101, 108, 108, 111, 44]); 99 controller.add([104, 101, 108, 108, 111, 44]);
101 return new Future(() { 100 return new Future(() {
102 controller 101 controller
103 ..add([32, 119, 111, 114, 108, 100]) 102 ..add([32, 119, 111, 114, 108, 100])
104 ..close(); 103 ..close();
105 }); 104 });
106 }); 105 });
107 }); 106 });
108 107
108 group("contentLength", () {
109 test("is null without a content-length header", () {
110 var request = _createMessage();
111 expect(request.contentLength, isNull);
112 });
113
114 test("comes from the content-length header", () {
115 var request = _createMessage(headers: {
116 'content-length': '42'
117 });
118 expect(request.contentLength, 42);
119 });
120 });
121
109 group("mimeType", () { 122 group("mimeType", () {
110 test("is null without a content-type header", () { 123 test("is null without a content-type header", () {
111 expect(_request().mimeType, isNull); 124 expect(_createMessage().mimeType, isNull);
112 }); 125 });
113 126
114 test("comes from the content-type header", () { 127 test("comes from the content-type header", () {
115 expect(_request({ 128 expect(_createMessage(headers: {
116 'content-type': 'text/plain' 129 'content-type': 'text/plain'
117 }).mimeType, equals('text/plain')); 130 }).mimeType, equals('text/plain'));
118 }); 131 });
119 132
120 test("doesn't include parameters", () { 133 test("doesn't include parameters", () {
121 expect(_request({ 134 expect(_createMessage(headers: {
122 'content-type': 'text/plain; foo=bar; bar=baz' 135 'content-type': 'text/plain; foo=bar; bar=baz'
123 }).mimeType, equals('text/plain')); 136 }).mimeType, equals('text/plain'));
124 }); 137 });
125 }); 138 });
126 139
127 group("encoding", () { 140 group("encoding", () {
128 test("is null without a content-type header", () { 141 test("is null without a content-type header", () {
129 expect(_request().encoding, isNull); 142 expect(_createMessage().encoding, isNull);
130 }); 143 });
131 144
132 test("is null without a charset parameter", () { 145 test("is null without a charset parameter", () {
133 expect(_request({ 146 expect(_createMessage(headers: {
134 'content-type': 'text/plain' 147 'content-type': 'text/plain'
135 }).encoding, isNull); 148 }).encoding, isNull);
136 }); 149 });
137 150
138 test("is null with an unrecognized charset parameter", () { 151 test("is null with an unrecognized charset parameter", () {
139 expect(_request({ 152 expect(_createMessage(headers: {
140 'content-type': 'text/plain; charset=fblthp' 153 'content-type': 'text/plain; charset=fblthp'
141 }).encoding, isNull); 154 }).encoding, isNull);
142 }); 155 });
143 156
144 test("comes from the content-type charset parameter", () { 157 test("comes from the content-type charset parameter", () {
145 expect(_request({ 158 expect(_createMessage(headers: {
146 'content-type': 'text/plain; charset=iso-8859-1' 159 'content-type': 'text/plain; charset=iso-8859-1'
147 }).encoding, equals(LATIN1)); 160 }).encoding, equals(LATIN1));
148 }); 161 });
149 }); 162 });
150 } 163 }
OLDNEW
« no previous file with comments | « pkg/shelf/pubspec.yaml ('k') | pkg/shelf/test/request_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698