chiark / gitweb /
Testing: Provide incrementing url
[cgi-auth-flexible.git] / tests / cgi
1 #!/usr/bin/perl -w
2
3 use strict;
4 use warnings;
5 use CGI qw/escapeHTML/;;
6 use CGI::Auth::Flexible;
7 use URI;
8 use Data::Dumper;
9
10 #use Carp::Always;
11 $SIG{__DIE__} = sub { Carp::confess(@_) };
12
13 my $dump = "$ENV{'CAFTEST_CAF'}/tests/tmp";
14
15 my $q = CGI->new;
16
17 my $url = $q->url();
18
19 $url =~ s{^\Qhttp://localhost/\E}{$ENV{CAFTEST_URLBASE}}
20     if $ENV{'CAFTEST_URLBASE'};
21
22 my @verifier_params =(
23     username_password_error => sub {
24         my ($c,$r,$u,$p)=@_;
25         return $p eq 'sesame' ? undef : 'wrong password'
26     },
27     encrypted_only => 0,
28     promise_check_mutate => 1,
29     dir => $dump,
30     srcdump_filter_cwd => 0,
31     debug => sub { print STDERR "DEBUG ", @_[2..@_-1]; },
32     get_url => sub { return $url },
33 );
34
35 my $verifier = CGI::Auth::Flexible->new_verifier(@verifier_params);
36
37 END { $verifier->disconnect() if $verifier; }
38
39 my $authreq = $verifier->new_request($q);
40
41 $authreq->check_ok() or exit;
42
43 my $cookie = $authreq->secret_cookie();
44 my $hiddenhtml = $authreq->secret_hidden_html();
45
46 print <<END;
47 Content-Type: text/html
48 Set-Cookie: $cookie
49
50 <html><head><title>TITLE</title></head>
51 <body><h1>ACCESSGRANTED</h1>
52 END
53
54 my $newurl = $authreq->url_with_query_params($authreq->chain_params());
55 my $newurl_esc = escapeHTML($newurl);
56
57 my $incrurl = $authreq->url_with_query_params
58     ({ counter => [ ($q->param('counter')//0) + 1 ]});
59 my $incrurl_esc = escapeHTML($incrurl);
60
61 print <<END;
62 <h1>info<h1>
63 <pre>
64 END
65
66 my $txt = Data::Dumper->Dump([$authreq->get_username(),
67  $q->request_method eq 'POST' ? $authreq->check_mutate() : "(not POST)",
68                               $q->path_info(),
69                               $authreq->chain_params(),
70                               scalar $q->Vars()],
71                              [qw(username mutate_ok path
72                                  authreq->chain_params() cgi->params())]);
73 foreach my $l (split /\n/, $txt) {
74     print escapeHTML($l),"\n";
75 }
76
77 print <<END;
78 </pre>
79 self=<a href="$newurl_esc">$newurl_esc</a>
80 increment=<a href="$incrurl_esc">$incrurl_esc</a>
81 <form method="POST" action="$url">
82 $hiddenhtml
83 <input type="submit" name="test_cgi_sponges" value="Make sponges">
84 <input type="submit" name="test_cgi_worms" value="Make worms">
85 <input type="submit" name="caf_logout" value="Logout">
86 </form>
87 <form method="POST" action="$url/extra">
88 $hiddenhtml
89 <input type="submit" name="test_cgi_append" value="Append">
90 </form>
91 END