Skip to content

Commit

Permalink
Form method middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
Emil Persson committed Nov 7, 2015
1 parent 6211c50 commit 28877db
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ part 'src/http/middleware.dart';
part 'src/http/middleware/csrf_middleware.dart';
part 'src/http/middleware/input_middleware.dart';
part 'src/http/middleware/static_files_middleware.dart';
part 'src/http/middleware/form_method_middleware.dart';
part 'src/http/router/route.dart';
part 'src/http/router/route_group.dart';
part 'src/http/router/router.dart';
Expand Down
2 changes: 2 additions & 0 deletions lib/src/http/http_service_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ class HttpServiceProvider extends ServiceProvider {
CsrfMiddleware csrfMiddleware,
StaticFilesMiddleware staticFilesMiddleware,
InputMiddleware inputMiddleware,
FormMethodMiddleware formMethodMiddleware,
UrlGenerator urlGenerator) {
_urlGenerator = urlGenerator;

server.addMiddleware(sessionsMiddleware, highPriority: true);
server.addMiddleware(staticFilesMiddleware);
server.addMiddleware(inputMiddleware);
server.addMiddleware(formMethodMiddleware);
server.addMiddleware(csrfMiddleware);
server.onError = (e, s) {
print('');
Expand Down
29 changes: 29 additions & 0 deletions lib/src/http/middleware/form_method_middleware.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
part of bridge.http;

class FormMethodMiddleware {
shelf.Handler innerHandler;

call(shelf.Handler innerHandler) {
this.innerHandler = innerHandler;
return _handleRequests;
}

_handleRequests(shelf.Request request) async {
shelf.Response response = await innerHandler(request);

return new shelf.Response(
response.statusCode,
body: _injectFormMethods(await response.readAsString()),
headers: response.headers,
context: response.context);
}

String _injectFormMethods(String body) {
const pattern = r'''(<form[^>]*?method=)(['"])(put|patch|update|delete)\2([^>]*>)''';
return body.replaceAllMapped(
new RegExp(pattern, caseSensitive: false), (m) {
return '${m[1]}${m[2]}POST${m[2]}${m[4]}'
"<input type='hidden' name='_method' value='${m[3].toUpperCase()}'>";
});
}
}
22 changes: 22 additions & 0 deletions test/http/request_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,28 @@ class RequestTest implements TestCase {
equals("<form><input type='hidden' name='_token' value='id'></form>"));
}

@test
it_injects_a_hidden_input_in_html_forms_with_http_method() async {
server.addMiddleware(new FormMethodMiddleware());
router.get('/', () => ""
"<form method='patch'></form>"
"<form method='delete'></form>"
"<form method='UPDATE'></form>"
"<form method='put'></form>");
var request = new shelf.Request('GET', new Uri.http('example.com', '/'));
shelf.Response response = await server.handle(request);
expect(await response.readAsString(),
equals(""
"<form method='POST'>"
"<input type='hidden' name='_method' value='PATCH'></form>"
"<form method='POST'>"
"<input type='hidden' name='_method' value='DELETE'></form>"
"<form method='POST'>"
"<input type='hidden' name='_method' value='UPDATE'></form>"
"<form method='POST'>"
"<input type='hidden' name='_method' value='PUT'></form>"));
}

@test
it_can_add_specific_middleware_for_a_route() async {
router.post('/', () => 'response')
Expand Down

0 comments on commit 28877db

Please sign in to comment.