OLD | NEW |
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); | |
16 tearDown(stopServer); | 15 tearDown(stopServer); |
17 | 16 |
18 test('head', () { | 17 test('head', () { |
19 expect(http.head(serverUrl).then((response) { | 18 expect(startServer().then((_) { |
20 expect(response.statusCode, equals(200)); | 19 expect(http.head(serverUrl).then((response) { |
21 expect(response.body, equals('')); | 20 expect(response.statusCode, equals(200)); |
| 21 expect(response.body, equals('')); |
| 22 }), completes); |
22 }), completes); | 23 }), completes); |
23 }); | 24 }); |
24 | 25 |
25 test('get', () { | 26 test('get', () { |
26 expect(http.get(serverUrl, headers: { | 27 expect(startServer().then((_) { |
27 'X-Random-Header': 'Value', | 28 expect(http.get(serverUrl, headers: { |
28 'X-Other-Header': 'Other Value' | 29 'X-Random-Header': 'Value', |
29 }).then((response) { | 30 'X-Other-Header': 'Other Value' |
30 expect(response.statusCode, equals(200)); | 31 }).then((response) { |
31 expect(response.body, parse(equals({ | 32 expect(response.statusCode, equals(200)); |
| 33 expect(response.body, parse(equals({ |
| 34 'method': 'GET', |
| 35 'path': '/', |
| 36 'headers': { |
| 37 'content-length': ['0'], |
| 38 'x-random-header': ['Value'], |
| 39 'x-other-header': ['Other Value'] |
| 40 }, |
| 41 }))); |
| 42 }), completes); |
| 43 }), completes); |
| 44 }); |
| 45 |
| 46 test('post', () { |
| 47 expect(startServer().then((_) { |
| 48 expect(http.post(serverUrl, headers: { |
| 49 'X-Random-Header': 'Value', |
| 50 'X-Other-Header': 'Other Value' |
| 51 }, fields: { |
| 52 'some-field': 'value', |
| 53 'other-field': 'other value' |
| 54 }).then((response) { |
| 55 expect(response.statusCode, equals(200)); |
| 56 expect(response.body, parse(equals({ |
| 57 'method': 'POST', |
| 58 'path': '/', |
| 59 'headers': { |
| 60 'content-type': [ |
| 61 'application/x-www-form-urlencoded; charset=UTF-8' |
| 62 ], |
| 63 'content-length': ['40'], |
| 64 'x-random-header': ['Value'], |
| 65 'x-other-header': ['Other Value'] |
| 66 }, |
| 67 'body': 'some-field=value&other-field=other+value' |
| 68 }))); |
| 69 }), completes); |
| 70 }), completes); |
| 71 }); |
| 72 |
| 73 test('post without fields', () { |
| 74 expect(startServer().then((_) { |
| 75 expect(http.post(serverUrl, headers: { |
| 76 'X-Random-Header': 'Value', |
| 77 'X-Other-Header': 'Other Value', |
| 78 'Content-Type': 'text/plain' |
| 79 }).then((response) { |
| 80 expect(response.statusCode, equals(200)); |
| 81 expect(response.body, parse(equals({ |
| 82 'method': 'POST', |
| 83 'path': '/', |
| 84 'headers': { |
| 85 'content-length': ['0'], |
| 86 'content-type': ['text/plain'], |
| 87 'x-random-header': ['Value'], |
| 88 'x-other-header': ['Other Value'] |
| 89 } |
| 90 }))); |
| 91 }), completes); |
| 92 }), completes); |
| 93 }); |
| 94 |
| 95 test('put', () { |
| 96 expect(startServer().then((_) { |
| 97 expect(http.put(serverUrl, headers: { |
| 98 'X-Random-Header': 'Value', |
| 99 'X-Other-Header': 'Other Value' |
| 100 }, fields: { |
| 101 'some-field': 'value', |
| 102 'other-field': 'other value' |
| 103 }).then((response) { |
| 104 expect(response.statusCode, equals(200)); |
| 105 expect(response.body, parse(equals({ |
| 106 'method': 'PUT', |
| 107 'path': '/', |
| 108 'headers': { |
| 109 'content-type': [ |
| 110 'application/x-www-form-urlencoded; charset=UTF-8' |
| 111 ], |
| 112 'content-length': ['40'], |
| 113 'x-random-header': ['Value'], |
| 114 'x-other-header': ['Other Value'] |
| 115 }, |
| 116 'body': 'some-field=value&other-field=other+value' |
| 117 }))); |
| 118 }), completes); |
| 119 }), completes); |
| 120 }); |
| 121 |
| 122 test('put without fields', () { |
| 123 expect(startServer().then((_) { |
| 124 expect(http.put(serverUrl, headers: { |
| 125 'X-Random-Header': 'Value', |
| 126 'X-Other-Header': 'Other Value', |
| 127 'Content-Type': 'text/plain' |
| 128 }).then((response) { |
| 129 expect(response.statusCode, equals(200)); |
| 130 expect(response.body, parse(equals({ |
| 131 'method': 'PUT', |
| 132 'path': '/', |
| 133 'headers': { |
| 134 'content-length': ['0'], |
| 135 'content-type': ['text/plain'], |
| 136 'x-random-header': ['Value'], |
| 137 'x-other-header': ['Other Value'] |
| 138 } |
| 139 }))); |
| 140 }), completes); |
| 141 }), completes); |
| 142 }); |
| 143 |
| 144 test('delete', () { |
| 145 expect(startServer().then((_) { |
| 146 expect(http.delete(serverUrl, headers: { |
| 147 'X-Random-Header': 'Value', |
| 148 'X-Other-Header': 'Other Value' |
| 149 }).then((response) { |
| 150 expect(response.statusCode, equals(200)); |
| 151 expect(response.body, parse(equals({ |
| 152 'method': 'DELETE', |
| 153 'path': '/', |
| 154 'headers': { |
| 155 'content-length': ['0'], |
| 156 'x-random-header': ['Value'], |
| 157 'x-other-header': ['Other Value'] |
| 158 } |
| 159 }))); |
| 160 }), completes); |
| 161 }), completes); |
| 162 }); |
| 163 |
| 164 test('read', () { |
| 165 expect(startServer().then((_) { |
| 166 expect(http.read(serverUrl, headers: { |
| 167 'X-Random-Header': 'Value', |
| 168 'X-Other-Header': 'Other Value' |
| 169 }).then((val) => val), completion(parse(equals({ |
32 'method': 'GET', | 170 'method': 'GET', |
33 'path': '/', | 171 'path': '/', |
34 'headers': { | 172 'headers': { |
35 'content-length': ['0'], | 173 'content-length': ['0'], |
36 'x-random-header': ['Value'], | 174 'x-random-header': ['Value'], |
37 'x-other-header': ['Other Value'] | 175 'x-other-header': ['Other Value'] |
38 }, | 176 }, |
39 }))); | 177 })))); |
40 }), completes); | 178 }), completes); |
41 }); | 179 }); |
42 | 180 |
43 test('post', () { | 181 test('read throws an error for a 4** status code', () { |
44 expect(http.post(serverUrl, headers: { | 182 expect(startServer().then((_) { |
45 'X-Random-Header': 'Value', | 183 expect(http.read(serverUrl.resolve('/error')), throwsHttpException); |
46 'X-Other-Header': 'Other Value' | 184 }), completes); |
47 }, fields: { | 185 }); |
48 'some-field': 'value', | 186 |
49 'other-field': 'other value' | 187 test('readBytes', () { |
50 }).then((response) { | 188 expect(startServer().then((_) { |
51 expect(response.statusCode, equals(200)); | 189 var future = http.readBytes(serverUrl, headers: { |
52 expect(response.body, parse(equals({ | 190 'X-Random-Header': 'Value', |
53 'method': 'POST', | 191 'X-Other-Header': 'Other Value' |
54 'path': '/', | 192 }).then((bytes) => new String.fromCharCodes(bytes)); |
55 'headers': { | 193 |
56 'content-type': [ | 194 expect(future, completion(parse(equals({ |
57 'application/x-www-form-urlencoded; charset=UTF-8' | 195 'method': 'GET', |
58 ], | |
59 'content-length': ['40'], | |
60 'x-random-header': ['Value'], | |
61 'x-other-header': ['Other Value'] | |
62 }, | |
63 'body': 'some-field=value&other-field=other+value' | |
64 }))); | |
65 }), completes); | |
66 }); | |
67 | |
68 test('post without fields', () { | |
69 expect(http.post(serverUrl, headers: { | |
70 'X-Random-Header': 'Value', | |
71 'X-Other-Header': 'Other Value', | |
72 'Content-Type': 'text/plain' | |
73 }).then((response) { | |
74 expect(response.statusCode, equals(200)); | |
75 expect(response.body, parse(equals({ | |
76 'method': 'POST', | |
77 'path': '/', | |
78 'headers': { | |
79 'content-length': ['0'], | |
80 'content-type': ['text/plain'], | |
81 'x-random-header': ['Value'], | |
82 'x-other-header': ['Other Value'] | |
83 } | |
84 }))); | |
85 }), completes); | |
86 }); | |
87 | |
88 test('put', () { | |
89 expect(http.put(serverUrl, headers: { | |
90 'X-Random-Header': 'Value', | |
91 'X-Other-Header': 'Other Value' | |
92 }, fields: { | |
93 'some-field': 'value', | |
94 'other-field': 'other value' | |
95 }).then((response) { | |
96 expect(response.statusCode, equals(200)); | |
97 expect(response.body, parse(equals({ | |
98 'method': 'PUT', | |
99 'path': '/', | |
100 'headers': { | |
101 'content-type': [ | |
102 'application/x-www-form-urlencoded; charset=UTF-8' | |
103 ], | |
104 'content-length': ['40'], | |
105 'x-random-header': ['Value'], | |
106 'x-other-header': ['Other Value'] | |
107 }, | |
108 'body': 'some-field=value&other-field=other+value' | |
109 }))); | |
110 }), completes); | |
111 }); | |
112 | |
113 test('put without fields', () { | |
114 expect(http.put(serverUrl, headers: { | |
115 'X-Random-Header': 'Value', | |
116 'X-Other-Header': 'Other Value', | |
117 'Content-Type': 'text/plain' | |
118 }).then((response) { | |
119 expect(response.statusCode, equals(200)); | |
120 expect(response.body, parse(equals({ | |
121 'method': 'PUT', | |
122 'path': '/', | |
123 'headers': { | |
124 'content-length': ['0'], | |
125 'content-type': ['text/plain'], | |
126 'x-random-header': ['Value'], | |
127 'x-other-header': ['Other Value'] | |
128 } | |
129 }))); | |
130 }), completes); | |
131 }); | |
132 | |
133 test('delete', () { | |
134 expect(http.delete(serverUrl, headers: { | |
135 'X-Random-Header': 'Value', | |
136 'X-Other-Header': 'Other Value' | |
137 }).then((response) { | |
138 expect(response.statusCode, equals(200)); | |
139 expect(response.body, parse(equals({ | |
140 'method': 'DELETE', | |
141 'path': '/', | 196 'path': '/', |
142 'headers': { | 197 'headers': { |
143 'content-length': ['0'], | 198 'content-length': ['0'], |
144 'x-random-header': ['Value'], | 199 'x-random-header': ['Value'], |
145 'x-other-header': ['Other Value'] | 200 'x-other-header': ['Other Value'] |
146 } | 201 }, |
147 }))); | 202 })))); |
148 }), completes); | 203 }), completes); |
149 }); | |
150 | |
151 test('read', () { | |
152 expect(http.read(serverUrl, headers: { | |
153 'X-Random-Header': 'Value', | |
154 'X-Other-Header': 'Other Value' | |
155 }).then((val) => val), completion(parse(equals({ | |
156 'method': 'GET', | |
157 'path': '/', | |
158 'headers': { | |
159 'content-length': ['0'], | |
160 'x-random-header': ['Value'], | |
161 'x-other-header': ['Other Value'] | |
162 }, | |
163 })))); | |
164 }); | |
165 | |
166 test('read throws an error for a 4** status code', () { | |
167 expect(http.read(serverUrl.resolve('/error')), throwsHttpException); | |
168 }); | |
169 | |
170 test('readBytes', () { | |
171 var future = http.readBytes(serverUrl, headers: { | |
172 'X-Random-Header': 'Value', | |
173 'X-Other-Header': 'Other Value' | |
174 }).then((bytes) => new String.fromCharCodes(bytes)); | |
175 | |
176 expect(future, completion(parse(equals({ | |
177 'method': 'GET', | |
178 'path': '/', | |
179 'headers': { | |
180 'content-length': ['0'], | |
181 'x-random-header': ['Value'], | |
182 'x-other-header': ['Other Value'] | |
183 }, | |
184 })))); | |
185 }); | 204 }); |
186 | 205 |
187 test('readBytes throws an error for a 4** status code', () { | 206 test('readBytes throws an error for a 4** status code', () { |
188 expect(http.readBytes(serverUrl.resolve('/error')), throwsHttpException); | 207 expect(startServer().then((_) { |
| 208 expect(http.readBytes(serverUrl.resolve('/error')), throwsHttpException)
; |
| 209 }), completes); |
189 }); | 210 }); |
190 }); | 211 }); |
191 } | 212 } |
OLD | NEW |