From 2a42a31999e2a6de6ee86e41b37be2c170ac48c9 Mon Sep 17 00:00:00 2001 From: Sebastian Wilzbach Date: Tue, 18 Jul 2017 16:11:54 +0200 Subject: [PATCH 1/2] vibe.web.web: Allow Json as a auto-injected parameter type --- tests/vibe.web.web/source/app.d | 33 ++++++++++++++++++++++++++------- web/vibe/web/web.d | 4 ++++ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/tests/vibe.web.web/source/app.d b/tests/vibe.web.web/source/app.d index 06fb8ab86f..1a73f08367 100644 --- a/tests/vibe.web.web/source/app.d +++ b/tests/vibe.web.web/source/app.d @@ -2,6 +2,7 @@ module app; import vibe.core.core; import vibe.core.log; +import vibe.data.json; import vibe.http.client; import vibe.http.router; import vibe.http.server; @@ -10,12 +11,18 @@ import std.format : format; // TODO: test the various parameter and return type combinations, as well as all attributes +class Service { + @noRoute void getFoo(HTTPServerResponse res) { res.writeBody("oops"); } + void getBar(HTTPServerResponse res) { res.writeBody("ok"); } + // you can work with Json objects directly + auto postJson(Json json) { return json; } +} + shared static this() { auto settings = new HTTPServerSettings; settings.bindAddresses = ["127.0.0.1"]; settings.port = 9132; - auto router = new URLRouter; router.registerWebInterface(new Service); @@ -23,7 +30,18 @@ shared static this() runTask({ scope (exit) exitEventLoop(); - + void postJson(V)(string url, V[string] payload, HTTPStatus expected, scope void delegate(scope HTTPClientResponse res) expectedHandler) { + requestHTTP("http://127.0.0.1:9132"~url, + (scope req) { + req.method = HTTPMethod.POST; + req.writeJsonBody(payload); + }, + (scope res) { + assert(res.statusCode == expected, format("Unexpected status code for %s: %s", url, res.statusCode)); + expectedHandler(res); + } + ); + } void test(string url, HTTPStatus expected) { requestHTTP("http://127.0.0.1:9132"~url, (scope req) { @@ -36,11 +54,12 @@ shared static this() } test("/foo", HTTPStatus.notFound); test("/bar", HTTPStatus.ok); + + postJson("/json", ["foo": "bar"], HTTPStatus.ok, (scope res) { + auto j = res.readJson; + assert(j["foo"].get!string == "bar"); + }); + logInfo("All web tests succeeded."); }); } - -class Service { - @noRoute void getFoo(HTTPServerResponse res) { res.writeBody("oops"); } - void getBar(HTTPServerResponse res) { res.writeBody("ok"); } -} diff --git a/web/vibe/web/web.d b/web/vibe/web/web.d index 0d18e6ab3d..1c97331721 100644 --- a/web/vibe/web/web.d +++ b/web/vibe/web/web.d @@ -911,6 +911,10 @@ private void handleRequest(string M, alias overload, C, ERROR...)(HTTPServerRequ else static if (is(PT == InputStream)) params[i] = req.bodyReader; else static if (is(PT == HTTPServerRequest) || is(PT == HTTPRequest)) params[i] = req; else static if (is(PT == HTTPServerResponse) || is(PT == HTTPResponse)) params[i] = res; + else static if (is(PT == Json)) { + enforceBadRequest(req.json.type != Json.Type.undefined, "Invalid Json passed."); + params[i] = req.json; + } else static if (is(PT == WebSocket)) {} // handled below else static if (param_names[i].startsWith("_")) { if (auto pv = param_names[i][1 .. $] in req.params) { From e44ffefb4990d2a5deb9f5ebfa9f36076644da83 Mon Sep 17 00:00:00 2001 From: Sebastian Wilzbach Date: Tue, 10 Oct 2017 20:06:48 +0200 Subject: [PATCH 2/2] Only allow _body and _json as valid parameter names for Json types --- tests/vibe.web.web/source/app.d | 2 +- web/vibe/web/web.d | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/vibe.web.web/source/app.d b/tests/vibe.web.web/source/app.d index 1a73f08367..ea0181e2ac 100644 --- a/tests/vibe.web.web/source/app.d +++ b/tests/vibe.web.web/source/app.d @@ -15,7 +15,7 @@ class Service { @noRoute void getFoo(HTTPServerResponse res) { res.writeBody("oops"); } void getBar(HTTPServerResponse res) { res.writeBody("ok"); } // you can work with Json objects directly - auto postJson(Json json) { return json; } + auto postJson(Json _json) { return _json; } } shared static this() diff --git a/web/vibe/web/web.d b/web/vibe/web/web.d index 1c97331721..335d4dfeda 100644 --- a/web/vibe/web/web.d +++ b/web/vibe/web/web.d @@ -912,6 +912,8 @@ private void handleRequest(string M, alias overload, C, ERROR...)(HTTPServerRequ else static if (is(PT == HTTPServerRequest) || is(PT == HTTPRequest)) params[i] = req; else static if (is(PT == HTTPServerResponse) || is(PT == HTTPResponse)) params[i] = res; else static if (is(PT == Json)) { + import std.algorithm.comparison : among; + static assert(param_names[i].among("_body", "_json", "The Json parameter must be named _body or _json.")); enforceBadRequest(req.json.type != Json.Type.undefined, "Invalid Json passed."); params[i] = req.json; }