File: test-request-basic-auth.t

package info (click to toggle)
libweb-simple-perl 0.033-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 380 kB
  • sloc: perl: 1,622; makefile: 7
file content (54 lines) | stat: -rw-r--r-- 1,258 bytes parent folder | download | duplicates (3)
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
54
use strictures 1;
use Test::More 0.88;

my $auth_result;
my @auth_args;

{
  package TestApp;

  use Web::Simple;
  use Plack::Middleware::Auth::Basic;

  sub dispatch_request {
    sub () {
      Plack::Middleware::Auth::Basic->new(
        authenticator => sub { 
          @auth_args = @_; return $auth_result
        }
      )
    },
    sub () {
      [ 200, [ 'Content-type' => 'text/plain' ], [ 'Woo' ] ]
    }
  }
}

my $ta = TestApp->new;

my $res = $ta->run_test_request(GET => '/');

is($res->code, '401', 'Auth failed with no user/pass');
ok(!@auth_args, 'Auth callback never called with no user/pass');

$res = $ta->run_test_request(GET => 'bob:secret@/');

is($res->code, '401', 'Auth failed with bad user/pass');
is($auth_args[0], 'bob', 'Username passed ok');
is($auth_args[1], 'secret', 'Password passed ok');

$auth_result = 1;
@auth_args = ();

$res = $ta->run_test_request(GET => '/');

is($res->code, '401', 'Auth failed with no user/pass');
ok(!@auth_args, 'Auth callback never called with no user/pass');

$res = $ta->run_test_request(GET => 'bob:secret@/');

is($res->code, '200', 'Auth succeeded with good user/pass');
is($auth_args[0], 'bob', 'Username passed ok');
is($auth_args[1], 'secret', 'Password passed ok');

done_testing;