half2me /
curlx
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 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 | use CurlX\Request; |
||
| 12 | use CurlX\RequestInterface; |
||
| 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 testNotify() |
||
| 51 | { |
||
| 52 | $request = new Request(); |
||
| 53 | $called1 = false; |
||
| 54 | $called2 = false; |
||
| 55 | $r1 = null; |
||
| 56 | $r2 = null; |
||
| 57 | |||
| 58 | $request->addListener(function (RequestInterface $var) use (&$called1, &$r1) { |
||
| 59 | $called1 = true; |
||
| 60 | $r1 = $var; |
||
| 61 | }); |
||
| 62 | |||
| 63 | $request->addListener(function (RequestInterface $var) use (&$called2, &$r2) { |
||
| 64 | $called2 = true; |
||
| 65 | $r2 = $var; |
||
| 66 | }); |
||
| 67 | |||
| 68 | $request->callBack([]); |
||
| 69 | |||
| 70 | $this->assertTrue($called1, 'Callback 1 was not notified on request completion!'); |
||
| 71 | $this->assertInstanceOf('CurlX\RequestInterface', $r1, 'Callback 1 did not receive the request object'); |
||
| 72 | $this->assertTrue($called2, 'Callback 2 was not notified on request completion!'); |
||
| 73 | $this->assertInstanceOf('CurlX\RequestInterface', $r2, 'Callback 2 did not receive the request object'); |
||
| 74 | } |
||
| 75 | |||
| 76 | public function testHeaders() |
||
| 77 | { |
||
| 78 | $request = new Request(); |
||
| 79 | |||
| 80 | $header = ['a' => 'aaa', 'b' => 'bbb']; |
||
| 81 | $normalHeader = ['a: aaa', 'b: bbb']; |
||
| 82 | |||
| 83 | $request->headers = $header; |
||
| 84 | $this->assertEquals($header, $request->headers); |
||
| 85 | |||
| 86 | $this->assertArrayHasKey(CURLOPT_HTTPHEADER, $request->options); |
||
| 87 | $this->assertEquals($normalHeader, $request->options[CURLOPT_HTTPHEADER]); |
||
| 88 | |||
| 89 | $header2 = ['b' => 'BBB', 'c' => 'CCC']; |
||
| 90 | $normalOfBothHeaders = ['b: BBB', 'c: CCC', 'a: aaa']; |
||
| 91 | $request->headers = $header2; |
||
| 92 | $this->assertEquals($header2 + $header, $request->headers); |
||
| 93 | |||
| 94 | $this->assertArrayHasKey(CURLOPT_HTTPHEADER, $request->options); |
||
| 95 | $this->assertArraySubset($normalOfBothHeaders, $request->options[CURLOPT_HTTPHEADER]); |
||
| 96 | } |
||
| 97 | |||
| 98 | public function testOptions() |
||
| 99 | { |
||
| 100 | $request = new Request(); |
||
| 101 | |||
| 102 | $opt = [CURLOPT_CRLF => 'test', CURLOPT_AUTOREFERER => 'test']; |
||
| 103 | $request->options = $opt; |
||
| 104 | |||
| 105 | $this->assertArrayHasKey(CURLOPT_CRLF, $request->options); |
||
| 106 | $this->assertArrayHasKey(CURLOPT_AUTOREFERER, $request->options); |
||
| 107 | $this->assertArraySubset($opt, $request->options); |
||
| 108 | |||
| 109 | $opt2 = [CURLOPT_AUTOREFERER => 'no-test', CURLOPT_BINARYTRANSFER => 'no-test']; |
||
| 110 | $request->options = $opt2; |
||
| 111 | |||
| 112 | $this->assertArrayHasKey(CURLOPT_CRLF, $request->options); |
||
| 113 | $this->assertArrayHasKey(CURLOPT_AUTOREFERER, $request->options); |
||
| 114 | $this->assertArrayHasKey(CURLOPT_BINARYTRANSFER, $request->options); |
||
| 115 | $this->assertArraySubset($opt2 + $opt, $request->options); |
||
| 116 | } |
||
| 117 | |||
| 118 | public function testHandle() |
||
| 119 | { |
||
| 120 | $request = new Request('http://example.com'); |
||
| 121 | $ch = $request->handle; |
||
| 122 | |||
| 123 | $this->assertNotNull($ch); |
||
| 124 | $info = curl_getinfo($ch); |
||
| 125 | |||
| 126 | $this->assertEquals($request->url, $info['url']); |
||
| 127 | |||
| 128 | // Test default cUrl options |
||
| 129 | $this->assertArrayHasKey(CURLOPT_RETURNTRANSFER, $request->options); |
||
| 130 | $this->assertEquals($request->options[CURLOPT_RETURNTRANSFER], true); |
||
| 131 | } |
||
| 132 | |||
| 133 | public function testClone() |
||
| 134 | { |
||
| 135 | // We make a Request object and set parameters |
||
| 136 | $request = new Request('http://url.url'); |
||
| 137 | $request->addListener(function (RequestInterface $r) { |
||
|
0 ignored issues
–
show
|
|||
| 138 | }); |
||
| 139 | $request->post_data = ['a' => 'a']; |
||
| 140 | $request->headers = ['a' => 'a']; |
||
| 141 | $request->timeout = 5; |
||
| 142 | $request->options = [CURLOPT_BINARYTRANSFER => true]; |
||
| 143 | |||
| 144 | // We clone it, and overwrite the parameters |
||
| 145 | $request2 = clone $request; |
||
| 146 | $request2->url = 'http://url2.url2'; |
||
| 147 | $iWasCalled = false; |
||
| 148 | $request2->addListener(function (RequestInterface $r) use (&$iWasCalled) { |
||
|
0 ignored issues
–
show
|
|||
| 149 | $iWasCalled = true; |
||
| 150 | }); |
||
| 151 | $request2->post_data = ['a' => 'A', 'b' => 'B']; |
||
| 152 | $request2->headers = ['a' => 'A', 'b' => 'B']; |
||
| 153 | $request2->timeout = 10; |
||
| 154 | $request2->options = [CURLOPT_BINARYTRANSFER => false]; |
||
| 155 | |||
| 156 | // Modifying the new object should not change values of the original one |
||
| 157 | $this->assertEquals('http://url.url', $request->url); |
||
| 158 | $this->assertEquals(['a' => 'a'], $request->post_data); |
||
| 159 | $this->assertEquals(['a' => 'a'], $request->headers); |
||
| 160 | $this->assertEquals(5, $request->timeout); |
||
| 161 | $this->assertArraySubset([CURLOPT_BINARYTRANSFER => true], $request->options); |
||
| 162 | |||
| 163 | // Running the first Request should not notify the cloned Request's listeners |
||
| 164 | $request->callBack([]); |
||
| 165 | $this->assertFalse($iWasCalled); |
||
| 166 | } |
||
| 167 | } |
||
| 168 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.