| 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 'package:http/http.dart' as http; | 7 import 'package:http/http.dart' as http; |
| 8 import 'package:unittest/unittest.dart'; | 8 import 'package:unittest/unittest.dart'; |
| 9 | 9 |
| 10 import 'utils.dart'; | 10 import 'utils.dart'; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 }))); | 43 }))); |
| 44 }), completes); | 44 }), completes); |
| 45 }), completes); | 45 }), completes); |
| 46 }); | 46 }); |
| 47 | 47 |
| 48 test('post', () { | 48 test('post', () { |
| 49 expect(startServer().then((_) { | 49 expect(startServer().then((_) { |
| 50 expect(http.post(serverUrl, headers: { | 50 expect(http.post(serverUrl, headers: { |
| 51 'X-Random-Header': 'Value', | 51 'X-Random-Header': 'Value', |
| 52 'X-Other-Header': 'Other Value', | 52 'X-Other-Header': 'Other Value', |
| 53 'Content-Type': 'text/plain', |
| 54 'User-Agent': 'Dart' |
| 55 }).then((response) { |
| 56 expect(response.statusCode, equals(200)); |
| 57 expect(response.body, parse(equals({ |
| 58 'method': 'POST', |
| 59 'path': '/', |
| 60 'headers': { |
| 61 'accept-encoding': ['gzip'], |
| 62 'content-length': ['0'], |
| 63 'content-type': ['text/plain'], |
| 64 'user-agent': ['Dart'], |
| 65 'x-random-header': ['Value'], |
| 66 'x-other-header': ['Other Value'] |
| 67 } |
| 68 }))); |
| 69 }), completes); |
| 70 }), completes); |
| 71 }); |
| 72 |
| 73 test('post with string', () { |
| 74 expect(startServer().then((_) { |
| 75 expect(http.post(serverUrl, headers: { |
| 76 'X-Random-Header': 'Value', |
| 77 'X-Other-Header': 'Other Value', |
| 53 'User-Agent': 'Dart' | 78 'User-Agent': 'Dart' |
| 54 }, fields: { | 79 }, body: 'request body').then((response) { |
| 80 expect(response.statusCode, equals(200)); |
| 81 expect(response.body, parse(equals({ |
| 82 'method': 'POST', |
| 83 'path': '/', |
| 84 'headers': { |
| 85 'content-type': ['text/plain; charset=utf-8'], |
| 86 'content-length': ['12'], |
| 87 'accept-encoding': ['gzip'], |
| 88 'user-agent': ['Dart'], |
| 89 'x-random-header': ['Value'], |
| 90 'x-other-header': ['Other Value'] |
| 91 }, |
| 92 'body': 'request body' |
| 93 }))); |
| 94 }), completes); |
| 95 }), completes); |
| 96 }); |
| 97 |
| 98 test('post with bytes', () { |
| 99 expect(startServer().then((_) { |
| 100 expect(http.post(serverUrl, headers: { |
| 101 'X-Random-Header': 'Value', |
| 102 'X-Other-Header': 'Other Value', |
| 103 'User-Agent': 'Dart' |
| 104 }, body: [104, 101, 108, 108, 111]).then((response) { |
| 105 expect(response.statusCode, equals(200)); |
| 106 expect(response.body, parse(equals({ |
| 107 'method': 'POST', |
| 108 'path': '/', |
| 109 'headers': { |
| 110 'content-length': ['5'], |
| 111 'accept-encoding': ['gzip'], |
| 112 'user-agent': ['Dart'], |
| 113 'x-random-header': ['Value'], |
| 114 'x-other-header': ['Other Value'] |
| 115 }, |
| 116 'body': [104, 101, 108, 108, 111] |
| 117 }))); |
| 118 }), completes); |
| 119 }), completes); |
| 120 }); |
| 121 |
| 122 test('post with fields', () { |
| 123 expect(startServer().then((_) { |
| 124 expect(http.post(serverUrl, headers: { |
| 125 'X-Random-Header': 'Value', |
| 126 'X-Other-Header': 'Other Value', |
| 127 'User-Agent': 'Dart' |
| 128 }, body: { |
| 55 'some-field': 'value', | 129 'some-field': 'value', |
| 56 'other-field': 'other value' | 130 'other-field': 'other value' |
| 57 }).then((response) { | 131 }).then((response) { |
| 58 expect(response.statusCode, equals(200)); | 132 expect(response.statusCode, equals(200)); |
| 59 expect(response.body, parse(equals({ | 133 expect(response.body, parse(equals({ |
| 60 'method': 'POST', | 134 'method': 'POST', |
| 61 'path': '/', | 135 'path': '/', |
| 62 'headers': { | 136 'headers': { |
| 63 'content-type': [ | 137 'content-type': [ |
| 64 'application/x-www-form-urlencoded; charset=utf-8' | 138 'application/x-www-form-urlencoded; charset=utf-8' |
| 65 ], | 139 ], |
| 66 'content-length': ['40'], | 140 'content-length': ['40'], |
| 67 'accept-encoding': ['gzip'], | 141 'accept-encoding': ['gzip'], |
| 68 'user-agent': ['Dart'], | 142 'user-agent': ['Dart'], |
| 69 'x-random-header': ['Value'], | 143 'x-random-header': ['Value'], |
| 70 'x-other-header': ['Other Value'] | 144 'x-other-header': ['Other Value'] |
| 71 }, | 145 }, |
| 72 'body': 'some-field=value&other-field=other+value' | 146 'body': 'some-field=value&other-field=other+value' |
| 73 }))); | 147 }))); |
| 74 }), completes); | 148 }), completes); |
| 75 }), completes); | 149 }), completes); |
| 76 }); | 150 }); |
| 77 | 151 |
| 78 test('post without fields', () { | 152 test('put', () { |
| 79 expect(startServer().then((_) { | 153 expect(startServer().then((_) { |
| 80 expect(http.post(serverUrl, headers: { | 154 expect(http.put(serverUrl, headers: { |
| 81 'X-Random-Header': 'Value', | 155 'X-Random-Header': 'Value', |
| 82 'X-Other-Header': 'Other Value', | 156 'X-Other-Header': 'Other Value', |
| 83 'Content-Type': 'text/plain', | 157 'Content-Type': 'text/plain', |
| 84 'User-Agent': 'Dart' | 158 'User-Agent': 'Dart' |
| 85 }).then((response) { | 159 }).then((response) { |
| 86 expect(response.statusCode, equals(200)); | 160 expect(response.statusCode, equals(200)); |
| 87 expect(response.body, parse(equals({ | 161 expect(response.body, parse(equals({ |
| 88 'method': 'POST', | 162 'method': 'PUT', |
| 89 'path': '/', | 163 'path': '/', |
| 90 'headers': { | 164 'headers': { |
| 91 'accept-encoding': ['gzip'], | 165 'accept-encoding': ['gzip'], |
| 92 'content-length': ['0'], | 166 'content-length': ['0'], |
| 93 'content-type': ['text/plain'], | 167 'content-type': ['text/plain'], |
| 94 'user-agent': ['Dart'], | 168 'user-agent': ['Dart'], |
| 95 'x-random-header': ['Value'], | 169 'x-random-header': ['Value'], |
| 96 'x-other-header': ['Other Value'] | 170 'x-other-header': ['Other Value'] |
| 97 } | 171 } |
| 98 }))); | 172 }))); |
| 99 }), completes); | 173 }), completes); |
| 100 }), completes); | 174 }), completes); |
| 101 }); | 175 }); |
| 102 | 176 |
| 103 test('put', () { | 177 test('put with string', () { |
| 104 expect(startServer().then((_) { | 178 expect(startServer().then((_) { |
| 105 expect(http.put(serverUrl, headers: { | 179 expect(http.put(serverUrl, headers: { |
| 106 'X-Random-Header': 'Value', | 180 'X-Random-Header': 'Value', |
| 107 'X-Other-Header': 'Other Value', | 181 'X-Other-Header': 'Other Value', |
| 108 'User-Agent': 'Dart' | 182 'User-Agent': 'Dart' |
| 109 }, fields: { | 183 }, body: 'request body').then((response) { |
| 184 expect(response.statusCode, equals(200)); |
| 185 expect(response.body, parse(equals({ |
| 186 'method': 'PUT', |
| 187 'path': '/', |
| 188 'headers': { |
| 189 'content-type': ['text/plain; charset=utf-8'], |
| 190 'content-length': ['12'], |
| 191 'accept-encoding': ['gzip'], |
| 192 'user-agent': ['Dart'], |
| 193 'x-random-header': ['Value'], |
| 194 'x-other-header': ['Other Value'] |
| 195 }, |
| 196 'body': 'request body' |
| 197 }))); |
| 198 }), completes); |
| 199 }), completes); |
| 200 }); |
| 201 |
| 202 test('put with bytes', () { |
| 203 expect(startServer().then((_) { |
| 204 expect(http.put(serverUrl, headers: { |
| 205 'X-Random-Header': 'Value', |
| 206 'X-Other-Header': 'Other Value', |
| 207 'User-Agent': 'Dart' |
| 208 }, body: [104, 101, 108, 108, 111]).then((response) { |
| 209 expect(response.statusCode, equals(200)); |
| 210 expect(response.body, parse(equals({ |
| 211 'method': 'PUT', |
| 212 'path': '/', |
| 213 'headers': { |
| 214 'content-length': ['5'], |
| 215 'accept-encoding': ['gzip'], |
| 216 'user-agent': ['Dart'], |
| 217 'x-random-header': ['Value'], |
| 218 'x-other-header': ['Other Value'] |
| 219 }, |
| 220 'body': [104, 101, 108, 108, 111] |
| 221 }))); |
| 222 }), completes); |
| 223 }), completes); |
| 224 }); |
| 225 |
| 226 test('put with fields', () { |
| 227 expect(startServer().then((_) { |
| 228 expect(http.put(serverUrl, headers: { |
| 229 'X-Random-Header': 'Value', |
| 230 'X-Other-Header': 'Other Value', |
| 231 'User-Agent': 'Dart' |
| 232 }, body: { |
| 110 'some-field': 'value', | 233 'some-field': 'value', |
| 111 'other-field': 'other value' | 234 'other-field': 'other value' |
| 112 }).then((response) { | 235 }).then((response) { |
| 113 expect(response.statusCode, equals(200)); | 236 expect(response.statusCode, equals(200)); |
| 114 expect(response.body, parse(equals({ | 237 expect(response.body, parse(equals({ |
| 115 'method': 'PUT', | 238 'method': 'PUT', |
| 116 'path': '/', | 239 'path': '/', |
| 117 'headers': { | 240 'headers': { |
| 118 'content-type': [ | 241 'content-type': [ |
| 119 'application/x-www-form-urlencoded; charset=utf-8' | 242 'application/x-www-form-urlencoded; charset=utf-8' |
| 120 ], | 243 ], |
| 244 'content-length': ['40'], |
| 121 'accept-encoding': ['gzip'], | 245 'accept-encoding': ['gzip'], |
| 122 'content-length': ['40'], | |
| 123 'user-agent': ['Dart'], | 246 'user-agent': ['Dart'], |
| 124 'x-random-header': ['Value'], | 247 'x-random-header': ['Value'], |
| 125 'x-other-header': ['Other Value'] | 248 'x-other-header': ['Other Value'] |
| 126 }, | 249 }, |
| 127 'body': 'some-field=value&other-field=other+value' | 250 'body': 'some-field=value&other-field=other+value' |
| 128 }))); | 251 }))); |
| 129 }), completes); | 252 }), completes); |
| 130 }), completes); | 253 }), completes); |
| 131 }); | |
| 132 | |
| 133 test('put without fields', () { | |
| 134 expect(startServer().then((_) { | |
| 135 expect(http.put(serverUrl, headers: { | |
| 136 'X-Random-Header': 'Value', | |
| 137 'X-Other-Header': 'Other Value', | |
| 138 'Content-Type': 'text/plain', | |
| 139 'User-Agent': 'Dart' | |
| 140 }).then((response) { | |
| 141 expect(response.statusCode, equals(200)); | |
| 142 expect(response.body, parse(equals({ | |
| 143 'method': 'PUT', | |
| 144 'path': '/', | |
| 145 'headers': { | |
| 146 'content-length': ['0'], | |
| 147 'accept-encoding': ['gzip'], | |
| 148 'content-type': ['text/plain'], | |
| 149 'user-agent': ['Dart'], | |
| 150 'x-random-header': ['Value'], | |
| 151 'x-other-header': ['Other Value'] | |
| 152 } | |
| 153 }))); | |
| 154 }), completes); | |
| 155 }), completes); | |
| 156 }); | 254 }); |
| 157 | 255 |
| 158 test('delete', () { | 256 test('delete', () { |
| 159 expect(startServer().then((_) { | 257 expect(startServer().then((_) { |
| 160 expect(http.delete(serverUrl, headers: { | 258 expect(http.delete(serverUrl, headers: { |
| 161 'X-Random-Header': 'Value', | 259 'X-Random-Header': 'Value', |
| 162 'X-Other-Header': 'Other Value', | 260 'X-Other-Header': 'Other Value', |
| 163 'User-Agent': 'Dart' | 261 'User-Agent': 'Dart' |
| 164 }).then((response) { | 262 }).then((response) { |
| 165 expect(response.statusCode, equals(200)); | 263 expect(response.statusCode, equals(200)); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 }), completes); | 324 }), completes); |
| 227 }); | 325 }); |
| 228 | 326 |
| 229 test('readBytes throws an error for a 4** status code', () { | 327 test('readBytes throws an error for a 4** status code', () { |
| 230 expect(startServer().then((_) { | 328 expect(startServer().then((_) { |
| 231 expect(http.readBytes(serverUrl.resolve('/error')), throwsHttpException)
; | 329 expect(http.readBytes(serverUrl.resolve('/error')), throwsHttpException)
; |
| 232 }), completes); | 330 }), completes); |
| 233 }); | 331 }); |
| 234 }); | 332 }); |
| 235 } | 333 } |
| OLD | NEW |