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

Added test for #1922 #1924

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions tests/vibe.web.rest.1922/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1922
7 changes: 7 additions & 0 deletions tests/vibe.web.rest.1922/dub.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "1922",
"dependencies": {
"vibe-d": { "path": "../../" }
},
"versions": ["VibeDefaultMain"]
}
112 changes: 112 additions & 0 deletions tests/vibe.web.rest.1922/source/app.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import vibe.d;
import std.datetime;
import vibe.web.auth;

shared static this()
{
auto settings = new HTTPServerSettings;
// 10k + issue number -> Avoid bind errors
settings.port = 11922;
settings.bindAddresses = ["::1", "127.0.0.1"];
settings.sessionStore = new MemorySessionStore();

auto router = new URLRouter;
router.registerRestInterface(new AuthAPI);
listenHTTP(settings, router);

setTimer(1.seconds, {
scope(exit) exitEventLoop();

void test(string endpoint, string user, HTTPStatus expected = HTTPStatus.ok){
requestHTTP("http://127.0.0.1:11922"~endpoint, (scope req){
if(user !is null)
req.headers["AuthUser"] = user;
}, (scope res) {
assert(res.statusCode == expected, format("Unexpected status code for GET %s (%s): %s\n%s", endpoint, user, res.statusCode,res.readJson));
});
}

test("/non_auth_number?num=5", null);
test("/non_auth_number?num=5", "admin");
test("/auth_number?num=5", "admin");
test("/auth_number?num=5", null, HTTPStatus.forbidden);
test("/items/name?item=something", "admin");
test("/items/name?item=something", null, HTTPStatus.forbidden);
test("/items/num?num=37", "admin");
test("/items/num?num=37", null, HTTPStatus.forbidden);
});
}

struct AuthInfo {
string name;
}

interface IItemAPI {
struct CollectionIndices {
string item;
}
string getName(string item, AuthInfo info);
int getNum(int num);
}

@requiresAuth
class ItemAPI : IItemAPI {
@anyAuth
string getName(string item, AuthInfo info){
return info.name ~ item;
}

@anyAuth
int getNum(int num){
return num;
}

@noRoute final mixin CreateAuthFunc;
}

@requiresAuth
interface IAuthAPI {

@noAuth int getNonAuthNumber(int num);
@anyAuth int getAuthNumber(AuthInfo info, int num);
@anyAuth Collection!IItemAPI items();

@noRoute final mixin CreateAuthFunc;
}

class AuthAPI : IAuthAPI {
private IItemAPI m_items;
this(){
m_items = new ItemAPI;
}

Collection!IItemAPI items(){
return Collection!IItemAPI(m_items);
}

int getNonAuthNumber(int num){
return num;
}
int getAuthNumber(AuthInfo info, int num){
logInfo("Returning auth number for authorized user: %s", info.name);
return info.name.length.to!int * num;
}
}

auto getReq(HTTPServerRequest req, HTTPServerResponse _){
return req;
}

auto getRes(HTTPServerRequest _, HTTPServerResponse res){
return res;
}

mixin template CreateAuthFunc(){
AuthInfo authenticate(HTTPServerRequest req, HTTPServerResponse res){
AuthInfo ret;
if("AuthUser" in req.headers && req.headers["AuthUser"]=="admin"){
ret.name = "admin";
} else throw new HTTPStatusException(HTTPStatus.forbidden, "Forbidden");
return ret;
}
}