Patched supportShelf to support 1.1.x
This commit is contained in:
parent
47b4ef3546
commit
36f238aa77
5 changed files with 35 additions and 23 deletions
|
@ -56,11 +56,10 @@ Future<shelf.Request> convertRequest(RequestContext request,
|
||||||
/// In addition, the response's context will be available in `angelResponse.properties`
|
/// In addition, the response's context will be available in `angelResponse.properties`
|
||||||
/// as `shelf_context`.
|
/// as `shelf_context`.
|
||||||
Future mergeShelfResponse(
|
Future mergeShelfResponse(
|
||||||
shelf.Response shelfResponse, ResponseContext angelResponse) async {
|
shelf.Response shelfResponse, ResponseContext angelResponse) {
|
||||||
angelResponse.headers.addAll(shelfResponse.headers);
|
angelResponse.headers.addAll(shelfResponse.headers);
|
||||||
angelResponse.statusCode = shelfResponse.statusCode;
|
angelResponse.statusCode = shelfResponse.statusCode;
|
||||||
angelResponse.properties['shelf_context'] = shelfResponse.context;
|
angelResponse.properties['shelf_context'] = shelfResponse.context;
|
||||||
angelResponse.properties['shelf_response'] = shelfResponse;
|
angelResponse.properties['shelf_response'] = shelfResponse;
|
||||||
await shelfResponse.read().forEach(angelResponse.buffer.add);
|
return shelfResponse.read().pipe(angelResponse);
|
||||||
angelResponse.end();
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,10 +25,14 @@ AngelConfigurer supportShelf() {
|
||||||
res.serializer = (val) {
|
res.serializer = (val) {
|
||||||
if (val is! shelf.Response) return oldSerializer(val);
|
if (val is! shelf.Response) return oldSerializer(val);
|
||||||
res.properties['shelf_response'] = val;
|
res.properties['shelf_response'] = val;
|
||||||
|
mergeShelfResponse(val, res);
|
||||||
return ''; // Write nothing
|
return ''; // Write nothing
|
||||||
};
|
};
|
||||||
|
|
||||||
|
return true;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/*
|
||||||
// Merge shelf response if necessary
|
// Merge shelf response if necessary
|
||||||
app.responseFinalizers.add((RequestContext req, ResponseContext res) async {
|
app.responseFinalizers.add((RequestContext req, ResponseContext res) async {
|
||||||
if (res.properties.containsKey('shelf_response')) {
|
if (res.properties.containsKey('shelf_response')) {
|
||||||
|
@ -36,5 +40,6 @@ AngelConfigurer supportShelf() {
|
||||||
await mergeShelfResponse(shelfResponse, res);
|
await mergeShelfResponse(shelfResponse, res);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
*/
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
22
pubspec.yaml
22
pubspec.yaml
|
@ -1,15 +1,15 @@
|
||||||
author: "Tobe O <thosakwe@gmail.com>"
|
author: Tobe O <thosakwe@gmail.com>
|
||||||
description: "Shelf interop with Angel."
|
description: Shelf interop with Angel.
|
||||||
homepage: "https://github.com/angel-dart/shelf"
|
homepage: https://github.com/angel-dart/shelf
|
||||||
name: "angel_shelf"
|
name: angel_shelf
|
||||||
version: "1.2.0"
|
version: 1.2.0
|
||||||
dependencies:
|
dependencies:
|
||||||
angel_framework: ">=1.1.0-alpha <2.0.0"
|
angel_framework: >=1.1.0-alpha <2.0.0
|
||||||
shelf: "^0.6.0"
|
shelf: ^0.6.0
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
angel_test: "^1.1.0"
|
angel_test: ^1.1.0
|
||||||
console: "^2.2.4"
|
console: ^2.2.4
|
||||||
shelf_proxy: "^0.1.0"
|
shelf_proxy: ^0.1.0
|
||||||
test: "^0.12.0"
|
test: ^0.12.0
|
||||||
environment:
|
environment:
|
||||||
sdk: ">=1.19.0"
|
sdk: ">=1.19.0"
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import 'package:console/console.dart';
|
import 'package:console/console.dart';
|
||||||
import 'package:logging/logging.dart';
|
import 'package:logging/logging.dart';
|
||||||
|
import 'package:stack_trace/stack_trace.dart';
|
||||||
|
|
||||||
/// Prints the contents of a [LogRecord] with pretty colors.
|
/// Prints the contents of a [LogRecord] with pretty colors.
|
||||||
void prettyLog(LogRecord record) {
|
void prettyLog(LogRecord record) {
|
||||||
|
@ -10,7 +11,7 @@ void prettyLog(LogRecord record) {
|
||||||
if (record.error != null)
|
if (record.error != null)
|
||||||
pen(record.error.toString());
|
pen(record.error.toString());
|
||||||
if (record.stackTrace != null)
|
if (record.stackTrace != null)
|
||||||
pen(record.stackTrace.toString());
|
pen(new Chain.forTrace(record.stackTrace).terse.toString());
|
||||||
|
|
||||||
pen();
|
pen();
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ import 'package:angel_shelf/angel_shelf.dart';
|
||||||
import 'package:angel_test/angel_test.dart';
|
import 'package:angel_test/angel_test.dart';
|
||||||
import 'package:logging/logging.dart';
|
import 'package:logging/logging.dart';
|
||||||
import 'package:shelf/shelf.dart' as shelf;
|
import 'package:shelf/shelf.dart' as shelf;
|
||||||
|
import 'package:stack_trace/stack_trace.dart';
|
||||||
import 'package:test/test.dart';
|
import 'package:test/test.dart';
|
||||||
import 'pretty_logging.dart';
|
import 'pretty_logging.dart';
|
||||||
|
|
||||||
|
@ -11,6 +12,7 @@ main() {
|
||||||
|
|
||||||
setUp(() async {
|
setUp(() async {
|
||||||
var app = new Angel()..lazyParseBodies = true;
|
var app = new Angel()..lazyParseBodies = true;
|
||||||
|
await app.configure(supportShelf());
|
||||||
|
|
||||||
app.get('/inject', (shelf.Request request) {
|
app.get('/inject', (shelf.Request request) {
|
||||||
print('URL of injected request: ${request.url.path}');
|
print('URL of injected request: ${request.url.path}');
|
||||||
|
@ -21,7 +23,6 @@ main() {
|
||||||
return new shelf.Response.ok('world');
|
return new shelf.Response.ok('world');
|
||||||
});
|
});
|
||||||
|
|
||||||
await app.configure(supportShelf());
|
|
||||||
app.logger = new Logger.detached('angel')..onRecord.listen(prettyLog);
|
app.logger = new Logger.detached('angel')..onRecord.listen(prettyLog);
|
||||||
client = await connectTo(app);
|
client = await connectTo(app);
|
||||||
});
|
});
|
||||||
|
@ -29,14 +30,20 @@ main() {
|
||||||
tearDown(() => client.close());
|
tearDown(() => client.close());
|
||||||
|
|
||||||
test('injected into request', () async {
|
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}');
|
print('Response: ${response.body}');
|
||||||
expect(response, isJson({'inject': true}));
|
expect(response, isJson({'inject': true}));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('can return shelf response', () async {
|
test('can return shelf response', () {
|
||||||
var response = await client.get('/hello');
|
return Chain.capture(() async {
|
||||||
|
var response = await client.get('/hello', headers: {'accept': 'application/json'});
|
||||||
print('Response: ${response.body}');
|
print('Response: ${response.body}');
|
||||||
expect(response, hasBody('world'));
|
expect(response, hasBody('world'));
|
||||||
|
}, onError: (e, chain) {
|
||||||
|
print(e);
|
||||||
|
print(chain.terse);
|
||||||
|
expect(0, 1);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue