forked from WordPress/Requests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRequests.php
executable file
·120 lines (106 loc) · 3.2 KB
/
Requests.php
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
<?php
class RequestsTest_Requests extends PHPUnit_Framework_TestCase {
/**
* @expectedException Requests_Exception
*/
public function testInvalidProtocol() {
$request = Requests::request('ftp://128.0.0.1/');
}
public function testDefaultTransport() {
$request = Requests::get('http://httpbin.org/get');
$this->assertEquals(200, $request->status_code);
}
/**
* Standard response header parsing
*/
public function testHeaderParsing() {
$transport = new RawTransport();
$transport->data =
"HTTP/1.0 200 OK\r\n".
"Host: localhost\r\n".
"Host: ambiguous\r\n".
"Nospace:here\r\n".
"Muchspace: there \r\n".
"Empty:\r\n".
"Empty2: \r\n".
"Folded: one\r\n".
"\ttwo\r\n".
" three\r\n\r\n".
"stop\r\n";
$options = array(
'transport' => $transport
);
$response = Requests::get('http://example.com/', array(), $options);
$expected = new Requests_Response_Headers();
$expected['host'] = 'localhost,ambiguous';
$expected['nospace'] = 'here';
$expected['muchspace'] = 'there';
$expected['empty'] = '';
$expected['empty2'] = '';
$expected['folded'] = 'one two three';
$this->assertEquals($expected, $response->headers);
}
/**
* Headers with only \n delimiting should be treated as if they're \r\n
*/
public function testHeaderOnlyLF() {
$transport = new RawTransport();
$transport->data = "HTTP/1.0 200 OK\r\nTest: value\nAnother-Test: value\r\n\r\n";
$options = array(
'transport' => $transport
);
$response = Requests::get('http://example.com/', array(), $options);
$this->assertEquals('value', $response->headers['test']);
$this->assertEquals('value', $response->headers['another-test']);
}
/**
* Check that invalid protocols are not accepted
*
* We do not support HTTP/0.9. If this is really an issue for you, file a
* new issue, and update your server/proxy to support a proper protocol.
*
* @expectedException Requests_Exception
*/
public function testInvalidProtocolVersion() {
$transport = new RawTransport();
$transport->data = "HTTP/0.9 200 OK\r\n\r\n<p>Test";
$options = array(
'transport' => $transport
);
$response = Requests::get('http://example.com/', array(), $options);
}
/**
* HTTP/0.9 also appears to use a single CRLF instead of two
*
* @expectedException Requests_Exception
*/
public function testSingleCRLFSeparator() {
$transport = new RawTransport();
$transport->data = "HTTP/0.9 200 OK\r\n<p>Test";
$options = array(
'transport' => $transport
);
$response = Requests::get('http://example.com/', array(), $options);
}
/**
* @expectedException Requests_Exception
*/
public function testInvalidStatus() {
$transport = new RawTransport();
$transport->data = "HTTP/1.1 OK\r\nTest: value\nAnother-Test: value\r\n\r\nTest";
$options = array(
'transport' => $transport
);
$response = Requests::get('http://example.com/', array(), $options);
}
public function test30xWithoutLocation() {
$transport = new MockTransport();
$transport->code = 302;
$options = array(
'transport' => $transport
);
$response = Requests::get('http://example.com/', array(), $options);
$this->assertEquals(302, $response->status_code);
$this->assertEquals(0, $response->redirects);
}
}