forked from tcdent/php-restclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.php
214 lines (169 loc) · 7.31 KB
/
test.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
// Test are comprised of two components: a simple json response for testing
// interaction via the built-in PHP server, and PHPUnit test methods.
// Test Server
// This code is only executed by the test server instance. It returns simple
// JSON debug information for validating behavior.
if(php_sapi_name() == 'cli-server'){
header("Content-Type: application/json");
die(json_encode(array(
'SERVER' => $_SERVER,
'REQUEST' => $_REQUEST,
'POST' => $_POST,
'GET' => $_GET,
'body' => file_get_contents('php://input'),
'headers' => getallheaders()
)));
}
// Unit Tests
//
require 'restclient.php';
// This varible can be overridden with a PHPUnit XML configuration file.
if(!isset($TEST_SERVER_URL))
$TEST_SERVER_URL = "http://localhost:8888";
class RestClientTest extends PHPUnit_Framework_TestCase {
public function test_get(){
global $TEST_SERVER_URL;
$api = new RestClient;
$result = $api->get($TEST_SERVER_URL, [
'foo' => ' bar', 'baz' => 1, 'bat' => ['foo', 'bar']
]);
$response_json = $result->decode_response();
$this->assertEquals('GET',
$response_json->SERVER->REQUEST_METHOD);
$this->assertEquals("foo=+bar&baz=1&bat%5B%5D=foo&bat%5B%5D=bar",
$response_json->SERVER->QUERY_STRING);
$this->assertEquals("",
$response_json->body);
}
public function test_post(){
global $TEST_SERVER_URL;
$api = new RestClient;
$result = $api->post($TEST_SERVER_URL, [
'foo' => ' bar', 'baz' => 1, 'bat' => ['foo', 'bar']
]);
$response_json = $result->decode_response();
$this->assertEquals('POST',
$response_json->SERVER->REQUEST_METHOD);
$this->assertEquals("foo=+bar&baz=1&bat%5B%5D=foo&bat%5B%5D=bar",
$response_json->body);
}
public function test_put(){
global $TEST_SERVER_URL;
$api = new RestClient;
$result = $api->put($TEST_SERVER_URL, array(
'foo' => ' bar', 'baz' => 1));
$response_json = $result->decode_response();
$this->assertEquals('PUT',
$response_json->SERVER->REQUEST_METHOD);
$this->assertEquals("foo=+bar&baz=1",
$response_json->body);
}
public function test_delete(){
global $TEST_SERVER_URL;
$api = new RestClient;
$result = $api->delete($TEST_SERVER_URL, array(
'foo' => ' bar', 'baz' => 1));
$response_json = $result->decode_response();
$this->assertEquals('DELETE',
$response_json->SERVER->REQUEST_METHOD);
$this->assertEquals("foo=+bar&baz=1",
$response_json->body);
}
public function test_user_agent(){
global $TEST_SERVER_URL;
$api = new RestClient(array(
'user_agent' => "RestClient Unit Test"
));
$result = $api->get($TEST_SERVER_URL);
$response_json = $result->decode_response();
$this->assertEquals("RestClient Unit Test",
$response_json->headers->{"User-Agent"});
}
public function test_json_patch(){
global $TEST_SERVER_URL;
$api = new RestClient;
$result = $api->execute($TEST_SERVER_URL, 'PATCH',
"{\"foo\":\"bar\"}",
array(
'X-HTTP-Method-Override' => 'PATCH',
'Content-Type' => 'application/json-patch+json'));
$response_json = $result->decode_response();
$this->assertEquals('application/json-patch+json',
$response_json->headers->{"Content-Type"});
$this->assertEquals('PATCH',
$response_json->headers->{"X-HTTP-Method-Override"});
$this->assertEquals('PATCH',
$response_json->SERVER->REQUEST_METHOD);
$this->assertEquals("{\"foo\":\"bar\"}",
$response_json->body);
}
public function test_json_post(){
global $TEST_SERVER_URL;
$api = new RestClient;
$result = $api->post($TEST_SERVER_URL, "{\"foo\":\"bar\"}",
array('Content-Type' => 'application/json'));
$response_json = $result->decode_response();
$this->assertEquals('application/json',
$response_json->headers->{"Content-Type"});
$this->assertEquals('POST',
$response_json->SERVER->REQUEST_METHOD);
$this->assertEquals("{\"foo\":\"bar\"}",
$response_json->body);
}
public function test_multiheader_response(){
$RESPONSE = "HTTP/1.1 200 OK\r\nContent-type: text/json\r\nContent-Type: application/json\r\n\r\nbody";
$api = new RestClient;
// bypass request execution to inject controlled response data.
$api->parse_response($RESPONSE);
$this->assertEquals(["HTTP/1.1 200 OK"],
$api->response_status_lines);
$this->assertEquals((object) [
'content_type' => ["text/json", "application/json"]
], $api->headers);
$this->assertEquals("body", $api->response);
}
public function test_multistatus_response(){
$RESPONSE = "HTTP/1.1 100 Continue\r\n\r\nHTTP/1.1 200 OK\r\nCache-Control: no-cache\r\nContent-Type: application/json\r\n\r\nbody";
$api = new RestClient;
// bypass request execution to inject controlled response data.
$api->parse_response($RESPONSE);
$this->assertEquals(["HTTP/1.1 100 Continue", "HTTP/1.1 200 OK"],
$api->response_status_lines);
$this->assertEquals((object) [
'cache_control' => "no-cache",
'content_type' => "application/json"
], $api->headers);
$this->assertEquals("body", $api->response);
}
public function test_status_only_response(){
$RESPONSE = "HTTP/1.1 100 Continue\r\n\r\n";
$api = new RestClient;
// bypass request execution to inject controlled response data.
$api->parse_response($RESPONSE);
$this->assertEquals(["HTTP/1.1 100 Continue"],
$api->response_status_lines);
$this->assertEquals((object) [], $api->headers);
$this->assertEquals("", $api->response);
}
public function test_build_indexed_queries(){
global $TEST_SERVER_URL;
$api = new RestClient(['build_indexed_queries' => TRUE]);
$result = $api->get($TEST_SERVER_URL, [
'foo' => ' bar', 'baz' => 1, 'bat' => ['foo', 'bar', 'baz[12]']
]);
$response_json = $result->decode_response();
$this->assertEquals("foo=+bar&baz=1&bat%5B0%5D=foo&bat%5B1%5D=bar&bat%5B2%5D=baz%5B12%5D",
$response_json->SERVER->QUERY_STRING);
}
public function test_build_non_indexed_queries(){
global $TEST_SERVER_URL;
$api = new RestClient;
$result = $api->get($TEST_SERVER_URL, [
'foo' => ' bar', 'baz' => 1, 'bat' => ['foo', 'bar', 'baz[12]']
]);
$response_json = $result->decode_response();
$this->assertEquals("foo=+bar&baz=1&bat%5B%5D=foo&bat%5B%5D=bar&bat%5B%5D=baz%5B12%5D",
$response_json->SERVER->QUERY_STRING);
}
}