From 3c4f7606fc6b2eb4de25b0c48d33fe4e21bcfadb Mon Sep 17 00:00:00 2001 From: Sun Jiao Date: Sun, 28 Jan 2024 22:23:56 +0800 Subject: [PATCH 1/3] fix(dart_frog): allow access of `shelf.Message.context` --- packages/dart_frog/lib/src/request.dart | 3 +++ packages/dart_frog/lib/src/response.dart | 3 +++ 2 files changed, 6 insertions(+) diff --git a/packages/dart_frog/lib/src/request.dart b/packages/dart_frog/lib/src/request.dart index 63c88766a..b558cc110 100644 --- a/packages/dart_frog/lib/src/request.dart +++ b/packages/dart_frog/lib/src/request.dart @@ -100,6 +100,9 @@ class Request { return _request.context['shelf.io.connection_info']! as HttpConnectionInfo; } + /// Shelf context that can be used by shelf middleware and shelf handlers. + Map get shelfContext => _request.context; + /// The requested url relative to the current handler path. Uri get url => _request.url; diff --git a/packages/dart_frog/lib/src/response.dart b/packages/dart_frog/lib/src/response.dart index 52cc4da07..1dcf4d7c1 100644 --- a/packages/dart_frog/lib/src/response.dart +++ b/packages/dart_frog/lib/src/response.dart @@ -87,6 +87,9 @@ class Response { /// The HTTP status code of the response. int get statusCode => _response.statusCode; + /// Shelf context that can be used by shelf middleware and shelf handlers. + Map get shelfContext => _response.context; + /// The HTTP headers with case-insensitive keys. /// The returned map is unmodifiable. Map get headers => _response.headers; From 14df274100cff22989e508f53e1d422a3dc5cb96 Mon Sep 17 00:00:00 2001 From: Sun Jiao Date: Tue, 30 Jan 2024 23:25:16 +0800 Subject: [PATCH 2/3] add tests for shelfContext --- packages/dart_frog/test/src/request_test.dart | 27 +++++++++++++++++ .../dart_frog/test/src/response_test.dart | 30 +++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/packages/dart_frog/test/src/request_test.dart b/packages/dart_frog/test/src/request_test.dart index 0af1ed14b..f77c01001 100644 --- a/packages/dart_frog/test/src/request_test.dart +++ b/packages/dart_frog/test/src/request_test.dart @@ -227,5 +227,32 @@ void main() { expect(request.json(), completion(equals(body))); }); }); + + group('shelfContext', () { + test('allow access of the shelf context ', () async { + late Map contextFromShelf; + late Map contextFromFrog; + final server = await serve( + ((RequestContext context) async { + contextFromFrog = context.request.shelfContext; + return Response(); + }).use( + fromShelfMiddleware((innerHandler) { + return (request) async { + contextFromShelf = request.context; + return innerHandler(request); + }; + }), + ), + 'localhost', + 3000, + ); + final client = HttpClient(); + final request = await client.getUrl(Uri.parse('http://localhost:3000')); + await request.close(); + expect(contextFromFrog, equals(contextFromShelf)); + await server.close(); + }); + }); }); } diff --git a/packages/dart_frog/test/src/response_test.dart b/packages/dart_frog/test/src/response_test.dart index 0e9e48b87..1ddf5fb7e 100644 --- a/packages/dart_frog/test/src/response_test.dart +++ b/packages/dart_frog/test/src/response_test.dart @@ -257,5 +257,35 @@ void main() { ); }); }); + + + group('shelfContext', () { + test('allow access of the shelf context ', () async { + late Map contextFromShelf; + late Map contextFromFrog; + final server = await serve( + ((RequestContext context) async { + final response = Response(); + contextFromFrog = response.shelfContext; + return response; + }).use( + fromShelfMiddleware((innerHandler) { + return (request) async { + final response = await innerHandler(request); + contextFromShelf = response.context; + return response; + }; + }), + ), + 'localhost', + 3000, + ); + final client = HttpClient(); + final request = await client.getUrl(Uri.parse('http://localhost:3000')); + await request.close(); + expect(contextFromFrog, equals(contextFromShelf)); + await server.close(); + }); + }); }); } From 453cb1450b4cbffb332e45db94e1faf1f55e5a1e Mon Sep 17 00:00:00 2001 From: Sun Jiao Date: Wed, 31 Jan 2024 00:10:07 +0800 Subject: [PATCH 3/3] format files --- packages/dart_frog/test/src/response_test.dart | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/dart_frog/test/src/response_test.dart b/packages/dart_frog/test/src/response_test.dart index 43947c8be..8e1d6f9ec 100644 --- a/packages/dart_frog/test/src/response_test.dart +++ b/packages/dart_frog/test/src/response_test.dart @@ -137,8 +137,7 @@ void main() { '''The context should have the '${Response.shelfBufferOutputContextKey}' key.''', ); - final bufferOutput = - response.context[Response.shelfBufferOutputContextKey]; + final bufferOutput = response.context[Response.shelfBufferOutputContextKey]; expect( bufferOutput, isFalse, @@ -298,7 +297,6 @@ void main() { }); }); - group('shelfContext', () { test('allow access of the shelf context ', () async { late Map contextFromShelf;