1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: lejla |
5
|
|
|
* Date: 2016.02.10. |
6
|
|
|
* Time: 17:51 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace CurlX\Tests; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use CurlX\Request; |
13
|
|
|
use PHPUnit_Framework_TestCase; |
14
|
|
|
|
15
|
|
|
class RequestTest extends PHPUnit_Framework_TestCase |
16
|
|
|
{ |
17
|
|
|
public function testUrl() |
18
|
|
|
{ |
19
|
|
|
$request = new Request('http://url.com'); |
20
|
|
|
$this->assertEquals('http://url.com', $request->url); |
21
|
|
|
|
22
|
|
|
$request->url = 'http://url2.com'; |
23
|
|
|
$this->assertEquals('http://url2.com', $request->url); |
24
|
|
|
|
25
|
|
|
$request->url = 'badurl'; |
26
|
|
|
$this->assertEquals('http://url2.com', $request->url); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testPostFieldsBuilder() |
30
|
|
|
{ |
31
|
|
|
$request = new Request(); |
32
|
|
|
|
33
|
|
|
// No post values yet, so it should default to GET |
34
|
|
|
$this->assertArrayNotHasKey(CURLOPT_POST, $request->options); |
35
|
|
|
$this->assertEmpty($request->post_data); |
36
|
|
|
|
37
|
|
|
// With post values |
38
|
|
|
$post = ['username' => 'mike', 'password' => 'pass']; |
39
|
|
|
$request->post_data = $post; |
40
|
|
|
$this->assertArrayHasKey(CURLOPT_POST, $request->options); |
41
|
|
|
$this->assertEquals($post, $request->post_data); |
42
|
|
|
|
43
|
|
|
// Add more post fields |
44
|
|
|
$post2 = ['otherdata' => 'newvalue', 'username' => 'stacey']; |
45
|
|
|
$request->post_data = $post2; |
46
|
|
|
$this->assertArrayHasKey(CURLOPT_POST, $request->options); |
47
|
|
|
$this->assertEquals($post2 + $post, $request->post_data); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testTimer() |
51
|
|
|
{ |
52
|
|
|
$request = new Request(); |
53
|
|
|
|
54
|
|
|
$this->assertNull($request->time); |
55
|
|
|
|
56
|
|
|
$request->startTimer(); |
57
|
|
|
$this->assertNull($request->time); |
58
|
|
|
|
59
|
|
|
$request->stopTimer(); |
60
|
|
|
$this->assertNotNull($request->time); |
61
|
|
|
$this->assertTrue($request->time >= 0); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testNotify() |
65
|
|
|
{ |
66
|
|
|
$request = new Request(); |
67
|
|
|
$called1 = false; |
68
|
|
|
$called2 = false; |
69
|
|
|
$r1 = null; |
70
|
|
|
$r2 = null; |
71
|
|
|
|
72
|
|
|
$request->addListener(function($var) use (&$called1, &$r1) { |
73
|
|
|
$called1 = true; |
74
|
|
|
$r1 = $var; |
75
|
|
|
}); |
76
|
|
|
|
77
|
|
|
$request->addListener(function($var) use (&$called2, &$r2) { |
78
|
|
|
$called2 = true; |
79
|
|
|
$r2 = $var; |
80
|
|
|
}); |
81
|
|
|
|
82
|
|
|
$request->callBack([]); |
83
|
|
|
|
84
|
|
|
$this->assertTrue($called1, 'Callback 1 was not notified on request completion!'); |
85
|
|
|
$this->assertInstanceOf('CurlX\RequestInterface', $r1, 'Callback 1 did not receive the request object'); |
86
|
|
|
$this->assertTrue($called2, 'Callback 2 was not notified on request completion!'); |
87
|
|
|
$this->assertInstanceOf('CurlX\RequestInterface', $r2, 'Callback 2 did not receive the request object'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function testHeaders() |
91
|
|
|
{ |
92
|
|
|
$request = new Request(); |
93
|
|
|
|
94
|
|
|
$header = ['a' => 'aaa', 'b' => 'bbb']; |
95
|
|
|
$normalHeader = ['a: aaa', 'b: bbb']; |
96
|
|
|
|
97
|
|
|
$request->headers = $header; |
98
|
|
|
$this->assertEquals($header, $request->headers); |
99
|
|
|
|
100
|
|
|
$this->assertArrayHasKey(CURLOPT_HTTPHEADER, $request->options); |
101
|
|
|
$this->assertEquals($normalHeader, $request->options[CURLOPT_HTTPHEADER]); |
102
|
|
|
|
103
|
|
|
$header2 = ['b' => 'BBB', 'c' => 'CCC']; |
104
|
|
|
$normalOfBothHeaders = ['b: BBB', 'c: CCC', 'a: aaa']; |
105
|
|
|
$request->headers = $header2; |
106
|
|
|
$this->assertEquals($header2 + $header, $request->headers); |
107
|
|
|
|
108
|
|
|
$this->assertArrayHasKey(CURLOPT_HTTPHEADER, $request->options); |
109
|
|
|
$this->assertArraySubset($normalOfBothHeaders, $request->options[CURLOPT_HTTPHEADER]); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function testOptions() |
113
|
|
|
{ |
114
|
|
|
$request = new Request(); |
115
|
|
|
|
116
|
|
|
$opt = [CURLOPT_CRLF => 'test', CURLOPT_AUTOREFERER => 'test']; |
117
|
|
|
$request->options = $opt; |
118
|
|
|
|
119
|
|
|
$this->assertArrayHasKey(CURLOPT_CRLF, $request->options); |
120
|
|
|
$this->assertArrayHasKey(CURLOPT_AUTOREFERER, $request->options); |
121
|
|
|
$this->assertArraySubset($opt, $request->options); |
122
|
|
|
|
123
|
|
|
$opt2 = [CURLOPT_AUTOREFERER => 'no-test', CURLOPT_BINARYTRANSFER => 'no-test']; |
124
|
|
|
$request->options = $opt2; |
125
|
|
|
|
126
|
|
|
$this->assertArrayHasKey(CURLOPT_CRLF, $request->options); |
127
|
|
|
$this->assertArrayHasKey(CURLOPT_AUTOREFERER, $request->options); |
128
|
|
|
$this->assertArrayHasKey(CURLOPT_BINARYTRANSFER, $request->options); |
129
|
|
|
$this->assertArraySubset($opt2 + $opt, $request->options); |
130
|
|
|
} |
131
|
|
|
} |