summaryrefslogtreecommitdiff
path: root/_test/tests/inc/httpclient_http.test.php
blob: 3857273f779de45ebd678ecbdf98013f3aa29c04 (plain)
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
141
<?php

require_once DOKU_INC.'inc/init.php';
require_once DOKU_INC.'inc/HTTPClient.php';

class httpclient_http_test extends DokuWikiTest {
    protected $server = 'http://httpbin.org';

    function test_simpleget(){
        $http = new HTTPClient();
        $data = $http->get($this->server.'/get?foo=bar');
        $this->assertFalse($data === false, 'HTTP response');
        $resp = json_decode($data, true);
        $this->assertTrue(is_array($resp), 'JSON response');
        $this->assertArrayHasKey('args',$resp);
        $this->assertEquals(array('foo'=>'bar'), $resp['args']);
    }

    function test_dget(){
        $http = new HTTPClient();
        $data = $http->dget($this->server.'/get',array('foo'=>'bar'));
        $this->assertFalse($data === false, 'HTTP response');
        $resp = json_decode($data, true);
        $this->assertTrue(is_array($resp), 'JSON response');
        $this->assertArrayHasKey('args',$resp);
        $this->assertEquals(array('foo'=>'bar'), $resp['args']);
    }

    function test_gzip(){
        $http = new HTTPClient();
        $data = $http->get($this->server.'/gzip');
        $this->assertFalse($data === false, 'HTTP response');
        $resp = json_decode($data, true);
        $this->assertTrue(is_array($resp), 'JSON response');
        $this->assertArrayHasKey('gzipped',$resp);
        $this->assertTrue($resp['gzipped']);
    }

    function test_simplepost(){
        $http = new HTTPClient();
        $data = $http->post($this->server.'/post',array('foo'=>'bar'));
        $this->assertFalse($data === false, 'HTTP response');
        $resp = json_decode($data, true);
        $this->assertTrue(is_array($resp), 'JSON response');
        $this->assertArrayHasKey('form',$resp);
        $this->assertEquals(array('foo'=>'bar'), $resp['form']);
    }

    function test_redirect(){
        $http = new HTTPClient();
        $data = $http->get($this->server.'/redirect/3');
        $this->assertFalse($data === false, 'HTTP response');
        $resp = json_decode($data, true);
        $this->assertTrue(is_array($resp), 'JSON response');
        $this->assertArrayHasKey('url',$resp);
        $this->assertRegExp('/\/get$/', $resp['url']);
    }

    function test_relredirect(){
        $http = new HTTPClient();
        $data = $http->get($this->server.'/relative-redirect/3');
        $this->assertFalse($data === false, 'HTTP response');
        $resp = json_decode($data, true);
        $this->assertTrue(is_array($resp), 'JSON response');
        $this->assertArrayHasKey('url',$resp);
        $this->assertRegExp('/\/get$/', $resp['url']);
    }

    function test_redirectfail(){
        $http = new HTTPClient();
        $data = $http->get($this->server.'/redirect/5');
        $this->assertTrue($data === false, 'HTTP response');
        $this->assertEquals('Maximum number of redirects exceeded',$http->error);
    }

    function test_cookies(){
        $http = new HTTPClient();
        $http->get($this->server.'/cookies/set/foo/bar');
        $this->assertEquals(array('foo' => 'bar'), $http->cookies);
        $data = $http->get($this->server.'/cookies');
        $this->assertFalse($data === false, 'HTTP response');
        $resp = json_decode($data, true);
        $this->assertTrue(is_array($resp), 'JSON response');
        $this->assertArrayHasKey('cookies',$resp);
        $this->assertEquals(array('foo'=>'bar'), $resp['cookies']);
    }

    function test_teapot(){
        $http = new HTTPClient();
        $data = $http->get($this->server.'/status/418');
        $this->assertTrue($data === false, 'HTTP response');
        $this->assertEquals(418,$http->status);
    }

    function test_maxbody(){
        $http = new HTTPClient();
        $http->max_bodysize = 250;
        $data = $http->get($this->server.'/stream/30');
        $this->assertTrue($data === false, 'HTTP response');
    }

    function test_basicauth(){
        $http = new HTTPClient();
        $http->user = 'user';
        $http->pass = 'pass';
        $data = $http->get($this->server.'/basic-auth/user/pass');
        $this->assertFalse($data === false, 'HTTP response');
        $resp = json_decode($data, true);
        $this->assertTrue(is_array($resp), 'JSON response');
        $this->assertEquals(array('authenticated'=>true,'user'=>'user'), $resp);
    }

    function test_basicauthfail(){
        $http = new HTTPClient();
        $http->user = 'user';
        $http->pass = 'invalid';
        $data = $http->get($this->server.'/basic-auth/user/pass');
        $this->assertTrue($data === false, 'HTTP response');
        $this->assertEquals(401,$http->status);
    }

    function test_timeout(){
        $http = new HTTPClient();
        $http->timeout = 5;
        $data = $http->get($this->server.'/delay/10');
        $this->assertTrue($data === false, 'HTTP response');
        $this->assertEquals(-100,$http->status);
    }

    function test_headers(){
        $http = new HTTPClient();
        $data = $http->get($this->server.'/response-headers?baz=&foo=bar');
        $this->assertFalse($data === false, 'HTTP response');
        $resp = json_decode($data, true);
        $this->assertTrue(is_array($resp), 'JSON response');
        $this->assertArrayHasKey('baz',$http->resp_headers);
        $this->assertArrayHasKey('foo',$http->resp_headers);
        $this->assertEquals('bar',$http->resp_headers['foo']);
    }
}
//Setup VIM: ex: et ts=4 :