This commit is contained in:
Tobe O 2019-01-06 12:41:06 -05:00
parent a3e07034b6
commit db6f753068
5 changed files with 23 additions and 74 deletions

View file

@ -1,3 +1,6 @@
# 2.0.0
Update to work with `client@2`.
# 2.0.0-alpha.4 # 2.0.0-alpha.4
# 2.0.0-alpha.3 # 2.0.0-alpha.3
* Update `http` dependency. * Update `http` dependency.

View file

@ -28,7 +28,7 @@ main() {
'billie': {'jean': 'hee-hee', 'is_my_lover': false} 'billie': {'jean': 'hee-hee', 'is_my_lover': false}
}) })
..post('/hello', (req, res) async { ..post('/hello', (req, res) async {
var body = await req.parseBody(); var body = await req.parseBody().then((_) => req.bodyAsMap);
return {'bar': body['foo']}; return {'bar': body['foo']};
}) })
..get('/gzip', (req, res) async { ..get('/gzip', (req, res) async {

View file

@ -1,5 +1,4 @@
import 'dart:async'; import 'dart:async';
import 'dart:convert';
import 'dart:io'; import 'dart:io';
import 'package:angel_client/base_angel_client.dart' as client; import 'package:angel_client/base_angel_client.dart' as client;
import 'package:angel_client/io.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 /// 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!). /// over HTTP, if it is not already listening. Unfortunately, WebSockets cannot be mocked (yet!).
Future<client.WebSockets> websocket({String path, Duration timeout}) async { Future<client.WebSockets> websocket(
HttpServer http = _http.httpServer; {String path: '/ws', Duration timeout}) async {
if (http == null) http = await _http.startServer(); if (_http.server == null) await _http.startServer();
var url = 'ws://${http.address.address}:${http.port}'; var url = _http.uri.replace(scheme: 'ws', path: path);
var cleanPath = (path ?? '/ws')?.replaceAll(_straySlashes, ''); var ws = new _MockWebSockets(this, url.toString());
if (cleanPath?.isNotEmpty == true) url += '/$cleanPath';
var ws = new _MockWebSockets(this, url);
await ws.connect(timeout: timeout); await ws.connect(timeout: timeout);
return ws; return ws;
} }
Future<http.Response> sendUnstreamed( Future<StreamedResponse> send(http.BaseRequest request) async {
String method, url, Map<String, String> headers, var rq = new MockHttpRequest(request.method, request.url);
[body, Encoding encoding]) => request.headers.forEach(rq.headers.add);
send(method, url, headers, body, encoding).then(http.Response.fromStream);
Future<StreamedResponse> send(String method, url, Map<String, String> headers,
[body, Encoding encoding]) async {
var rq = new MockHttpRequest(
method, url is Uri ? url : Uri.parse(url.toString()));
headers?.forEach(rq.headers.add);
if (authToken?.isNotEmpty == true) if (authToken?.isNotEmpty == true)
rq.headers.set('authorization', 'Bearer $authToken'); rq.headers.add('authorization', 'Bearer $authToken');
rq..cookies.addAll(cookies)..session.addAll(session); rq..cookies.addAll(cookies)..session.addAll(session);
if (body is Stream<List<int>>) { await request.finalize().pipe(rq);
await rq.addStream(body);
} else if (body is List<int>) {
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<Map<String, dynamic>>(
{}, (out, k) => out..[k.toString()] = body[k])));
} else if (rq.headers.contentType?.mimeType ==
'application/x-www-form-urlencoded') {
rq.write(body.keys.fold<List<String>>(
[],
(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 _http.handleRequest(rq); await _http.handleRequest(rq);
@ -153,24 +118,6 @@ class TestClient extends client.BaseAngelClient {
reasonPhrase: rs.reasonPhrase); reasonPhrase: rs.reasonPhrase);
} }
Future<http.Response> delete(url, {Map<String, String> headers}) =>
sendUnstreamed('DELETE', url, headers);
Future<http.Response> get(url, {Map<String, String> headers}) =>
sendUnstreamed('GET', url, headers);
Future<http.Response> head(url, {Map<String, String> headers}) =>
sendUnstreamed('HEAD', url, headers);
Future<http.Response> patch(url, {body, Map<String, String> headers}) =>
sendUnstreamed('PATCH', url, headers, body);
Future<http.Response> post(url, {body, Map<String, String> headers}) =>
sendUnstreamed('POST', url, headers, body);
Future<http.Response> put(url, {body, Map<String, String> headers}) =>
sendUnstreamed('PUT', url, headers, body);
@override @override
String basePath; String basePath;
@ -205,11 +152,10 @@ class _MockService<Id, Data> extends client.BaseAngelService<Id, Data> {
@override @override
Future<StreamedResponse> send(http.BaseRequest request) { Future<StreamedResponse> send(http.BaseRequest request) {
if (app.authToken != null && app.authToken.isNotEmpty) { if (app.authToken != null && app.authToken.isNotEmpty) {
request.headers['Authorization'] = 'Bearer ${app.authToken}'; request.headers['authorization'] ??= 'Bearer ${app.authToken}';
} }
return _app.send( return _app.send(request);
request.method, request.url, request.headers, request.finalize());
} }
} }
@ -225,7 +171,7 @@ class _MockWebSockets extends client.WebSockets {
if (app.authToken?.isNotEmpty == true) if (app.authToken?.isNotEmpty == true)
headers['authorization'] = 'Bearer ${app.authToken}'; 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); return new IOWebSocketChannel(socket);
} }
} }

View file

@ -2,15 +2,15 @@ author: Tobe O <thosakwe@gmail.com>
description: Testing utility library for the Angel framework. Use with package:test. description: Testing utility library for the Angel framework. Use with package:test.
homepage: https://github.com/angel-dart/test.git homepage: https://github.com/angel-dart/test.git
name: angel_test name: angel_test
version: 2.0.0-alpha.4 version: 2.0.0
dependencies: dependencies:
angel_client: ^2.0.0-alpha angel_client: ^2.0.0
angel_framework: ^2.0.0-alpha angel_framework: ^2.0.0-alpha
angel_http_exception: ^1.0.0 angel_http_exception: ^1.0.0
angel_validate: ^2.0.0-alpha angel_validate: ^2.0.0
angel_websocket: ^2.0.0-alpha angel_websocket: ^2.0.0
http: ^0.12.0 http: ^0.12.0
matcher: ^0.12.0+2 matcher: ^0.12.0
mock_request: ^1.0.0 mock_request: ^1.0.0
web_socket_channel: ^1.0.0 web_socket_channel: ^1.0.0
dev_dependencies: dev_dependencies:

View file

@ -28,7 +28,7 @@ main() {
'billie': {'jean': 'hee-hee', 'is_my_lover': false} 'billie': {'jean': 'hee-hee', 'is_my_lover': false}
}) })
..post('/hello', (req, res) async { ..post('/hello', (req, res) async {
var body = await req.parseBody(); var body = await req.parseBody().then((_) => req.bodyAsMap);
return {'bar': body['foo']}; return {'bar': body['foo']};
}) })
..get('/gzip', (req, res) async { ..get('/gzip', (req, res) async {