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 ca5a5466c..535e8218f 100644 --- a/packages/dart_frog/lib/src/response.dart +++ b/packages/dart_frog/lib/src/response.dart @@ -110,6 +110,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; 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 c8c70a39f..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, @@ -297,5 +296,34 @@ 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(); + }); + }); }); }