20-Common-CGI.t 3.58 KB
Newer Older
Yadd's avatar
Yadd committed
1
2
3
4
5
6
# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl Lemonldap-NG-Manager.t'

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

# change 'tests => 1' to 'tests => last_test_to_print';
Yadd's avatar
Yadd committed
7
package My::Portal;
Yadd's avatar
Yadd committed
8

Yadd's avatar
Yadd committed
9
use strict;
10
use Test::More tests => 17;
Yadd's avatar
Yadd committed
11
use_ok('Lemonldap::NG::Common::CGI');
Yadd's avatar
Yadd committed
12

Yadd's avatar
Yadd committed
13
14
#our @ISA = qw('Lemonldap::NG::Common::CGI');
use base 'Lemonldap::NG::Common::CGI';
Yadd's avatar
Yadd committed
15

Yadd's avatar
Yadd committed
16
sub mySubtest {
Yadd's avatar
Yadd committed
17
18
19
    return 'OK1';
}

Yadd's avatar
Yadd committed
20
21
22
23
24
25
sub abort {
    shift;
    $, = '';
    print STDERR @_;
    die 'abort has been called';
}
Yadd's avatar
Yadd committed
26

Yadd's avatar
Yadd committed
27
28
29
sub quit {
    2;
}
Yadd's avatar
Yadd committed
30

Yadd's avatar
Yadd committed
31
our $param;
Yadd's avatar
Yadd committed
32

Yadd's avatar
Yadd committed
33
34
35
sub param {
    return $param;
}
Yadd's avatar
Yadd committed
36

Yadd's avatar
Yadd committed
37
38
39
sub soapfunc {
    return 'SoapOK';
}
Yadd's avatar
Yadd committed
40
41

our $buf;
Yadd's avatar
Yadd committed
42
43
44
45
46
47
48
49
50
51
our $lastpos = 0;

sub diff {
    my $str = $buf;
    $str =~ s/^.{$lastpos}//s if ($lastpos);
    $str =~ s/\r//gs;
    $lastpos = length $buf;
    return $str;
}

Yadd's avatar
Yadd committed
52
53
54
55
56
SKIP: {
    eval "use IO::String;";
    skip "IO::String not installed", 9 if ($@);
    tie *STDOUT, 'IO::String', $buf;

Yadd's avatar
Yadd committed
57
58
#########################

Yadd's avatar
Yadd committed
59
60
    # Insert your test code below, the Test::More module is use()ed here so read
    # its man page ( perldoc Test::More ) for help writing this test script.
Yadd's avatar
Yadd committed
61

Yadd's avatar
Yadd committed
62
    my $cgi;
Yadd's avatar
Yadd committed
63

Yadd's avatar
Yadd committed
64
65
66
67
68
    $ENV{SCRIPT_NAME}     = '/test.pl';
    $ENV{SCRIPT_FILENAME} = 't/20-Common-CGI.t';
    $ENV{REQUEST_METHOD}  = 'GET';
    $ENV{REQUEST_URI}     = '/';
    $ENV{QUERY_STRING}    = '';
Yadd's avatar
Yadd committed
69

Yadd's avatar
Yadd committed
70
71
72
    #$cgi = CGI->new;
    ok( ( $cgi = Lemonldap::NG::Common::CGI->new() ), 'New CGI' );
    bless $cgi, 'My::Portal';
Yadd's avatar
Yadd committed
73

Yadd's avatar
Yadd committed
74
75
76
77
78
    # Test header_public
    ok( $buf = $cgi->header_public('t/20-Common-CGI.t'), 'header_public' );
    ok( $buf =~ /Cache-control: public; must-revalidate; max-age=\d+\r?\n/s,
        'Cache-Control' );
    ok( $buf =~ /Last-modified: /s, 'Last-Modified' );
Yadd's avatar
Yadd committed
79

Yadd's avatar
Yadd committed
80
81
82
83
    # Test _sub mechanism
    ok( $cgi->_sub('mySubtest') eq 'OK1', '_sub mechanism 1' );
    $cgi->{mySubtest} = sub { return 'OK2' };
    ok( $cgi->_sub('mySubtest') eq 'OK2', '_sub mechanism 2' );
Yadd's avatar
Yadd committed
84

85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
    # Test extract_lang
    my $lang;
    ok( $lang = $cgi->extract_lang(),
        'extract_lang 0 with void "Accept-language"' );
    ok( scalar(@$lang) == 0, 'extract_lang 1 with void "Accept-language"' );

    my $cgi2;
    $ENV{SCRIPT_NAME}     = '/test.pl';
    $ENV{SCRIPT_FILENAME} = 't/20-Common-CGI.t';
    $ENV{REQUEST_METHOD}  = 'GET';
    $ENV{REQUEST_URI}     = '/';
    $ENV{QUERY_STRING}    = '';
    $ENV{HTTP_ACCEPT_LANGUAGE} = 'fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3';
    ok( ( $cgi2 = Lemonldap::NG::Common::CGI->new() ), 'New CGI' );
    ok( $lang = $cgi2->extract_lang(), 'extract_lang' );
    ok( $lang->[0] eq 'fr', 'extract_lang' );
    ok( $lang->[1] eq 'en', 'extract_lang' );
    ok( scalar(@$lang) == 2, 'extract_lang' );

Yadd's avatar
Yadd committed
104
    # SOAP
Yadd's avatar
Yadd committed
105
    eval { require SOAP::Lite };
106
    skip "SOAP::Lite is not installed, so CGI SOAP functions will not work", 3
Yadd's avatar
Yadd committed
107
108
109
110
111
112
113
114
115
116
117
      if ($@);
    $ENV{HTTP_SOAPACTION} =
      'http://localhost/Lemonldap/NG/Common/CGI/SOAPService#soapfunc';
    $param =
'<?xml version="1.0" encoding="UTF-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><soapfunc xmlns="http://localhost/Lemonldap/NG/Common/CGI/SOAPService"><var xsi:type="xsd:string">fr</var></soapfunc></soap:Body></soap:Envelope>';
    ok( $cgi->soapTest('soapfunc') == 2, 'SOAP call exit fine' );
    my $tmp = diff();
    ok( $tmp =~ /^Status: 200/s, 'HTTP response 200' );
    ok( $tmp =~ /<result xsi:type="xsd:string">SoapOK<\/result>/s,
        'result of SOAP call' );
}