Skip to content

Commit

Permalink
vibe.web.web: Allow to return strings
Browse files Browse the repository at this point in the history
  • Loading branch information
wilzbach committed Jul 18, 2017
1 parent be6862a commit 519f555
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
26 changes: 15 additions & 11 deletions tests/vibe.web.web/source/app.d
Expand Up @@ -5,42 +5,46 @@ import vibe.core.log;
import vibe.http.client;
import vibe.http.router;
import vibe.http.server;
import vibe.stream.operations : readAllUTF8;
import vibe.web.web;
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"); }
string getString() { return "string"; }
}

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 test(string url, HTTPStatus expected) {
void test(string url, HTTPStatus expectedStatus, string expectedBody = "") {
requestHTTP("http://127.0.0.1:9132"~url,
(scope req) {
},
(scope res) {
res.dropBody();
assert(res.statusCode == expected, format("Unexpected status code for %s: %s", url, res.statusCode));
assert(res.statusCode == expectedStatus, format("Unexpected status code for %s: %s", url, res.statusCode));
if (res.statusCode == HTTPStatus.ok) {
auto body_ = res.bodyReader.readAllUTF8;
assert(body_ == expectedBody, format("Unexpected body for %s: %s", url, body_));
}
}
);
}
test("/foo", HTTPStatus.notFound);
test("/bar", HTTPStatus.ok);
test("/bar", HTTPStatus.ok, "ok");
test("/string", HTTPStatus.ok, "string");
logInfo("All web tests succeeded.");
});
}

class Service {
@noRoute void getFoo(HTTPServerResponse res) { res.writeBody("oops"); }
void getBar(HTTPServerResponse res) { res.writeBody("ok"); }
}
2 changes: 2 additions & 0 deletions web/vibe/web/web.d
Expand Up @@ -964,6 +964,8 @@ private void handleRequest(string M, alias overload, C, ERROR...)(HTTPServerRequ
} else {
res.writeBody(ret);
}
} else static if (is(RET : string)) {
res.writeBody(ret);
} else {
static assert(is(RET == void), M~": Only InputStream, Json and void are supported as return types for route methods.");
}
Expand Down

0 comments on commit 519f555

Please sign in to comment.