Patched supportShelf to support 1.1.x

This commit is contained in:
Tobe O 2018-01-09 09:44:59 -05:00
parent 47b4ef3546
commit 36f238aa77
5 changed files with 35 additions and 23 deletions

View file

@ -56,11 +56,10 @@ Future<shelf.Request> convertRequest(RequestContext request,
/// In addition, the response's context will be available in `angelResponse.properties`
/// as `shelf_context`.
Future mergeShelfResponse(
shelf.Response shelfResponse, ResponseContext angelResponse) async {
shelf.Response shelfResponse, ResponseContext angelResponse) {
angelResponse.headers.addAll(shelfResponse.headers);
angelResponse.statusCode = shelfResponse.statusCode;
angelResponse.properties['shelf_context'] = shelfResponse.context;
angelResponse.properties['shelf_response'] = shelfResponse;
await shelfResponse.read().forEach(angelResponse.buffer.add);
angelResponse.end();
return shelfResponse.read().pipe(angelResponse);
}

View file

@ -25,10 +25,14 @@ AngelConfigurer supportShelf() {
res.serializer = (val) {
if (val is! shelf.Response) return oldSerializer(val);
res.properties['shelf_response'] = val;
mergeShelfResponse(val, res);
return ''; // Write nothing
};
return true;
});
/*
// Merge shelf response if necessary
app.responseFinalizers.add((RequestContext req, ResponseContext res) async {
if (res.properties.containsKey('shelf_response')) {
@ -36,5 +40,6 @@ AngelConfigurer supportShelf() {
await mergeShelfResponse(shelfResponse, res);
}
});
*/
};
}

View file

@ -1,15 +1,15 @@
author: "Tobe O <thosakwe@gmail.com>"
description: "Shelf interop with Angel."
homepage: "https://github.com/angel-dart/shelf"
name: "angel_shelf"
version: "1.2.0"
author: Tobe O <thosakwe@gmail.com>
description: Shelf interop with Angel.
homepage: https://github.com/angel-dart/shelf
name: angel_shelf
version: 1.2.0
dependencies:
angel_framework: ">=1.1.0-alpha <2.0.0"
shelf: "^0.6.0"
angel_framework: >=1.1.0-alpha <2.0.0
shelf: ^0.6.0
dev_dependencies:
angel_test: "^1.1.0"
console: "^2.2.4"
shelf_proxy: "^0.1.0"
test: "^0.12.0"
angel_test: ^1.1.0
console: ^2.2.4
shelf_proxy: ^0.1.0
test: ^0.12.0
environment:
sdk: ">=1.19.0"

View file

@ -1,5 +1,6 @@
import 'package:console/console.dart';
import 'package:logging/logging.dart';
import 'package:stack_trace/stack_trace.dart';
/// Prints the contents of a [LogRecord] with pretty colors.
void prettyLog(LogRecord record) {
@ -10,7 +11,7 @@ void prettyLog(LogRecord record) {
if (record.error != null)
pen(record.error.toString());
if (record.stackTrace != null)
pen(record.stackTrace.toString());
pen(new Chain.forTrace(record.stackTrace).terse.toString());
pen();
}

View file

@ -3,6 +3,7 @@ import 'package:angel_shelf/angel_shelf.dart';
import 'package:angel_test/angel_test.dart';
import 'package:logging/logging.dart';
import 'package:shelf/shelf.dart' as shelf;
import 'package:stack_trace/stack_trace.dart';
import 'package:test/test.dart';
import 'pretty_logging.dart';
@ -11,6 +12,7 @@ main() {
setUp(() async {
var app = new Angel()..lazyParseBodies = true;
await app.configure(supportShelf());
app.get('/inject', (shelf.Request request) {
print('URL of injected request: ${request.url.path}');
@ -21,7 +23,6 @@ main() {
return new shelf.Response.ok('world');
});
await app.configure(supportShelf());
app.logger = new Logger.detached('angel')..onRecord.listen(prettyLog);
client = await connectTo(app);
});
@ -29,14 +30,20 @@ main() {
tearDown(() => client.close());
test('injected into request', () async {
var response = await client.get('/inject');
var response = await client.get('/inject', headers: {'accept': 'application/json'});
print('Response: ${response.body}');
expect(response, isJson({'inject': true}));
});
test('can return shelf response', () async {
var response = await client.get('/hello');
print('Response: ${response.body}');
expect(response, hasBody('world'));
test('can return shelf response', () {
return Chain.capture(() async {
var response = await client.get('/hello', headers: {'accept': 'application/json'});
print('Response: ${response.body}');
expect(response, hasBody('world'));
}, onError: (e, chain) {
print(e);
print(chain.terse);
expect(0, 1);
});
});
}