chiark / gitweb /
Testing: Fix up $url management
[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>H1</h1>
52 <h1>again</h1>
53
54 <h1>info<h1>
55 <pre>
56 END
57
58 my $newurl = $authreq->url_with_query_params($authreq->chain_params());
59 my $newurl_esc = escapeHTML($newurl);
60
61 my $txt = Data::Dumper->Dump([$authreq->get_username(),
62  $q->request_method eq 'POST' ? $authreq->check_mutate() : "(not POST)",
63                               $q->path_info(),
64                               $authreq->chain_params(),
65                               scalar $q->Vars()],
66                              [qw(username mutate_ok path
67                                  authreq->chain_params() cgi->params())]);
68 foreach my $l (split /\n/, $txt) {
69     print escapeHTML($l),"\n";
70 }
71
72 print <<END;
73 </pre>
74 <a href="$newurl_esc">$newurl_esc</a>
75 <form method="POST" action="$url">
76 $hiddenhtml
77 <input type="submit" name="test_cgi_sponges" value="Make sponges">
78 <input type="submit" name="test_cgi_worms" value="Make worms">
79 <input type="submit" name="caf_logout" value="Logout">
80 </form>
81 <form method="POST" action="$url/extra">
82 $hiddenhtml
83 <input type="submit" name="test_cgi_append" value="Append">
84 </form>
85 END