Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dart_frog): allow access of shelf.Message.context #1262

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/dart_frog/lib/src/request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> get shelfContext => _request.context;

/// The requested url relative to the current handler path.
Uri get url => _request.url;

Expand Down
3 changes: 3 additions & 0 deletions packages/dart_frog/lib/src/response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> get shelfContext => _response.context;

/// The HTTP headers with case-insensitive keys.
/// The returned map is unmodifiable.
Map<String, String> get headers => _response.headers;
Expand Down
27 changes: 27 additions & 0 deletions packages/dart_frog/test/src/request_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -227,5 +227,32 @@ void main() {
expect(request.json(), completion(equals(body)));
});
});

group('shelfContext', () {
test('allow access of the shelf context ', () async {
late Map<String, Object> contextFromShelf;
late Map<String, Object> 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();
});
});
});
}
32 changes: 30 additions & 2 deletions packages/dart_frog/test/src/response_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -297,5 +296,34 @@ void main() {
);
});
});

group('shelfContext', () {
test('allow access of the shelf context ', () async {
late Map<String, Object> contextFromShelf;
late Map<String, Object> 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();
});
});
});
}