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

Side by Side Diff: pkg/http/test/http_test.dart

Issue 11825010: Update pkg/http to use the new async APIs. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Code review changes Created 7 years, 11 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/http/test/client_test.dart ('k') | pkg/http/test/mock_client_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) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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 http_test; 5 library http_test;
6 6
7 import 'dart:io'; 7 import 'dart:io';
8 8
9 import 'package:unittest/unittest.dart'; 9 import 'package:unittest/unittest.dart';
10 import 'package:http/http.dart' as http; 10 import 'package:http/http.dart' as http;
11 import 'utils.dart'; 11 import 'utils.dart';
12 12
13 main() { 13 main() {
14 group('http.', () { 14 group('http.', () {
15 setUp(startServer); 15 setUp(startServer);
16 tearDown(stopServer); 16 tearDown(stopServer);
17 17
18 test('head', () { 18 test('head', () {
19 http.head(serverUrl).then(expectAsync1((response) { 19 expect(http.head(serverUrl).then((response) {
20 expect(response.statusCode, equals(200)); 20 expect(response.statusCode, equals(200));
21 expect(response.body, equals('')); 21 expect(response.body, equals(''));
22 })); 22 }), completes);
23 }); 23 });
24 24
25 test('get', () { 25 test('get', () {
26 expect(http.get(serverUrl, headers: { 26 expect(http.get(serverUrl, headers: {
27 'X-Random-Header': 'Value', 27 'X-Random-Header': 'Value',
28 'X-Other-Header': 'Other Value' 28 'X-Other-Header': 'Other Value'
29 }).then(expectAsync1((response) { 29 }).then((response) {
30 expect(response.statusCode, equals(200)); 30 expect(response.statusCode, equals(200));
31 expect(response.body, parse(equals({ 31 expect(response.body, parse(equals({
32 'method': 'GET', 32 'method': 'GET',
33 'path': '/', 33 'path': '/',
34 'headers': { 34 'headers': {
35 'content-length': ['0'], 35 'content-length': ['0'],
36 'x-random-header': ['Value'], 36 'x-random-header': ['Value'],
37 'x-other-header': ['Other Value'] 37 'x-other-header': ['Other Value']
38 }, 38 },
39 }))); 39 })));
40 })), completes); 40 }), completes);
41 }); 41 });
42 42
43 test('post', () { 43 test('post', () {
44 expect(http.post(serverUrl, headers: { 44 expect(http.post(serverUrl, headers: {
45 'X-Random-Header': 'Value', 45 'X-Random-Header': 'Value',
46 'X-Other-Header': 'Other Value' 46 'X-Other-Header': 'Other Value'
47 }, fields: { 47 }, fields: {
48 'some-field': 'value', 48 'some-field': 'value',
49 'other-field': 'other value' 49 'other-field': 'other value'
50 }).then(expectAsync1((response) { 50 }).then((response) {
51 expect(response.statusCode, equals(200)); 51 expect(response.statusCode, equals(200));
52 expect(response.body, parse(equals({ 52 expect(response.body, parse(equals({
53 'method': 'POST', 53 'method': 'POST',
54 'path': '/', 54 'path': '/',
55 'headers': { 55 'headers': {
56 'content-type': [ 56 'content-type': [
57 'application/x-www-form-urlencoded; charset=UTF-8' 57 'application/x-www-form-urlencoded; charset=UTF-8'
58 ], 58 ],
59 'content-length': ['40'], 59 'content-length': ['40'],
60 'x-random-header': ['Value'], 60 'x-random-header': ['Value'],
61 'x-other-header': ['Other Value'] 61 'x-other-header': ['Other Value']
62 }, 62 },
63 'body': 'some-field=value&other-field=other+value' 63 'body': 'some-field=value&other-field=other+value'
64 }))); 64 })));
65 })), completes); 65 }), completes);
66 }); 66 });
67 67
68 test('post without fields', () { 68 test('post without fields', () {
69 expect(http.post(serverUrl, headers: { 69 expect(http.post(serverUrl, headers: {
70 'X-Random-Header': 'Value', 70 'X-Random-Header': 'Value',
71 'X-Other-Header': 'Other Value', 71 'X-Other-Header': 'Other Value',
72 'Content-Type': 'text/plain' 72 'Content-Type': 'text/plain'
73 }).then(expectAsync1((response) { 73 }).then((response) {
74 expect(response.statusCode, equals(200)); 74 expect(response.statusCode, equals(200));
75 expect(response.body, parse(equals({ 75 expect(response.body, parse(equals({
76 'method': 'POST', 76 'method': 'POST',
77 'path': '/', 77 'path': '/',
78 'headers': { 78 'headers': {
79 'content-length': ['0'], 79 'content-length': ['0'],
80 'content-type': ['text/plain'], 80 'content-type': ['text/plain'],
81 'x-random-header': ['Value'], 81 'x-random-header': ['Value'],
82 'x-other-header': ['Other Value'] 82 'x-other-header': ['Other Value']
83 } 83 }
84 }))); 84 })));
85 })), completes); 85 }), completes);
86 }); 86 });
87 87
88 test('put', () { 88 test('put', () {
89 expect(http.put(serverUrl, headers: { 89 expect(http.put(serverUrl, headers: {
90 'X-Random-Header': 'Value', 90 'X-Random-Header': 'Value',
91 'X-Other-Header': 'Other Value' 91 'X-Other-Header': 'Other Value'
92 }, fields: { 92 }, fields: {
93 'some-field': 'value', 93 'some-field': 'value',
94 'other-field': 'other value' 94 'other-field': 'other value'
95 }).then(expectAsync1((response) { 95 }).then((response) {
96 expect(response.statusCode, equals(200)); 96 expect(response.statusCode, equals(200));
97 expect(response.body, parse(equals({ 97 expect(response.body, parse(equals({
98 'method': 'PUT', 98 'method': 'PUT',
99 'path': '/', 99 'path': '/',
100 'headers': { 100 'headers': {
101 'content-type': [ 101 'content-type': [
102 'application/x-www-form-urlencoded; charset=UTF-8' 102 'application/x-www-form-urlencoded; charset=UTF-8'
103 ], 103 ],
104 'content-length': ['40'], 104 'content-length': ['40'],
105 'x-random-header': ['Value'], 105 'x-random-header': ['Value'],
106 'x-other-header': ['Other Value'] 106 'x-other-header': ['Other Value']
107 }, 107 },
108 'body': 'some-field=value&other-field=other+value' 108 'body': 'some-field=value&other-field=other+value'
109 }))); 109 })));
110 })), completes); 110 }), completes);
111 }); 111 });
112 112
113 test('put without fields', () { 113 test('put without fields', () {
114 expect(http.put(serverUrl, headers: { 114 expect(http.put(serverUrl, headers: {
115 'X-Random-Header': 'Value', 115 'X-Random-Header': 'Value',
116 'X-Other-Header': 'Other Value', 116 'X-Other-Header': 'Other Value',
117 'Content-Type': 'text/plain' 117 'Content-Type': 'text/plain'
118 }).then(expectAsync1((response) { 118 }).then((response) {
119 expect(response.statusCode, equals(200)); 119 expect(response.statusCode, equals(200));
120 expect(response.body, parse(equals({ 120 expect(response.body, parse(equals({
121 'method': 'PUT', 121 'method': 'PUT',
122 'path': '/', 122 'path': '/',
123 'headers': { 123 'headers': {
124 'content-length': ['0'], 124 'content-length': ['0'],
125 'content-type': ['text/plain'], 125 'content-type': ['text/plain'],
126 'x-random-header': ['Value'], 126 'x-random-header': ['Value'],
127 'x-other-header': ['Other Value'] 127 'x-other-header': ['Other Value']
128 } 128 }
129 }))); 129 })));
130 })), completes); 130 }), completes);
131 }); 131 });
132 132
133 test('delete', () { 133 test('delete', () {
134 expect(http.delete(serverUrl, headers: { 134 expect(http.delete(serverUrl, headers: {
135 'X-Random-Header': 'Value', 135 'X-Random-Header': 'Value',
136 'X-Other-Header': 'Other Value' 136 'X-Other-Header': 'Other Value'
137 }).then(expectAsync1((response) { 137 }).then((response) {
138 expect(response.statusCode, equals(200)); 138 expect(response.statusCode, equals(200));
139 expect(response.body, parse(equals({ 139 expect(response.body, parse(equals({
140 'method': 'DELETE', 140 'method': 'DELETE',
141 'path': '/', 141 'path': '/',
142 'headers': { 142 'headers': {
143 'content-length': ['0'], 143 'content-length': ['0'],
144 'x-random-header': ['Value'], 144 'x-random-header': ['Value'],
145 'x-other-header': ['Other Value'] 145 'x-other-header': ['Other Value']
146 } 146 }
147 }))); 147 })));
148 })), completes); 148 }), completes);
149 }); 149 });
150 150
151 test('read', () { 151 test('read', () {
152 expect(http.read(serverUrl, headers: { 152 expect(http.read(serverUrl, headers: {
153 'X-Random-Header': 'Value', 153 'X-Random-Header': 'Value',
154 'X-Other-Header': 'Other Value' 154 'X-Other-Header': 'Other Value'
155 }).then(expectAsync1((val) => val)), completion(parse(equals({ 155 }).then((val) => val), completion(parse(equals({
156 'method': 'GET', 156 'method': 'GET',
157 'path': '/', 157 'path': '/',
158 'headers': { 158 'headers': {
159 'content-length': ['0'], 159 'content-length': ['0'],
160 'x-random-header': ['Value'], 160 'x-random-header': ['Value'],
161 'x-other-header': ['Other Value'] 161 'x-other-header': ['Other Value']
162 }, 162 },
163 })))); 163 }))));
164 }); 164 });
165 165
166 test('read throws an error for a 4** status code', () { 166 test('read throws an error for a 4** status code', () {
167 http.read(serverUrl.resolve('/error')) 167 expect(http.read(serverUrl.resolve('/error')), throwsHttpException);
168 .then((_) { throw "Error expected for readBytes"; })
169 .catchError(expectAsync1((e) { }), test: (e) => e is HttpException);
170 }); 168 });
171 169
172 test('readBytes', () { 170 test('readBytes', () {
173 var future = http.readBytes(serverUrl, headers: { 171 var future = http.readBytes(serverUrl, headers: {
174 'X-Random-Header': 'Value', 172 'X-Random-Header': 'Value',
175 'X-Other-Header': 'Other Value' 173 'X-Other-Header': 'Other Value'
176 }).then(expectAsync1((bytes) => new String.fromCharCodes(bytes))); 174 }).then((bytes) => new String.fromCharCodes(bytes));
177 175
178 expect(future, completion(parse(equals({ 176 expect(future, completion(parse(equals({
179 'method': 'GET', 177 'method': 'GET',
180 'path': '/', 178 'path': '/',
181 'headers': { 179 'headers': {
182 'content-length': ['0'], 180 'content-length': ['0'],
183 'x-random-header': ['Value'], 181 'x-random-header': ['Value'],
184 'x-other-header': ['Other Value'] 182 'x-other-header': ['Other Value']
185 }, 183 },
186 })))); 184 }))));
187 }); 185 });
188 186
189 test('readBytes throws an error for a 4** status code', () { 187 test('readBytes throws an error for a 4** status code', () {
190 http.readBytes(serverUrl.resolve('/error')) 188 expect(http.readBytes(serverUrl.resolve('/error')), throwsHttpException);
191 .then((_) { throw "Error expected for readBytes"; })
192 .catchError(expectAsync1((e) { }), test: (e) => e is HttpException);
193 }); 189 });
194 }); 190 });
195 } 191 }
OLDNEW
« no previous file with comments | « pkg/http/test/client_test.dart ('k') | pkg/http/test/mock_client_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698