diff --git a/CHANGELOG.md b/CHANGELOG.md index d5aa0d58..a2c4b140 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +# 2.0.0 +Update to work with `client@2`. + # 2.0.0-alpha.4 # 2.0.0-alpha.3 * Update `http` dependency. diff --git a/example/main.dart b/example/main.dart index 1c7ac552..266cdba3 100644 --- a/example/main.dart +++ b/example/main.dart @@ -28,7 +28,7 @@ main() { 'billie': {'jean': 'hee-hee', 'is_my_lover': false} }) ..post('/hello', (req, res) async { - var body = await req.parseBody(); + var body = await req.parseBody().then((_) => req.bodyAsMap); return {'bar': body['foo']}; }) ..get('/gzip', (req, res) async { diff --git a/lib/src/client.dart b/lib/src/client.dart index ac5f3c04..d03506fe 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -1,5 +1,4 @@ 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; @@ -68,59 +67,25 @@ class TestClient extends client.BaseAngelClient { /// Opens a WebSockets connection to the server. This will automatically bind the server /// over HTTP, if it is not already listening. Unfortunately, WebSockets cannot be mocked (yet!). - Future websocket({String path, Duration timeout}) async { - HttpServer http = _http.httpServer; - if (http == null) http = await _http.startServer(); - var url = 'ws://${http.address.address}:${http.port}'; - var cleanPath = (path ?? '/ws')?.replaceAll(_straySlashes, ''); - if (cleanPath?.isNotEmpty == true) url += '/$cleanPath'; - var ws = new _MockWebSockets(this, url); + Future websocket( + {String path: '/ws', Duration timeout}) async { + if (_http.server == null) await _http.startServer(); + var url = _http.uri.replace(scheme: 'ws', path: path); + var ws = new _MockWebSockets(this, url.toString()); await ws.connect(timeout: timeout); return ws; } - Future sendUnstreamed( - String method, url, Map headers, - [body, Encoding encoding]) => - send(method, url, headers, body, encoding).then(http.Response.fromStream); - - Future send(String method, url, Map headers, - [body, Encoding encoding]) async { - var rq = new MockHttpRequest( - method, url is Uri ? url : Uri.parse(url.toString())); - headers?.forEach(rq.headers.add); + Future send(http.BaseRequest request) async { + var rq = new MockHttpRequest(request.method, request.url); + request.headers.forEach(rq.headers.add); if (authToken?.isNotEmpty == true) - rq.headers.set('authorization', 'Bearer $authToken'); + rq.headers.add('authorization', 'Bearer $authToken'); rq..cookies.addAll(cookies)..session.addAll(session); - if (body is Stream>) { - await rq.addStream(body); - } else if (body is List) { - rq.add(body); - } else if (body is Map) { - if (rq.headers.contentType == null || - rq.headers.contentType.mimeType == 'application/json') { - rq - ..headers.contentType = new ContentType('application', 'json') - ..write(json.encode(body.keys.fold>( - {}, (out, k) => out..[k.toString()] = body[k]))); - } else if (rq.headers.contentType?.mimeType == - 'application/x-www-form-urlencoded') { - rq.write(body.keys.fold>( - [], - (out, k) => out - ..add('$k=' + Uri.encodeComponent(body[k].toString()))).join()); - } else { - throw new UnsupportedError( - 'Map bodies can only be sent for requests with the content type application/json or application/x-www-form-urlencoded.'); - } - } else if (body != null) { - rq.write(body); - } - - await rq.close(); + await request.finalize().pipe(rq); await _http.handleRequest(rq); @@ -153,24 +118,6 @@ class TestClient extends client.BaseAngelClient { reasonPhrase: rs.reasonPhrase); } - Future delete(url, {Map headers}) => - sendUnstreamed('DELETE', url, headers); - - Future get(url, {Map headers}) => - sendUnstreamed('GET', url, headers); - - Future head(url, {Map headers}) => - sendUnstreamed('HEAD', url, headers); - - Future patch(url, {body, Map headers}) => - sendUnstreamed('PATCH', url, headers, body); - - Future post(url, {body, Map headers}) => - sendUnstreamed('POST', url, headers, body); - - Future put(url, {body, Map headers}) => - sendUnstreamed('PUT', url, headers, body); - @override String basePath; @@ -205,11 +152,10 @@ class _MockService extends client.BaseAngelService { @override Future send(http.BaseRequest request) { if (app.authToken != null && app.authToken.isNotEmpty) { - request.headers['Authorization'] = 'Bearer ${app.authToken}'; + request.headers['authorization'] ??= 'Bearer ${app.authToken}'; } - return _app.send( - request.method, request.url, request.headers, request.finalize()); + return _app.send(request); } } @@ -225,7 +171,7 @@ class _MockWebSockets extends client.WebSockets { if (app.authToken?.isNotEmpty == true) headers['authorization'] = 'Bearer ${app.authToken}'; - var socket = await WebSocket.connect(basePath, headers: headers); + var socket = await WebSocket.connect(baseUrl.toString(), headers: headers); return new IOWebSocketChannel(socket); } } diff --git a/pubspec.yaml b/pubspec.yaml index e2b49338..ba410501 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -2,15 +2,15 @@ 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-alpha.4 +version: 2.0.0 dependencies: - angel_client: ^2.0.0-alpha + angel_client: ^2.0.0 angel_framework: ^2.0.0-alpha angel_http_exception: ^1.0.0 - angel_validate: ^2.0.0-alpha - angel_websocket: ^2.0.0-alpha + angel_validate: ^2.0.0 + angel_websocket: ^2.0.0 http: ^0.12.0 - matcher: ^0.12.0+2 + matcher: ^0.12.0 mock_request: ^1.0.0 web_socket_channel: ^1.0.0 dev_dependencies: diff --git a/test/simple_test.dart b/test/simple_test.dart index c4056e3f..b75b7c6b 100644 --- a/test/simple_test.dart +++ b/test/simple_test.dart @@ -28,7 +28,7 @@ main() { 'billie': {'jean': 'hee-hee', 'is_my_lover': false} }) ..post('/hello', (req, res) async { - var body = await req.parseBody(); + var body = await req.parseBody().then((_) => req.bodyAsMap); return {'bar': body['foo']}; }) ..get('/gzip', (req, res) async {