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 Apr 2, 2018
1 parent e3a0d3a commit 4258cfa
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
25 changes: 15 additions & 10 deletions tests/vibe.web.web/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@ 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;
Expand All @@ -21,24 +28,22 @@ shared static this()

runTask({
scope (exit) exitEventLoop();

void test(string url, HTTPStatus expected) {
void test(string url, HTTPStatus expectedStatus, string expectedBody = "") {
requestHTTP("http://" ~ serverAddr.toString ~ 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"); }
}
4 changes: 3 additions & 1 deletion web/vibe/web/web.d
Original file line number Diff line number Diff line change
Expand Up @@ -1020,8 +1020,10 @@ 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.");
static assert(is(RET == void), M~": Only InputStream, Json, string and void are supported as return types for route methods.");
}
}
} catch (Exception ex) {
Expand Down

0 comments on commit 4258cfa

Please sign in to comment.