-
Notifications
You must be signed in to change notification settings - Fork 18
/
auth_basic.t
53 lines (46 loc) · 1.48 KB
/
auth_basic.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use v6;
use Test;
use Crust::Builder;
use Crust::Test;
use HTTP::Request;
my %map = (
:admin("s3cr3t"),
:john("foo:bar"),
);
my $app = builder {
enable "Auth::Basic",
:authenticator(-> $u, $p, %env {
%map{$u} && %map{$u} eq $p;
});
-> %env { start { 200, [:Content-Type('text/plain')], ["Hello {%env<REMOTE_USER>}!"] } }
};
test-p6w
app => $app,
client => -> $cb {
my ($req, $res);
# No auth, should get 401
$req = HTTP::Request.new(GET => "http://localhost/");
$res = $cb($req);
is $res.code, 401;
# Auth for admin, should get 200
$req = HTTP::Request.new(GET => "http://localhost/");
$req.header.field(:Authorization('Basic YWRtaW46czNjcjN0'));
$res = $cb($req);
is $res.code, 200, "Should succeed";
is $res.content.decode, "Hello admin!";
# Auth for john, should get 200
$req = HTTP::Request.new(GET => "http://localhost/");
$req.header.field(:Authorization('Basic am9objpmb286YmFy'));
$res = $cb($req);
is $res.code, 200, "Should succeed";
is $res.content.decode, "Hello john!";
# Corrupt Authorization header, should get 401
$req = HTTP::Request.new(GET => "http://localhost/");
$req.header.field(:Authorization('Basic deadBEAFam9objpmb286YmFy'));
$res = $cb($req);
if !is $res.code, 401, "Should fail" {
$res.content.decode.say;
}
}
;
done-testing;