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

Side by Side Diff: pkg/shelf/test/request_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/test/message_test.dart ('k') | pkg/shelf/test/response_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.request_test;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 import 'dart:convert';
9 8
10 import 'package:shelf/shelf.dart'; 9 import 'package:shelf/shelf.dart';
11 import 'package:unittest/unittest.dart'; 10 import 'package:unittest/unittest.dart';
12 11
12 import 'test_util.dart';
13
13 Request _request([Map<String, String> headers, Stream<List<int>> body]) { 14 Request _request([Map<String, String> headers, Stream<List<int>> body]) {
14 if (headers == null) headers = {}; 15 return new Request("GET", LOCALHOST_URI, headers: headers, body: body);
15 return new Request("/", "", "GET", "", "1.1", Uri.parse('http://localhost/'),
16 headers, body: body);
17 } 16 }
18 17
19 void main() { 18 void main() {
20 group("contentLength", () { 19 group('constructor', () {
21 test("is null without a content-length header", () { 20 test('protocolVersion defaults to "1.1"', () {
22 var request = _request(); 21 var request = new Request('GET', LOCALHOST_URI);
23 expect(request.contentLength, isNull); 22 expect(request.protocolVersion, '1.1');
24 }); 23 });
25 24
26 test("comes from the content-length header", () { 25 test('provide non-default protocolVersion', () {
27 var request = _request({ 26 var request = new Request('GET', LOCALHOST_URI, protocolVersion: '1.0');
28 'content-length': '42' 27 expect(request.protocolVersion, '1.0');
29 }); 28 });
30 expect(request.contentLength, 42); 29
30 test('requestedUri must be absolute', () {
31 expect(() => new Request('GET', Uri.parse('/path')),
32 throwsArgumentError);
33 });
34
35 test('if uri is null, scriptName must be null', () {
36 expect(() => new Request('GET', Uri.parse('/path'),
37 scriptName: '/script/name'), throwsArgumentError);
38 });
39
40 test('if scriptName is null, uri must be null', () {
41 var relativeUri = new Uri(path: '/cool/beans.html');
42 expect(() => new Request('GET', Uri.parse('/path'),
43 url: relativeUri), throwsArgumentError);
44 });
45
46 test('uri must be relative', () {
47 var relativeUri = Uri.parse('http://localhost/test');
48
49 expect(() => new Request('GET', LOCALHOST_URI,
50 url: relativeUri, scriptName: '/news'),
51 throwsArgumentError);
52
53 // NOTE: explicitly testing fragments due to Issue 18053
54 relativeUri = Uri.parse('http://localhost/test#fragment');
55
56 expect(() => new Request('GET', LOCALHOST_URI,
57 url: relativeUri, scriptName: '/news'),
58 throwsArgumentError);
59 });
60
61 test('uri and scriptName', () {
62 var pathInfo = '/pages/page.html?utm_source=ABC123';
63 var scriptName = '/assets/static';
64 var fullUrl = 'http://localhost/other_path/other_resource.asp';
65 var request = new Request('GET', Uri.parse(fullUrl),
66 url: Uri.parse(pathInfo), scriptName: scriptName);
67
68 expect(request.scriptName, scriptName);
69 expect(request.url.path, '/pages/page.html');
70 expect(request.url.query, 'utm_source=ABC123');
71 });
72
73 test('minimal uri', () {
74 var pathInfo = '/';
75 var scriptName = '/assets/static';
76 var fullUrl = 'http://localhost$scriptName$pathInfo';
77 var request = new Request('GET', Uri.parse(fullUrl),
78 url: Uri.parse(pathInfo), scriptName: scriptName);
79
80 expect(request.scriptName, scriptName);
81 expect(request.url.path, '/');
82 expect(request.url.query, '');
83 });
84
85 test('invalid url', () {
86 var testUrl = 'page';
87 var scriptName = '/assets/static';
88 var fullUrl = 'http://localhost$scriptName$testUrl';
89
90 expect(() => new Request('GET', Uri.parse(fullUrl),
91 url: Uri.parse(testUrl), scriptName: scriptName),
92 throwsArgumentError);
93 });
94
95 test('scriptName with no leading slash', () {
96 var pathInfo = '/page';
97 var scriptName = 'assets/static';
98 var fullUrl = 'http://localhost/assets/static/pages';
99
100 expect(() => new Request('GET',Uri.parse(fullUrl),
101 url: Uri.parse(pathInfo), scriptName: scriptName),
102 throwsArgumentError);
103
104 pathInfo = '/assets/static/page';
105 scriptName = '/';
106 fullUrl = 'http://localhost/assets/static/pages';
107
108 expect(() => new Request('GET',Uri.parse(fullUrl),
109 url: Uri.parse(pathInfo), scriptName: scriptName),
110 throwsArgumentError);
111 });
112
113 test('scriptName that is only a slash', () {
114 var pathInfo = '/assets/static/page';
115 var scriptName = '/';
116 var fullUrl = 'http://localhost/assets/static/pages';
117
118 expect(() => new Request('GET',Uri.parse(fullUrl),
119 url: Uri.parse(pathInfo), scriptName: scriptName),
120 throwsArgumentError);
31 }); 121 });
32 }); 122 });
33 123
34 group("ifModifiedSince", () { 124 group("ifModifiedSince", () {
35 test("is null without an If-Modified-Since header", () { 125 test("is null without an If-Modified-Since header", () {
36 var request = _request(); 126 var request = _request();
37 expect(request.ifModifiedSince, isNull); 127 expect(request.ifModifiedSince, isNull);
38 }); 128 });
39 129
40 test("comes from the Last-Modified header", () { 130 test("comes from the Last-Modified header", () {
(...skipping 16 matching lines...) Expand all
57 var request = _request({}, controller.stream); 147 var request = _request({}, controller.stream);
58 expect(request.readAsString(), completion(equals("hello, world"))); 148 expect(request.readAsString(), completion(equals("hello, world")));
59 149
60 controller.add([104, 101, 108, 108, 111, 44]); 150 controller.add([104, 101, 108, 108, 111, 44]);
61 return new Future(() { 151 return new Future(() {
62 controller 152 controller
63 ..add([32, 119, 111, 114, 108, 100]) 153 ..add([32, 119, 111, 114, 108, 100])
64 ..close(); 154 ..close();
65 }); 155 });
66 }); 156 });
67
68 test("defaults to UTF-8", () {
69 var request = _request({}, new Stream.fromIterable([[195, 168]]));
70 expect(request.readAsString(), completion(equals("è")));
71 });
72
73 test("the content-type header overrides the default", () {
74 var request = _request({'content-type': 'text/plain; charset=iso-8859-1'},
75 new Stream.fromIterable([[195, 168]]));
76 expect(request.readAsString(), completion(equals("è")));
77 });
78
79 test("an explicit encoding overrides the content-type header", () {
80 var request = _request({'content-type': 'text/plain; charset=iso-8859-1'},
81 new Stream.fromIterable([[195, 168]]));
82 expect(request.readAsString(LATIN1), completion(equals("è")));
83 });
84 }); 157 });
85 158
86 group("read", () { 159 group("read", () {
87 test("supports a null body", () { 160 test("supports a null body", () {
88 var request = _request(); 161 var request = _request();
89 expect(request.read().toList(), completion(isEmpty)); 162 expect(request.read().toList(), completion(isEmpty));
90 }); 163 });
91 164
92 test("supports a Stream<List<int>> body", () { 165 test("supports a Stream<List<int>> body", () {
93 var controller = new StreamController(); 166 var controller = new StreamController();
94 var request = _request({}, controller.stream); 167 var request = _request({}, controller.stream);
95 expect(request.read().toList(), completion(equals([ 168 expect(request.read().toList(), completion(equals([
96 [104, 101, 108, 108, 111, 44], 169 [104, 101, 108, 108, 111, 44],
97 [32, 119, 111, 114, 108, 100] 170 [32, 119, 111, 114, 108, 100]
98 ]))); 171 ])));
99 172
100 controller.add([104, 101, 108, 108, 111, 44]); 173 controller.add([104, 101, 108, 108, 111, 44]);
101 return new Future(() { 174 return new Future(() {
102 controller 175 controller
103 ..add([32, 119, 111, 114, 108, 100]) 176 ..add([32, 119, 111, 114, 108, 100])
104 ..close(); 177 ..close();
105 }); 178 });
106 }); 179 });
107 }); 180 });
108
109 group("mimeType", () {
110 test("is null without a content-type header", () {
111 expect(_request().mimeType, isNull);
112 });
113
114 test("comes from the content-type header", () {
115 expect(_request({
116 'content-type': 'text/plain'
117 }).mimeType, equals('text/plain'));
118 });
119
120 test("doesn't include parameters", () {
121 expect(_request({
122 'content-type': 'text/plain; foo=bar; bar=baz'
123 }).mimeType, equals('text/plain'));
124 });
125 });
126
127 group("encoding", () {
128 test("is null without a content-type header", () {
129 expect(_request().encoding, isNull);
130 });
131
132 test("is null without a charset parameter", () {
133 expect(_request({
134 'content-type': 'text/plain'
135 }).encoding, isNull);
136 });
137
138 test("is null with an unrecognized charset parameter", () {
139 expect(_request({
140 'content-type': 'text/plain; charset=fblthp'
141 }).encoding, isNull);
142 });
143
144 test("comes from the content-type charset parameter", () {
145 expect(_request({
146 'content-type': 'text/plain; charset=iso-8859-1'
147 }).encoding, equals(LATIN1));
148 });
149 });
150 } 181 }
OLDNEW
« no previous file with comments | « pkg/shelf/test/message_test.dart ('k') | pkg/shelf/test/response_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698