diff --git a/CHANGELOG.md b/CHANGELOG.md index a2c4b140..b5d5abcd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ +# 2.0.1 +* Update badge. +* Handle userInfo + basic auth. + # 2.0.0 -Update to work with `client@2`. +* Update to work with `client@2`. # 2.0.0-alpha.4 # 2.0.0-alpha.3 diff --git a/README.md b/README.md index 86e4605c..9e70392b 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # angel_test -[![version 1.0.6](https://img.shields.io/badge/pub-1.0.6-brightgreen.svg)](https://pub.dartlang.org/packages/angel_test) +[![Pub](https://img.shields.io/pub/v/angel_test.svg)](https://pub.dartlang.org/packages/angel_test) [![build status](https://travis-ci.org/angel-dart/test.svg)](https://travis-ci.org/angel-dart/test) Testing utility library for the Angel framework. diff --git a/lib/src/client.dart b/lib/src/client.dart index d03506fe..fc1f78f2 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -1,4 +1,5 @@ import 'dart:async'; +import 'dart:convert'; import 'dart:io'; import 'package:angel_client/base_angel_client.dart' as client; import 'package:angel_client/io.dart' as client; @@ -80,6 +81,19 @@ class TestClient extends client.BaseAngelClient { var rq = new MockHttpRequest(request.method, request.url); request.headers.forEach(rq.headers.add); + if (request.url.userInfo.isNotEmpty) { + // Attempt to send as Basic auth + var encoded = base64Url.encode(utf8.encode(request.url.userInfo)); + rq.headers.add('authorization', 'Basic $encoded'); + } else if (rq.headers.value('authorization')?.startsWith('Basic ') == true) { + var encoded = rq.headers.value('authorization').substring(6); + var decoded = utf8.decode(base64Url.decode(encoded)); + var oldRq = rq; + var newRq = new MockHttpRequest(rq.method, rq.uri.replace(userInfo: decoded)); + oldRq.headers.forEach(newRq.headers.add); + rq = newRq; + } + if (authToken?.isNotEmpty == true) rq.headers.add('authorization', 'Bearer $authToken'); diff --git a/pubspec.yaml b/pubspec.yaml index ba410501..0a9b6867 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -2,7 +2,7 @@ author: Tobe O description: Testing utility library for the Angel framework. Use with package:test. homepage: https://github.com/angel-dart/test.git name: angel_test -version: 2.0.0 +version: 2.0.1 dependencies: angel_client: ^2.0.0 angel_framework: ^2.0.0-alpha diff --git a/test/simple_test.dart b/test/simple_test.dart index b75b7c6b..e9a4e8b6 100644 --- a/test/simple_test.dart +++ b/test/simple_test.dart @@ -1,3 +1,4 @@ +import 'dart:convert'; import 'dart:io'; import 'package:angel_framework/angel_framework.dart'; import 'package:angel_test/angel_test.dart'; @@ -12,6 +13,7 @@ main() { setUp(() async { app = new Angel() ..get('/hello', (req, res) => 'Hello') + ..get('/user_info', (req, res) => {'u': req.uri.userInfo}) ..get( '/error', (req, res) => throw new AngelHttpException.forbidden(message: 'Test') @@ -83,6 +85,26 @@ main() { statusCode: 403, message: 'Test', errors: ['foo', 'bar'])); }); + test('userInfo from Uri', () async { + var url = new Uri(userInfo: 'foo:bar', path: '/user_info'); + print('URL: $url'); + var res = await client.get(url); + print(res.body); + var m = json.decode(res.body) as Map; + expect(m, {'u': 'foo:bar'}); + }); + + test('userInfo from Basic auth header', () async { + var url = new Uri(path: '/user_info'); + print('URL: $url'); + var res = await client.get(url, headers: { + 'authorization': 'Basic ' + (base64Url.encode(utf8.encode('foo:bar'))) + }); + print(res.body); + var m = json.decode(res.body) as Map; + expect(m, {'u': 'foo:bar'}); + }); + test('hasBody', () async { var res = await client.get('/body'); expect(res, hasBody());