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

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

Issue 219283008: pkg/shelf (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: cl nits 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/response_test.dart ('k') | pkg/shelf/test/stack_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
(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_io_test;
6
7 import 'dart:async';
8 import 'dart:convert';
9 import 'dart:io';
10
11 import 'package:http/http.dart' as http;
12 import 'package:scheduled_test/scheduled_test.dart';
13 import 'package:shelf/shelf.dart';
14 import 'package:shelf/shelf_io.dart' as shelf_io;
15
16 import 'test_util.dart';
17
18 void main() {
19 test('sync handler returns a value to the client', () {
20 _scheduleServer(syncHandler);
21
22 return _scheduleGet().then((response) {
23 expect(response.statusCode, HttpStatus.OK);
24 expect(response.body, 'Hello from /');
25 });
26 });
27
28 test('async handler returns a value to the client', () {
29 _scheduleServer(asyncHandler);
30
31 return _scheduleGet().then((response) {
32 expect(response.statusCode, HttpStatus.OK);
33 expect(response.body, 'Hello from /');
34 });
35 });
36
37 test('sync null response leads to a 500', () {
38 _scheduleServer((request) => null);
39
40 return _scheduleGet().then((response) {
41 expect(response.statusCode, HttpStatus.INTERNAL_SERVER_ERROR);
42 expect(response.body, 'Internal Server Error');
43 });
44 });
45
46 test('async null response leads to a 500', () {
47 _scheduleServer((request) => new Future.value(null));
48
49 return _scheduleGet().then((response) {
50 expect(response.statusCode, HttpStatus.INTERNAL_SERVER_ERROR);
51 expect(response.body, 'Internal Server Error');
52 });
53 });
54
55 test('thrown error leads to a 500', () {
56 _scheduleServer((request) {
57 throw new UnsupportedError('test');
58 });
59
60 return _scheduleGet().then((response) {
61 expect(response.statusCode, HttpStatus.INTERNAL_SERVER_ERROR);
62 expect(response.body, 'Internal Server Error');
63 });
64 });
65
66 test('async error leads to a 500', () {
67 _scheduleServer((request) {
68 return new Future.error('test');
69 });
70
71 return _scheduleGet().then((response) {
72 expect(response.statusCode, HttpStatus.INTERNAL_SERVER_ERROR);
73 expect(response.body, 'Internal Server Error');
74 });
75 });
76
77 test('Request is populated correctly', () {
78 var path = '/foo/bar?qs=value#anchor';
79
80 _scheduleServer((request) {
81 expect(request.contentLength, 0);
82 expect(request.method, 'GET');
83
84 var expectedUrl = 'http://localhost:$_serverPort$path';
85 expect(request.requestedUri, Uri.parse(expectedUrl));
86
87 expect(request.pathInfo, '/foo/bar');
88 expect(request.pathSegments, ['foo', 'bar']);
89 expect(request.protocolVersion, '1.1');
90 expect(request.queryString, 'qs=value');
91 expect(request.scriptName, '');
92
93 return syncHandler(request);
94 });
95
96 return schedule(() => http.get('http://localhost:$_serverPort$path'))
97 .then((response) {
98 expect(response.statusCode, HttpStatus.OK);
99 expect(response.body, 'Hello from /foo/bar');
100 });
101 });
102
103 test('custom response headers are received by the client', () {
104 _scheduleServer((request) {
105 return new Response.ok('Hello from /', headers: {
106 'test-header': 'test-value',
107 'test-list': 'a, b, c'
108 });
109 });
110
111 return _scheduleGet().then((response) {
112 expect(response.statusCode, HttpStatus.OK);
113 expect(response.headers['test-header'], 'test-value');
114 expect(response.body, 'Hello from /');
115 });
116 });
117
118 test('custom status code is received by the client', () {
119 _scheduleServer((request) {
120 return new Response(299, body: 'Hello from /');
121 });
122
123 return _scheduleGet().then((response) {
124 expect(response.statusCode, 299);
125 expect(response.body, 'Hello from /');
126 });
127 });
128
129 test('custom request headers are received by the handler', () {
130 _scheduleServer((request) {
131 expect(request.headers, containsPair('custom-header', 'client value'));
132 return syncHandler(request);
133 });
134
135 var headers = {
136 'custom-header': 'client value'
137 };
138
139 return _scheduleGet(headers: headers).then((response) {
140 expect(response.statusCode, HttpStatus.OK);
141 expect(response.body, 'Hello from /');
142 });
143 });
144
145 test('post with empty content', () {
146 _scheduleServer((request) {
147 expect(request.mimeType, isNull);
148 expect(request.encoding, isNull);
149 expect(request.method, 'POST');
150 expect(request.contentLength, 0);
151
152 return request.readAsString().then((body) {
153 expect(body, '');
154 return syncHandler(request);
155 });
156 });
157
158 return _schedulePost().then((response) {
159 expect(response.statusCode, HttpStatus.OK);
160 expect(response.stream.bytesToString(), completion('Hello from /'));
161 });
162 });
163
164 test('post with request content', () {
165 _scheduleServer((request) {
166 expect(request.mimeType, 'text/plain');
167 expect(request.encoding, UTF8);
168 expect(request.method, 'POST');
169 expect(request.contentLength, 9);
170
171 return request.readAsString().then((body) {
172 expect(body, 'test body');
173 return syncHandler(request);
174 });
175 });
176
177 return _schedulePost(body: 'test body').then((response) {
178 expect(response.statusCode, HttpStatus.OK);
179 expect(response.stream.bytesToString(), completion('Hello from /'));
180 });
181 });
182 }
183
184 int _serverPort;
185
186 Future _scheduleServer(Handler handler) {
187 return schedule(() => shelf_io.serve(handler, 'localhost', 0).then((server) {
188 currentSchedule.onComplete.schedule(() {
189 _serverPort = null;
190 return server.close(force: true);
191 });
192
193 _serverPort = server.port;
194 }));
195 }
196
197 Future<http.Response> _scheduleGet({Map<String, String> headers}) {
198 if (headers == null) headers = {};
199
200 return schedule(() =>
201 http.get('http://localhost:$_serverPort/', headers: headers));
202 }
203
204 Future<http.StreamedResponse> _schedulePost({Map<String, String> headers,
205 String body}) {
206
207 return schedule(() {
208
209 var request = new http.Request('POST',
210 Uri.parse('http://localhost:$_serverPort/'));
211
212 if (headers != null) request.headers.addAll(headers);
213 if (body != null) request.body = body;
214
215 return request.send();
216 });
217 }
OLDNEW
« no previous file with comments | « pkg/shelf/test/response_test.dart ('k') | pkg/shelf/test/stack_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698