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

vibe.web.web: Allow Json as a auto-injected parameter type #1853

Open
wants to merge 2 commits into
base: master
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
33 changes: 26 additions & 7 deletions tests/vibe.web.web/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -10,20 +11,37 @@ 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);

listenHTTP(settings, router);

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) {
Expand All @@ -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"); }
}
6 changes: 6 additions & 0 deletions web/vibe/web/web.d
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,12 @@ 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)) {
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;
}
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) {
Expand Down