Line data Source code
1 : import 'dart:async';
2 : import 'dart:io';
3 :
4 : import 'package:agattp/agattp.dart';
5 : import 'package:test/test.dart';
6 :
7 : ///
8 : ///
9 : ///
10 1 : void main() {
11 : ///
12 2 : group('Basic Online Tests', () {
13 2 : test('GET 200', () async {
14 : final AgattpResponse response =
15 3 : await Agattp().get(Uri.parse('https://httpbingo.org/status/200'));
16 :
17 2 : expect(response.statusCode, 200);
18 2 : expect(response.reasonPhrase, 'OK');
19 2 : expect(response.isRedirect, false);
20 2 : expect(response.isPersistentConnection, true);
21 3 : expect(response.headers, isA<HttpHeaders>());
22 2 : expect(response.cookies, isEmpty);
23 2 : expect(response.body, isEmpty);
24 : });
25 :
26 2 : test('GET 201', () async {
27 : final AgattpResponse response =
28 3 : await Agattp().get(Uri.parse('https://httpbingo.org/status/201'));
29 :
30 2 : expect(response.statusCode, 201);
31 2 : expect(response.reasonPhrase, 'Created');
32 2 : expect(response.body, isEmpty);
33 : });
34 :
35 2 : test('GET Json With Content', () async {
36 : const String url = 'https://httpbingo.org/get?test=ok';
37 :
38 : final AgattpJsonResponse<Map<String, dynamic>> response =
39 3 : await Agattp().getJson(Uri.parse(url));
40 :
41 2 : expect(response.statusCode, 200);
42 2 : expect(response.reasonPhrase, 'OK');
43 2 : expect(response.isRedirect, false);
44 2 : expect(response.isPersistentConnection, true);
45 3 : expect(response.headers, isA<HttpHeaders>());
46 2 : expect(response.cookies, isEmpty);
47 3 : expect(response.json['url'], url);
48 : });
49 :
50 2 : test('GET Json No Content', () async {
51 : const String url = 'https://httpbingo.org/status/204';
52 :
53 : final AgattpJsonResponse<void> response =
54 3 : await Agattp().getJson(Uri.parse(url));
55 :
56 2 : expect(response.statusCode, 204);
57 2 : expect(response.reasonPhrase, 'No Content');
58 2 : expect(response.body, isEmpty);
59 : });
60 :
61 2 : test('HEAD', () async {
62 : final AgattpResponse response =
63 3 : await Agattp().head(Uri.parse('https://httpbingo.org/head'));
64 :
65 2 : expect(response.statusCode, 200);
66 2 : expect(response.reasonPhrase, 'OK');
67 2 : expect(response.body, isEmpty);
68 : });
69 :
70 2 : test('HEAD Json', () async {
71 : const String url = 'https://httpbingo.org/head?test=ok';
72 :
73 : final AgattpJsonResponse<Map<String, dynamic>> response =
74 3 : await Agattp().headJson(Uri.parse(url));
75 :
76 2 : expect(response.statusCode, 200);
77 2 : expect(response.reasonPhrase, 'OK');
78 2 : expect(response.body, isEmpty);
79 : });
80 :
81 2 : test('HEAD Json No Content', () async {
82 : const String url = 'https://httpbingo.org/status/204';
83 :
84 : final AgattpJsonResponse<void> response =
85 3 : await Agattp().headJson(Uri.parse(url));
86 :
87 2 : expect(response.statusCode, 204);
88 2 : expect(response.reasonPhrase, 'No Content');
89 2 : expect(response.body, isEmpty);
90 : });
91 :
92 2 : test('POST', () async {
93 2 : final AgattpResponse response = await Agattp().post(
94 1 : Uri.parse('https://httpbingo.org/post'),
95 1 : headers: <String, String>{
96 : HttpHeaders.contentTypeHeader: 'text/plain',
97 : },
98 : body: 'Hello World!',
99 : );
100 :
101 2 : expect(response.statusCode, 200);
102 2 : expect(response.reasonPhrase, 'OK');
103 3 : expect(response.body, contains('"data": "Hello World!"'));
104 : });
105 :
106 2 : test('POST Json With Body', () async {
107 : const String url = 'https://httpbingo.org/post?test=ok';
108 :
109 : const Map<String, dynamic> body = <String, dynamic>{
110 : 'message': 'Hello World!',
111 : };
112 :
113 : final AgattpJsonResponse<Map<String, dynamic>> response =
114 3 : await Agattp().postJson(Uri.parse(url), body: body);
115 :
116 2 : expect(response.statusCode, 200);
117 2 : expect(response.reasonPhrase, 'OK');
118 :
119 1 : final Map<String, dynamic> json = response.json;
120 :
121 2 : expect(json['url'], url);
122 2 : expect(json['json'], body);
123 : });
124 :
125 2 : test('POST Json Without Body', () async {
126 : const String url = 'https://httpbingo.org/post?test=ok';
127 :
128 : final AgattpJsonResponse<Map<String, dynamic>> response =
129 3 : await Agattp().postJson(Uri.parse(url));
130 :
131 2 : expect(response.statusCode, 200);
132 2 : expect(response.reasonPhrase, 'OK');
133 :
134 1 : final Map<String, dynamic> json = response.json;
135 :
136 2 : expect(json['json'], isNull);
137 2 : expect(json['url'], url);
138 : });
139 :
140 2 : test('POST Json No Content', () async {
141 : const String url = 'https://httpbingo.org/status/204';
142 :
143 : final AgattpJsonResponse<void> response =
144 3 : await Agattp().postJson(Uri.parse(url));
145 :
146 2 : expect(response.statusCode, 204);
147 2 : expect(response.reasonPhrase, 'No Content');
148 2 : expect(response.body, isEmpty);
149 : });
150 :
151 2 : test('PUT', () async {
152 2 : final AgattpResponse response = await Agattp().put(
153 1 : Uri.parse('https://httpbingo.org/put'),
154 1 : headers: <String, String>{
155 : HttpHeaders.contentTypeHeader: 'text/plain',
156 : },
157 : body: 'Hello World!',
158 : );
159 :
160 2 : expect(response.statusCode, 200);
161 2 : expect(response.reasonPhrase, 'OK');
162 3 : expect(response.body, contains('"data": "Hello World!"'));
163 : });
164 :
165 2 : test('PUT Json With Body', () async {
166 : const String url = 'https://httpbingo.org/put?test=ok';
167 :
168 : const Map<String, dynamic> body = <String, dynamic>{
169 : 'message': 'Hello World!',
170 : };
171 :
172 : final AgattpJsonResponse<Map<String, dynamic>> response =
173 3 : await Agattp().putJson(Uri.parse(url), body: body);
174 :
175 2 : expect(response.statusCode, 200);
176 2 : expect(response.reasonPhrase, 'OK');
177 :
178 1 : final Map<String, dynamic> json = response.json;
179 :
180 2 : expect(json['json'], body);
181 2 : expect(json['url'], url);
182 : });
183 :
184 2 : test('PUT Json Without Body', () async {
185 : const String url = 'https://httpbingo.org/put?test=ok';
186 :
187 : final AgattpJsonResponse<Map<String, dynamic>> response =
188 3 : await Agattp().putJson(Uri.parse(url));
189 :
190 2 : expect(response.statusCode, 200);
191 2 : expect(response.reasonPhrase, 'OK');
192 :
193 1 : final Map<String, dynamic> json = response.json;
194 :
195 2 : expect(json['json'], isNull);
196 2 : expect(json['url'], url);
197 : });
198 :
199 2 : test('PUT Json No Content', () async {
200 : const String url = 'https://httpbingo.org/status/204';
201 :
202 : final AgattpJsonResponse<void> response =
203 3 : await Agattp().putJson(Uri.parse(url));
204 :
205 2 : expect(response.statusCode, 204);
206 2 : expect(response.reasonPhrase, 'No Content');
207 2 : expect(response.body, isEmpty);
208 : });
209 :
210 2 : test('PATCH', () async {
211 2 : final AgattpResponse response = await Agattp().patch(
212 1 : Uri.parse('https://httpbingo.org/patch'),
213 1 : headers: <String, String>{
214 : HttpHeaders.contentTypeHeader: 'text/plain',
215 : },
216 : body: 'Hello World!',
217 : );
218 :
219 2 : expect(response.statusCode, 200);
220 2 : expect(response.reasonPhrase, 'OK');
221 3 : expect(response.body, contains('"data": "Hello World!"'));
222 : });
223 :
224 2 : test('PATCH Json With Body', () async {
225 : const String url = 'https://httpbingo.org/patch?test=ok';
226 :
227 : const Map<String, dynamic> body = <String, dynamic>{
228 : 'message': 'Hello World!',
229 : };
230 :
231 : final AgattpJsonResponse<Map<String, dynamic>> response =
232 3 : await Agattp().patchJson(Uri.parse(url), body: body);
233 :
234 2 : expect(response.statusCode, 200);
235 2 : expect(response.reasonPhrase, 'OK');
236 :
237 1 : final Map<String, dynamic> json = response.json;
238 :
239 2 : expect(json['json'], body);
240 2 : expect(json['url'], url);
241 : });
242 :
243 2 : test('PATCH Json Without Body', () async {
244 : const String url = 'https://httpbingo.org/patch?test=ok';
245 :
246 : final AgattpJsonResponse<Map<String, dynamic>> response =
247 3 : await Agattp().patchJson(Uri.parse(url));
248 :
249 2 : expect(response.statusCode, 200);
250 2 : expect(response.reasonPhrase, 'OK');
251 :
252 1 : final Map<String, dynamic> json = response.json;
253 :
254 2 : expect(json['json'], isNull);
255 2 : expect(json['url'], url);
256 : });
257 :
258 2 : test('PATCH Json No Content', () async {
259 : const String url = 'https://httpbingo.org/status/204';
260 :
261 : final AgattpJsonResponse<void> response =
262 3 : await Agattp().patchJson(Uri.parse(url));
263 :
264 2 : expect(response.statusCode, 204);
265 2 : expect(response.reasonPhrase, 'No Content');
266 2 : expect(response.body, isEmpty);
267 : });
268 :
269 2 : test('DELETE', () async {
270 2 : final AgattpResponse response = await Agattp().delete(
271 1 : Uri.parse('https://httpbingo.org/delete'),
272 1 : headers: <String, String>{
273 : HttpHeaders.contentTypeHeader: 'text/plain',
274 : },
275 : body: 'Hello World!',
276 : );
277 :
278 2 : expect(response.statusCode, 200);
279 2 : expect(response.reasonPhrase, 'OK');
280 3 : expect(response.body, contains('"data": "Hello World!"'));
281 : });
282 :
283 2 : test('DELETE Json With Body', () async {
284 : const String url = 'https://httpbingo.org/delete?test=ok';
285 :
286 : const Map<String, dynamic> body = <String, dynamic>{
287 : 'message': 'Hello World!',
288 : };
289 :
290 : final AgattpJsonResponse<Map<String, dynamic>> response =
291 3 : await Agattp().deleteJson(Uri.parse(url), body: body);
292 :
293 2 : expect(response.statusCode, 200);
294 2 : expect(response.reasonPhrase, 'OK');
295 :
296 1 : final Map<String, dynamic> json = response.json;
297 :
298 2 : expect(json['json'], body);
299 2 : expect(json['url'], url);
300 : });
301 :
302 2 : test('DELETE Json Without Body', () async {
303 : const String url = 'https://httpbingo.org/delete?test=ok';
304 :
305 : final AgattpJsonResponse<Map<String, dynamic>> response =
306 3 : await Agattp().deleteJson(Uri.parse(url));
307 :
308 2 : expect(response.statusCode, 200);
309 2 : expect(response.reasonPhrase, 'OK');
310 :
311 1 : final Map<String, dynamic> json = response.json;
312 :
313 2 : expect(json['json'], isNull);
314 2 : expect(json['url'], url);
315 : });
316 :
317 2 : test('DELETE Json No Content', () async {
318 : const String url = 'https://httpbingo.org/status/204';
319 :
320 : final AgattpJsonResponse<void> response =
321 3 : await Agattp().deleteJson(Uri.parse(url));
322 :
323 2 : expect(response.statusCode, 204);
324 2 : expect(response.reasonPhrase, 'No Content');
325 2 : expect(response.body, isEmpty);
326 : });
327 :
328 2 : test('Redirect', () async {
329 1 : final AgattpResponse response = await Agattp(
330 : config: const AgattpConfig(followRedirects: false),
331 1 : ).get(
332 1 : Uri.parse('https://httpbingo.org/redirect/1'),
333 : );
334 :
335 2 : expect(response.statusCode, 302);
336 2 : expect(response.reasonPhrase, 'Found');
337 2 : expect(response.isRedirect, true);
338 2 : expect(response.body, isEmpty);
339 : });
340 :
341 2 : test('Bearer Token', () async {
342 : final String token =
343 4 : DateTime.now().microsecondsSinceEpoch.toRadixString(16).toLowerCase();
344 :
345 : final AgattpJsonResponse<Map<String, dynamic>> response =
346 2 : await Agattp.authBearer(token).getJson(
347 1 : Uri.parse('https://httpbingo.org/bearer'),
348 : );
349 :
350 2 : expect(response.statusCode, 200);
351 2 : expect(response.reasonPhrase, 'OK');
352 2 : expect(response.isRedirect, false);
353 3 : expect(response.json['authenticated'], true);
354 3 : expect(response.json['token'], token);
355 : });
356 :
357 2 : test('No Bearer Token', () async {
358 : final AgattpJsonResponse<Map<String, dynamic>> response =
359 3 : await Agattp().getJson(Uri.parse('https://httpbingo.org/bearer'));
360 :
361 2 : expect(response.statusCode, 401);
362 2 : expect(response.reasonPhrase, 'Unauthorized');
363 2 : expect(response.isRedirect, false);
364 2 : expect(response.json, isNotEmpty);
365 : });
366 :
367 2 : test('Basic Auth Success', () async {
368 : const String user = 'user';
369 : const String pass = 'pass';
370 :
371 : final AgattpJsonResponse<Map<String, dynamic>> response =
372 2 : await Agattp.authBasic(username: user, password: pass).getJson(
373 1 : Uri.parse('https://httpbingo.org/basic-auth/$user/$pass'),
374 : );
375 :
376 2 : expect(response.statusCode, 200);
377 2 : expect(response.reasonPhrase, 'OK');
378 2 : expect(response.isRedirect, false);
379 3 : expect(response.json['authorized'], true);
380 3 : expect(response.json['user'], user);
381 : });
382 :
383 2 : test('Basic Auth Fail', () async {
384 : const String user = 'user';
385 : const String pass = 'pass';
386 :
387 : final AgattpJsonResponse<Map<String, dynamic>> response =
388 2 : await Agattp.authBasic(username: user, password: pass).getJson(
389 1 : Uri.parse('https://httpbingo.org/basic-auth/$user/a1$pass'),
390 : );
391 :
392 2 : expect(response.statusCode, 401);
393 2 : expect(response.reasonPhrase, 'Unauthorized');
394 2 : expect(response.isRedirect, false);
395 3 : expect(response.json['authorized'], false);
396 3 : expect(response.json['user'], user);
397 : });
398 :
399 2 : test('Request Headers', () async {
400 : const String key = 'X-Test';
401 : const String value = 'ok';
402 :
403 : final AgattpJsonResponse<Map<String, dynamic>> response =
404 2 : await Agattp().getJson(
405 1 : Uri.parse('https://httpbingo.org/headers'),
406 1 : extraHeaders: <String, String>{
407 : key: value,
408 : },
409 : );
410 :
411 2 : expect(response.statusCode, 200);
412 2 : expect(response.reasonPhrase, 'OK');
413 2 : expect(response.isRedirect, false);
414 4 : expect(response.json['headers'] is Map<String, dynamic>, true);
415 :
416 2 : final Map<String, dynamic> headers = response.json['headers'];
417 :
418 3 : expect(headers[key], <String>[value]);
419 : });
420 :
421 2 : test('Response Headers', () async {
422 : final AgattpJsonResponse<Map<String, dynamic>> response =
423 3 : await Agattp().getJson(Uri.parse('https://httpbingo.org/headers'));
424 :
425 2 : expect(response.statusCode, 200);
426 2 : expect(response.reasonPhrase, 'OK');
427 2 : expect(response.isRedirect, false);
428 2 : expect(response.isPersistentConnection, true);
429 3 : expect(response.headers, isA<HttpHeaders>());
430 2 : expect(response.cookies, isEmpty);
431 :
432 1 : expect(
433 2 : response.headers[HttpHeaders.accessControlAllowCredentialsHeader],
434 1 : <String>['true'],
435 : );
436 :
437 1 : expect(
438 2 : response.headers[HttpHeaders.accessControlAllowOriginHeader],
439 1 : <String>['*'],
440 : );
441 :
442 1 : expect(
443 2 : response.headers[HttpHeaders.transferEncodingHeader],
444 1 : <String>['chunked'],
445 : );
446 :
447 1 : expect(
448 2 : response.headers[HttpHeaders.contentEncodingHeader],
449 1 : <String>['gzip'],
450 : );
451 :
452 1 : expect(
453 2 : response.headers[HttpHeaders.contentTypeHeader],
454 1 : <String>['application/json; charset=utf-8'],
455 : );
456 : });
457 :
458 2 : test('Timeout', () async {
459 : try {
460 : /// Should have thrown TimeoutException
461 2 : await Agattp().get(
462 1 : Uri.parse('https://httpbingo.org/delay/5'),
463 1 : headers: <String, String>{
464 : HttpHeaders.contentTypeHeader: 'text/plain',
465 : },
466 : timeout: const Duration(milliseconds: 2000),
467 : );
468 1 : } on Exception catch (e) {
469 2 : expect(e, isA<TimeoutException>());
470 : }
471 : });
472 :
473 2 : test('Digest MD5 Auth Get', () async {
474 : const String username = 'user';
475 : const String password = 'pass';
476 : const String algorithm = 'MD5';
477 :
478 : const String url = 'https://httpbingo.org'
479 : '/digest-auth/auth/$username/$password/$algorithm';
480 :
481 : final AgattpAuthDigest authDigest =
482 1 : AgattpAuthDigest(username: username, password: password);
483 :
484 2 : expect(authDigest.ready, false);
485 :
486 1 : final Agattp agattpDigest = Agattp(
487 1 : config: AgattpConfig(auth: authDigest),
488 : );
489 :
490 : final AgattpJsonResponse<Map<String, dynamic>> response1 =
491 2 : await agattpDigest.getJson(Uri.parse(url));
492 :
493 2 : expect(response1.statusCode, 200);
494 2 : expect(response1.reasonPhrase, 'OK');
495 2 : expect(response1.isRedirect, false);
496 2 : expect(response1.isPersistentConnection, true);
497 3 : expect(response1.json['authorized'], true);
498 3 : expect(response1.json['user'], username);
499 :
500 2 : expect(authDigest.ready, true);
501 :
502 : final AgattpJsonResponse<Map<String, dynamic>> response2 =
503 2 : await agattpDigest.getJson(Uri.parse(url));
504 :
505 2 : expect(response2.statusCode, 200);
506 2 : expect(response2.reasonPhrase, 'OK');
507 2 : expect(response2.isRedirect, false);
508 2 : expect(response2.isPersistentConnection, true);
509 3 : expect(response2.json['authorized'], true);
510 3 : expect(response2.json['user'], username);
511 :
512 1 : authDigest.reset();
513 :
514 2 : expect(authDigest.ready, false);
515 :
516 : final AgattpJsonResponse<Map<String, dynamic>> response3 =
517 2 : await agattpDigest.getJson(Uri.parse(url));
518 :
519 2 : expect(response3.statusCode, 200);
520 2 : expect(response3.reasonPhrase, 'OK');
521 2 : expect(response3.isRedirect, false);
522 2 : expect(response3.isPersistentConnection, true);
523 3 : expect(response3.json['authorized'], true);
524 3 : expect(response3.json['user'], username);
525 :
526 2 : expect(authDigest.ready, true);
527 : });
528 :
529 2 : test('Digest MD5 Auth Post', () async {
530 : const String username = 'user';
531 : const String password = 'pass';
532 : const String algorithm = 'MD5';
533 :
534 : const String url = 'https://httpbingo.org'
535 : '/digest-auth/auth/$username/$password/$algorithm';
536 :
537 : final AgattpJsonResponse<Map<String, dynamic>> response =
538 1 : await Agattp.authDigest(
539 : username: username,
540 : password: password,
541 2 : ).postJson(Uri.parse(url));
542 :
543 2 : expect(response.statusCode, 200);
544 2 : expect(response.reasonPhrase, 'OK');
545 2 : expect(response.isRedirect, false);
546 2 : expect(response.isPersistentConnection, true);
547 3 : expect(response.json['authorized'], true);
548 3 : expect(response.json['user'], username);
549 : });
550 :
551 2 : test('Digest MD5 Auth Put', () async {
552 : const String username = 'user';
553 : const String password = 'pass';
554 : const String algorithm = 'MD5';
555 :
556 : const String url = 'https://httpbingo.org'
557 : '/digest-auth/auth/$username/$password/$algorithm';
558 :
559 : final AgattpJsonResponse<Map<String, dynamic>> response =
560 1 : await Agattp.authDigest(
561 : username: username,
562 : password: password,
563 2 : ).putJson(Uri.parse(url));
564 :
565 2 : expect(response.statusCode, 200);
566 2 : expect(response.reasonPhrase, 'OK');
567 2 : expect(response.isRedirect, false);
568 2 : expect(response.isPersistentConnection, true);
569 3 : expect(response.json['authorized'], true);
570 3 : expect(response.json['user'], username);
571 : });
572 :
573 2 : test('Digest MD5 Auth Delete', () async {
574 : const String username = 'user';
575 : const String password = 'pass';
576 : const String algorithm = 'MD5';
577 :
578 : const String url = 'https://httpbingo.org'
579 : '/digest-auth/auth/$username/$password/$algorithm';
580 :
581 : final AgattpJsonResponse<Map<String, dynamic>> response =
582 1 : await Agattp.authDigest(
583 : username: username,
584 : password: password,
585 2 : ).deleteJson(Uri.parse(url));
586 :
587 2 : expect(response.statusCode, 200);
588 2 : expect(response.reasonPhrase, 'OK');
589 2 : expect(response.isRedirect, false);
590 2 : expect(response.isPersistentConnection, true);
591 3 : expect(response.json['authorized'], true);
592 3 : expect(response.json['user'], username);
593 : });
594 : });
595 :
596 2 : test('Digest SHA-256 Auth Get', () async {
597 : const String username = 'user';
598 : const String password = 'pass';
599 : const String algorithm = 'SHA-256';
600 :
601 : const String url = 'https://httpbingo.org'
602 : '/digest-auth/auth/$username/$password/$algorithm';
603 :
604 : final AgattpAuthDigest authDigest =
605 1 : AgattpAuthDigest(username: username, password: password);
606 :
607 2 : expect(authDigest.ready, false);
608 :
609 1 : final Agattp agattpDigest = Agattp(
610 1 : config: AgattpConfig(auth: authDigest),
611 : );
612 :
613 : final AgattpJsonResponse<Map<String, dynamic>> response1 =
614 2 : await agattpDigest.getJson(Uri.parse(url));
615 :
616 2 : expect(response1.statusCode, 200);
617 2 : expect(response1.reasonPhrase, 'OK');
618 2 : expect(response1.isRedirect, false);
619 2 : expect(response1.isPersistentConnection, true);
620 3 : expect(response1.json['authorized'], true);
621 3 : expect(response1.json['user'], username);
622 :
623 2 : expect(authDigest.ready, true);
624 :
625 : final AgattpJsonResponse<Map<String, dynamic>> response2 =
626 2 : await agattpDigest.getJson(Uri.parse(url));
627 :
628 2 : expect(response2.statusCode, 200);
629 2 : expect(response2.reasonPhrase, 'OK');
630 2 : expect(response2.isRedirect, false);
631 2 : expect(response2.isPersistentConnection, true);
632 3 : expect(response2.json['authorized'], true);
633 3 : expect(response2.json['user'], username);
634 :
635 1 : authDigest.reset();
636 :
637 2 : expect(authDigest.ready, false);
638 :
639 : final AgattpJsonResponse<Map<String, dynamic>> response3 =
640 2 : await agattpDigest.getJson(Uri.parse(url));
641 :
642 2 : expect(response3.statusCode, 200);
643 2 : expect(response3.reasonPhrase, 'OK');
644 2 : expect(response3.isRedirect, false);
645 2 : expect(response3.isPersistentConnection, true);
646 3 : expect(response3.json['authorized'], true);
647 3 : expect(response3.json['user'], username);
648 :
649 2 : expect(authDigest.ready, true);
650 : });
651 :
652 2 : test('Digest SHA-256 Auth Post', () async {
653 : const String username = 'user';
654 : const String password = 'pass';
655 : const String algorithm = 'SHA-256';
656 :
657 : const String url = 'https://httpbingo.org'
658 : '/digest-auth/auth/$username/$password/$algorithm';
659 :
660 : final AgattpJsonResponse<Map<String, dynamic>> response =
661 1 : await Agattp.authDigest(
662 : username: username,
663 : password: password,
664 2 : ).postJson(Uri.parse(url));
665 :
666 2 : expect(response.statusCode, 200);
667 2 : expect(response.reasonPhrase, 'OK');
668 2 : expect(response.isRedirect, false);
669 2 : expect(response.isPersistentConnection, true);
670 3 : expect(response.json['authorized'], true);
671 3 : expect(response.json['user'], username);
672 : });
673 :
674 2 : test('Digest SHA-256 Auth Put', () async {
675 : const String username = 'user';
676 : const String password = 'pass';
677 : const String algorithm = 'SHA-256';
678 :
679 : const String url = 'https://httpbingo.org'
680 : '/digest-auth/auth/$username/$password/$algorithm';
681 :
682 : final AgattpJsonResponse<Map<String, dynamic>> response =
683 2 : await Agattp.authDigest(username: username, password: password).putJson(
684 1 : Uri.parse(url),
685 : );
686 :
687 2 : expect(response.statusCode, 200);
688 2 : expect(response.reasonPhrase, 'OK');
689 2 : expect(response.isRedirect, false);
690 2 : expect(response.isPersistentConnection, true);
691 3 : expect(response.json['authorized'], true);
692 3 : expect(response.json['user'], username);
693 : });
694 :
695 2 : test('Digest SHA-256 Auth Delete', () async {
696 : const String username = 'user';
697 : const String password = 'pass';
698 : const String algorithm = 'SHA-256';
699 :
700 : const String url = 'https://httpbingo.org'
701 : '/digest-auth/auth/$username/$password/$algorithm';
702 :
703 : final AgattpJsonResponse<Map<String, dynamic>> response =
704 1 : await Agattp.authDigest(
705 : username: username,
706 : password: password,
707 2 : ).deleteJson(Uri.parse(url));
708 :
709 2 : expect(response.statusCode, 200);
710 2 : expect(response.reasonPhrase, 'OK');
711 2 : expect(response.isRedirect, false);
712 2 : expect(response.isPersistentConnection, true);
713 3 : expect(response.json['authorized'], true);
714 3 : expect(response.json['user'], username);
715 : });
716 :
717 2 : test('Digest Auth Error', () async {
718 : const String username = 'user';
719 : const String password = 'pass';
720 :
721 : const String url = 'https://httpbingo.org/deny';
722 :
723 : try {
724 : /// Should have thrown Exception
725 2 : await Agattp.authDigest(username: username, password: password).getJson(
726 1 : Uri.parse(url),
727 : );
728 1 : } on Exception catch (e) {
729 2 : expect(e.toString(), 'Exception: WWW-Authenticate header not found');
730 : }
731 : });
732 : }
|