/** * Test the parse_url method. * * @return array * * @covers Asika\Http\Uri\UriHelper::parseUrl * @since 1.0 */ public function testParseUrl() { $url = 'http://localhost/Windwalker_development/j16_trunk/administrator/index.php?option=com_contact&view=contact&layout=edit&id=5'; $expected = parse_url($url); $actual = UriHelper::parseUrl($url); $this->assertEquals($expected, $actual, 'Line: ' . __LINE__ . ' Results should be equal'); // Test all parts of query $url = 'https://*****:*****@www.google.com:80/folder/page.html#id?var=kay&var2=key&true'; $expected = parse_url($url); $actual = UriHelper::parseUrl($url); $this->assertEquals($expected, $actual, 'Line: ' . __LINE__ . ' Results should be equal'); // Test special characters in URL $url = 'http://Windwalker.org/mytestpath/È'; $expected = parse_url($url); // Fix up path for UTF-8 characters $expected['path'] = '/mytestpath/È'; $actual = UriHelper::parseUrl($url); $this->assertEquals($expected, $actual, 'Line: ' . __LINE__ . ' Results should be equal'); // Test special characters in URL $url = 'http://mydomain.com/!*\'();:@&=+$,/?%#[]" \\'; $expected = parse_url($url); $actual = UriHelper::parseUrl($url); $this->assertEquals($expected, $actual, 'Line: ' . __LINE__ . ' Results should be equal'); // Test url encoding in URL $url = 'http://mydomain.com/%21%2A%27%28%29%3B%3A%40%26%3D%24%2C%2F%3F%25%23%5B%22%20%5C'; $expected = parse_url($url); $actual = UriHelper::parseUrl($url); $this->assertEquals($expected, $actual, 'Line: ' . __LINE__ . ' Results should be equal'); // Test a mix of the above $url = 'http://*****:*****@mydomain.com:80/%È21%25È3*%('; $expected = parse_url($url); // Fix up path for UTF-8 characters $expected['path'] = '/%È21%25È3*%('; $actual = UriHelper::parseUrl($url); $this->assertEquals($expected, $actual, 'Line: ' . __LINE__ . ' Results should be equal'); // Test invalild URL $url = 'http:///mydomain.com'; $expected = parse_url($url); $actual = UriHelper::parseUrl($url); $this->assertEquals($expected, $actual, 'Line: ' . __LINE__ . ' Results should be equal'); }
/** * testRequestPut * * @return void */ public function testRequestPut() { $request = $this->createRequest(); $request = $request->withUri(new PsrUri(ASIKA_HTTP_TEST_URL))->withMethod('PUT'); $request->getBody()->write(UriHelper::buildQuery(array('foo' => 'bar'))); $response = $this->instance->request($request); $data = json_decode($response->getBody()->getContents(), true); $this->assertEquals(array('foo' => 'bar'), $data['data']); $this->assertEquals('PUT', $data['_SERVER']['REQUEST_METHOD']); }
/** * Return an instance with the specified URI fragment. * * This method MUST retain the state of the current instance, and return * an instance that contains the specified URI fragment. * * Users can provide both encoded and decoded fragment characters. * Implementations ensure the correct encoding as outlined in getFragment(). * * An empty fragment value is equivalent to removing the fragment. * * @param string $fragment The fragment to use with the new instance. * * @return static A new instance with the specified fragment. */ public function withFragment($fragment) { $fragment = UriHelper::filterFragment($fragment); $new = clone $this; $new->fragment = $fragment; return $new; }
/** * Prepare Request object to send request. * * @param RequestInterface $request The Psr Request object. * @param string $method The method type. * @param string|object $url The URL to request, may be string or Uri object. * @param mixed $data The request body data, can be an array of POST data. * @param array $headers The headers array. * * @return RequestInterface */ protected function prepareRequest(RequestInterface $request, $method, $url, $data, $headers) { $url = (string) $url; // If is GET, we merge data into URL. if (strtoupper($method) == 'GET' && is_array($data)) { $url = new Uri($url); foreach ($data as $k => $v) { $url->setVar($k, $v); } $url = (string) $url; $data = null; } // If not GET, convert data to query string. if (is_array($data)) { $data = UriHelper::buildQuery($data); } /** @var RequestInterface $request */ $request->getBody()->write((string) $data); $request = $request->withRequestTarget((string) new PsrUri($url))->withMethod($method); // Set global headers foreach ((array) $this->getOption('headers') as $key => $value) { $request = $request->withHeader($key, $value); } // Override with this method foreach ($headers as $key => $value) { $request = $request->withHeader($key, $value); } return $request; }
/** * Method to test patch(). * * @return void * * @covers Asika\Http\HttpClient::patch */ public function testPatch() { $url = 'http://example.com/?foo=bar'; $data = array('flower' => 'sakura'); $headers = array('X-Foo' => 'Bar'); $this->instance->patch($url, $data, $headers); $this->assertEquals('PATCH', $this->transport->request->getMethod()); $this->assertEquals($url, $this->transport->request->getRequestTarget()); $this->assertEquals('Bar', $this->transport->request->getHeaderLine('X-Foo')); $this->assertEquals(UriHelper::buildQuery($data), $this->transport->request->getBody()->__toString()); }