File: 20-Common-CGI.t

package info (click to toggle)
lemonldap-ng 1.9.7-3%2Bdeb9u2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 39,024 kB
  • sloc: perl: 37,552; makefile: 922; sh: 472; sql: 5
file content (133 lines) | stat: -rw-r--r-- 4,393 bytes parent folder | download
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# 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';
package My::Portal;

use strict;
use Test::More tests => 25;
use_ok('Lemonldap::NG::Common::CGI');

#our @ISA = qw('Lemonldap::NG::Common::CGI');
use base 'Lemonldap::NG::Common::CGI';

sub mySubtest {
    return 'OK1';
}

sub abort {
    shift;
    $, = '';
    print STDERR @_;
    die 'abort has been called';
}

sub quit {
    2;
}

our $param;

sub param {
    return $param;
}

sub soapfunc {
    return 'SoapOK';
}

our $buf;
our $lastpos = 0;

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

SKIP: {
    eval "use IO::String;";
    skip "IO::String not installed", 9 if ($@);
    tie *STDOUT, 'IO::String', $buf;

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

    # 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.

    my $cgi;

    $ENV{SCRIPT_NAME}     = '/test.pl';
    $ENV{SCRIPT_FILENAME} = 't/20-Common-CGI.t';
    $ENV{REQUEST_METHOD}  = 'GET';
    $ENV{REQUEST_URI}     = '/';
    $ENV{QUERY_STRING}    = '';

    #$cgi = CGI->new;
    ok( ( $cgi = Lemonldap::NG::Common::CGI->new() ), 'New CGI' );
    bless $cgi, 'My::Portal';

    # 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' );

    # 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' );

    # 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' );

    # Extract lang Android (See #LEMONLDAP-530)
    my $cgi3;
    $ENV{HTTP_ACCEPT_LANGUAGE} = 'fr-FR, en-US';
    ok( ( $cgi3 = Lemonldap::NG::Common::CGI->new() ), 'New CGI' );
    ok( $lang = $cgi3->extract_lang(), 'extract_lang Android' );
    ok( $lang->[0] eq 'fr',  'extract_lang Android' );
    ok( $lang->[1] eq 'en',  'extract_lang Android' );
    ok( scalar(@$lang) == 2, 'extract_lang Android' );

    # Extract lang with * value
    my $cgi4;
    $ENV{HTTP_ACCEPT_LANGUAGE} = "fr,en,*";
    ok( ( $cgi4 = Lemonldap::NG::Common::CGI->new() ), 'New CGI' );
    ok( $lang = $cgi4->extract_lang(), 'extract_lang with * value' );
    ok( scalar(@$lang) == 2, 'extract_lang with * value' );

    # SOAP
    eval { require SOAP::Lite };
    skip "SOAP::Lite is not installed, so CGI SOAP functions will not work", 3
      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' );
}