1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Created by PhpStorm. |
5
|
|
|
* User: jaredchu |
6
|
|
|
* Date: 31/05/2017 |
7
|
|
|
* Time: 10:14 |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
use JC\JCRequest; |
11
|
|
|
|
12
|
|
|
class JCRequestTest extends PHPUnit_Framework_TestCase |
|
|
|
|
13
|
|
|
{ |
14
|
|
|
private $baseUrl = 'https://httpbin.org'; |
15
|
|
|
private $params; |
16
|
|
|
private $headers; |
17
|
|
|
|
18
|
|
|
public function setUp() |
19
|
|
|
{ |
20
|
|
|
$this->params = [ |
21
|
|
|
'b' => 2, |
22
|
|
|
'c' => '3' |
23
|
|
|
]; |
24
|
|
|
$this->headers = [ |
25
|
|
|
'User-Agent' => 'Jared Chu', |
26
|
|
|
'Accept' => 'application/json' |
27
|
|
|
]; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function testGet() |
31
|
|
|
{ |
32
|
|
|
$url = $this->baseUrl; |
33
|
|
|
$response = JCRequest::get($url); |
34
|
|
|
$this->assertEquals(200, $response->status()); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
View Code Duplication |
public function testGetWithParams() |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
$url = $this->baseUrl . '/get?a=1'; |
40
|
|
|
|
41
|
|
|
$response = JCRequest::get($url, $this->params, $this->headers); |
42
|
|
|
$this->assertEquals(200, $response->status()); |
43
|
|
|
$this->assertNotEmpty($response->body()); |
44
|
|
|
|
45
|
|
|
$responseData = $response->json(); |
46
|
|
|
$this->assertEquals('https://httpbin.org/get?a=1&b=2&c=3', $responseData->url); |
47
|
|
|
$this->assertEquals(1, $responseData->args->a); |
48
|
|
|
$this->assertEquals(2, $responseData->args->b); |
49
|
|
|
$this->assertEquals(3, $responseData->args->c); |
50
|
|
|
|
51
|
|
|
$this->assertEquals('Jared Chu', $responseData->headers->{'User-Agent'}); |
52
|
|
|
$this->assertEquals('application/json', $responseData->headers->{'Accept'}); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
View Code Duplication |
public function testPost() |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
$url = $this->baseUrl . '/post?a=1'; |
58
|
|
|
|
59
|
|
|
$response = JCRequest::post($url, $this->params, $this->headers); |
60
|
|
|
$this->assertEquals(200, $response->status()); |
61
|
|
|
|
62
|
|
|
$responseData = $response->json(); |
63
|
|
|
$this->assertEquals('https://httpbin.org/post?a=1', $responseData->url); |
64
|
|
|
$this->assertEquals(1, $responseData->args->a); |
65
|
|
|
$this->assertEquals(2, $responseData->form->b); |
66
|
|
|
$this->assertEquals(3, $responseData->form->c); |
67
|
|
|
|
68
|
|
|
$this->assertEquals('Jared Chu', $responseData->headers->{'User-Agent'}); |
69
|
|
|
$this->assertEquals('application/json', $responseData->headers->{'Accept'}); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
View Code Duplication |
public function testPut() |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
$url = $this->baseUrl . '/put?a=1'; |
75
|
|
|
|
76
|
|
|
$response = JCRequest::put($url, $this->params, $this->headers); |
77
|
|
|
$this->assertEquals(200, $response->status()); |
78
|
|
|
|
79
|
|
|
$responseData = $response->json(); |
80
|
|
|
$this->assertEquals('https://httpbin.org/put?a=1', $responseData->url); |
81
|
|
|
$this->assertEquals(1, $responseData->args->a); |
82
|
|
|
$this->assertEquals(2, $responseData->form->b); |
83
|
|
|
$this->assertEquals(3, $responseData->form->c); |
84
|
|
|
|
85
|
|
|
$this->assertEquals('Jared Chu', $responseData->headers->{'User-Agent'}); |
86
|
|
|
$this->assertEquals('application/json', $responseData->headers->{'Accept'}); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
View Code Duplication |
public function testPatch() |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
$url = $this->baseUrl . '/patch?a=1'; |
92
|
|
|
|
93
|
|
|
$response = JCRequest::patch($url, $this->params, $this->headers); |
94
|
|
|
$this->assertEquals(200, $response->status()); |
95
|
|
|
|
96
|
|
|
$responseData = $response->json(); |
97
|
|
|
$this->assertEquals('https://httpbin.org/patch?a=1', $responseData->url); |
98
|
|
|
$this->assertEquals(1, $responseData->args->a); |
99
|
|
|
$this->assertEquals(2, $responseData->form->b); |
100
|
|
|
$this->assertEquals(3, $responseData->form->c); |
101
|
|
|
|
102
|
|
|
$this->assertEquals('Jared Chu', $responseData->headers->{'User-Agent'}); |
103
|
|
|
$this->assertEquals('application/json', $responseData->headers->{'Accept'}); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
View Code Duplication |
public function testDelete() |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
$url = $this->baseUrl . '/delete?a=1'; |
109
|
|
|
|
110
|
|
|
$response = JCRequest::delete($url, $this->params, $this->headers); |
111
|
|
|
$this->assertEquals(200, $response->status()); |
112
|
|
|
|
113
|
|
|
$responseData = $response->json(); |
114
|
|
|
$this->assertEquals('https://httpbin.org/delete?a=1', $responseData->url); |
115
|
|
|
$this->assertEquals(1, $responseData->args->a); |
116
|
|
|
$this->assertEquals(2, $responseData->form->b); |
117
|
|
|
$this->assertEquals(3, $responseData->form->c); |
118
|
|
|
|
119
|
|
|
$this->assertEquals('Jared Chu', $responseData->headers->{'User-Agent'}); |
120
|
|
|
$this->assertEquals('application/json', $responseData->headers->{'Accept'}); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function testHead() |
124
|
|
|
{ |
125
|
|
|
$url = $this->baseUrl . '/headers'; |
126
|
|
|
|
127
|
|
|
$response = JCRequest::head($url, $this->params, $this->headers); |
128
|
|
|
$this->assertEquals(200, $response->status()); |
129
|
|
|
$this->assertEmpty($response->body()); |
130
|
|
|
|
131
|
|
|
$responseHeaders = $response->headers(); |
132
|
|
|
$this->assertEquals('keep-alive', $responseHeaders['Connection'][0]); |
133
|
|
|
$this->assertEquals('application/json', $responseHeaders['Content-Type'][0]); |
134
|
|
|
$this->assertEquals('Flask', $responseHeaders['X-Powered-By'][0]); |
135
|
|
|
$this->assertEquals(144, $responseHeaders['Content-Length'][0]); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function testJson() |
139
|
|
|
{ |
140
|
|
|
$url = $this->baseUrl . '/post?a=1'; |
141
|
|
|
|
142
|
|
|
$response = JCRequest::post($url, json_encode($this->params), $this->headers); |
143
|
|
|
$this->assertEquals(200, $response->status()); |
144
|
|
|
|
145
|
|
|
$responseData = $response->json(); |
146
|
|
|
$this->assertEquals('https://httpbin.org/post?a=1', $responseData->url); |
147
|
|
|
$this->assertEquals(1, $responseData->args->a); |
148
|
|
|
$this->assertEquals('{"b":2,"c":"3"}', json_decode($responseData->data)); |
149
|
|
|
|
150
|
|
|
$this->assertEquals('Jared Chu', $responseData->headers->{'User-Agent'}); |
151
|
|
|
$this->assertEquals('application/json', $responseData->headers->{'Accept'}); |
152
|
|
|
} |
153
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.