File: TestWebServer.pm

package info (click to toggle)
libwebinject-perl 1.86-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 428 kB
  • ctags: 197
  • sloc: perl: 4,101; xml: 257; makefile: 7
file content (72 lines) | stat: -rw-r--r-- 2,174 bytes parent folder | download | duplicates (5)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env perl

##################################################

package TestWebServer;

use strict;
use Test::More;
use Data::Dumper;
use FindBin qw($Bin);
use base qw(HTTP::Server::Simple::CGI);

my $webserverpid;

# Fire up test webserver
sub handle_request {
    my $self   = shift;
    my $cgi    = shift;
    my $path   = $cgi->path_info();
    my $method = $cgi->request_method();

    # GET Requests
    if($method eq 'GET' and $path =~ m|/code/(\d+)|) {
        print "HTTP/1.0 $1\r\n\r\nrequest for response code $1\r\n";
    }
    elsif($method eq 'GET' and $path =~ m|/teststring|) {
        print "HTTP/1.0 200 OK\r\n\r\nthis is just a teststring";
    }
    elsif($method eq 'GET' and $path =~ m|/sleep/(\d+)|) {
        sleep($1);
        print "HTTP/1.0 200 OK\r\n\r\nsleeped $1 seconds";
    }
    elsif($method eq 'GET' and $path =~ m|/auth|) {
        if(defined $ENV{'HTTP_AUTHORIZATION'} and $ENV{'HTTP_AUTHORIZATION'} eq 'Basic dXNlcjpwYXNz') {
            print "HTTP/1.0 200 OK\r\n\r\nauth successfull";
        }
        elsif(defined $ENV{'HTTP_AUTHORIZATION'}) {
            print "HTTP/1.0 403 Forbidden\r\n\r\nyou are not authorized";
        } else {
            print "HTTP/1.0 401 Authorization required\r\n";
            print "WWW-Authenticate: Basic realm=\"my_realm\"\r\n";
            print "\r\n";
            print "got auth request with the following cgi object:\n";
        }
        print Dumper($cgi);
        print Dumper(\%ENV);
    }

    # POST Requests
    elsif($method eq 'POST' and $path =~ m|/post|) {
        print "HTTP/1.0 200 OK\r\n\r\n";
        print "got post request with the following cgi object:\n";
        print Dumper($cgi);
    } else {
        print "HTTP/1.0 400 Bad Request\r\n\r\n";
        print "bad path: '$path'\r\n";
    }
}

sub start_webserver {
    # start the server on port 508080
    $webserverpid = TestWebServer->new(58080)->background();
    $SIG{INT} = sub{ kill 2, $webserverpid if defined $webserverpid; undef $webserverpid; exit 1; };
}

##################################################
# stop our test webserver
END {
    kill 2, $webserverpid if defined $webserverpid;
}

1;