summaryrefslogtreecommitdiffstats
path: root/vespaclient/src/perl/test/Yahoo/Vespa/HttpTest.pl
blob: 88c2961e3a220d43ae9674d987cd6e5fd3cdad04 (plain) (blame)
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
134
135
136
137
138
139
140
# Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#
# Tests of the Http wrapper library..
#
# NOTE: Test server set up does not support content not ending in newline.
# 

use strict;
use Test::More;
use Yahoo::Vespa::Mocks::HttpServerMock;

BEGIN {
    use_ok( 'Yahoo::Vespa::Http' );
    *Http:: = *Yahoo::Vespa::Http::
}
require_ok( 'Yahoo::Vespa::Http' );

my $httpTestServerPort = setupTestHttpServer();
ok(defined $httpTestServerPort, "Test server set up");

&testSimpleGet();
&testAdvancedGet();
&testFailingGet();
&testSimplePost();
&testJsonReturnInPost();

done_testing();

exit(0);

sub filterRequest {
    my ($request) = @_;
    $request =~ s/\r//g;
    $request =~ s/(Content-Length:\s*)\d+/$1##/g;
    $request =~ s/(Host: localhost:)\d+/$1##/g;
    $request =~ s/(?:Connection|TE|Client-[^:]+):[^\n]*\n//g;

    return $request;
}

sub testSimpleGet {
    my %r = Http::get('localhost', $httpTestServerPort, '/foo');
    is( $r{'code'}, 200, "Get request code" );
    is( $r{'status'}, 'OK', "Get request status" );

    my $expected = <<EOS;
HTTP/1.1 200 OK
Content-Length: ##
Content-Type: text/plain; charset=utf-8

GET /foo HTTP/1.1
Host: localhost:##
User-Agent: Vespa-perl-script
EOS
    is( &filterRequest($r{'all'}), $expected, 'Get result' );
}

sub testAdvancedGet {
    my @headers = ("X-Foo" => 'Bar');
    my @uri_param = ("uricrap" => 'special=?&%value',
                     "other" => 'hmm');
    my %r = Http::request('GET', 'localhost', $httpTestServerPort, '/foo',
                          \@uri_param, undef, \@headers);
    is( $r{'code'}, 200, "Get request code" );
    is( $r{'status'}, 'OK', "Get request status" );

    my $expected = <<EOS;
HTTP/1.1 200 OK
Content-Length: ##
Content-Type: text/plain; charset=utf-8

GET /foo?uricrap=special%3D%3F%26%25value&other=hmm HTTP/1.1
Host: localhost:##
User-Agent: Vespa-perl-script
X-Foo: Bar
EOS
    is( &filterRequest($r{'all'}), $expected, 'Get result' );
}

sub testFailingGet {
    my @uri_param = ("code" => '501',
                     "status" => 'Works');
    my %r = Http::request('GET', 'localhost', $httpTestServerPort, '/foo',
                          \@uri_param);
    is( $r{'code'}, 501, "Get request code" );
    is( $r{'status'}, 'Works', "Get request status" );

    my $expected = <<EOS;
HTTP/1.1 501 Works
Content-Length: ##
Content-Type: text/plain; charset=utf-8

GET /foo?code=501&status=Works HTTP/1.1
Host: localhost:##
User-Agent: Vespa-perl-script
EOS
    is( &filterRequest($r{'all'}), $expected, 'Get result' );
}

sub testSimplePost {
    my @uri_param = ("uricrap" => 'Rrr' );
    my %r = Http::request('POST', 'localhost', $httpTestServerPort, '/foo',
                          \@uri_param, "Some content\n");
    is( $r{'code'}, 200, "Get request code" );
    is( $r{'status'}, 'OK', "Get request status" );

    my $expected = <<EOS;
HTTP/1.1 200 OK
Content-Length: ##
Content-Type: text/plain; charset=utf-8

POST /foo?uricrap=Rrr HTTP/1.1
Host: localhost:##
User-Agent: Vespa-perl-script
Content-Length: ##
Content-Type: application/x-www-form-urlencoded

Some content
EOS
    is( &filterRequest($r{'all'}), $expected, 'Get result' );
}

sub testJsonReturnInPost
{
    my @uri_param = ("contenttype" => 'application/json' );
    my $json = "{ \"key\" : \"value\" }\n";
    my %r = Http::request('POST', 'localhost', $httpTestServerPort, '/foo',
                          \@uri_param, $json);
    is( $r{'code'}, 200, "Get request code" );
    is( $r{'status'}, 'OK', "Get request status" );

    my $expected = <<EOS;
HTTP/1.1 200 OK
Content-Length: ##
Content-Type: application/json

{ "key" : "value" }
EOS
    is( &filterRequest($r{'all'}), $expected, 'Get json result' );
}