diff --git a/4dev/tests/UrlRequests/CoreLibsUrlRequestsCurlTest.php b/4dev/tests/UrlRequests/CoreLibsUrlRequestsCurlTest.php index 5982efa1..9e4432e4 100644 --- a/4dev/tests/UrlRequests/CoreLibsUrlRequestsCurlTest.php +++ b/4dev/tests/UrlRequests/CoreLibsUrlRequestsCurlTest.php @@ -732,7 +732,7 @@ final class CoreLibsUrlRequestsCurlTest extends TestCase ); } - // MARK: test basi call provider + // MARK: test basic call provider /** * Undocumented function @@ -976,11 +976,61 @@ final class CoreLibsUrlRequestsCurlTest extends TestCase ); } + // MARK: auth header set via config + + /** + * Test auth settings and auth override + * + * @testdox UrlRequests\Curl auth test call + * + * @return void + */ + public function testUrlRequestsCurlAuthHeader() + { + $curl = new \CoreLibs\UrlRequests\Curl([ + "auth" => ["user", "pass", "basic"], + "http_errors" => false, + ]); + $curl->request('get', $this->url_basic); + // check that the auth header matches + $this->assertContains( + "Authorization:Basic dXNlcjpwYXNz", + $curl->getHeadersSent() + ); + // if we sent new request with auth header, this one should not be used + $curl->request('get', $this->url_basic, [ + "headers" => ["Authorization" => "Failed"] + ]); + // check that the auth header matches + $this->assertContains( + "Authorization:Basic dXNlcjpwYXNz", + $curl->getHeadersSent() + ); + // override auth: reset + $curl->request('get', $this->url_basic, [ + "auth" => null + ]); + $this->assertNotContains( + "Authorization:Basic dXNlcjpwYXNz", + $curl->getHeadersSent() + ); + // override auth: different auth + $curl->request('get', $this->url_basic, [ + "auth" => ["user2", "pass2", "basic"] + ]); + // check that the auth header matches + $this->assertContains( + "Authorization:Basic dXNlcjI6cGFzczI=", + $curl->getHeadersSent() + ); + } + // MARK: test exceptions /** - * Undocumented function + * Exception:InvalidRequestType * + * @covers ::request * @testdox UrlRequests\Curl Exception:InvalidRequestType * * @return void @@ -1006,9 +1056,10 @@ final class CoreLibsUrlRequestsCurlTest extends TestCase // } /** - * Undocumented function + * Exception:CurlExecError * - * @testdox UrlRequests\Curl Exception:CurlError + * @covers ::request + * @testdox UrlRequests\Curl Exception:CurlExecError * * @return void */ @@ -1024,6 +1075,7 @@ final class CoreLibsUrlRequestsCurlTest extends TestCase /** * Exception:ClientError * + * @covers ::request * @testdox UrlRequests\Curl Exception:ClientError * * @return void @@ -1033,7 +1085,7 @@ final class CoreLibsUrlRequestsCurlTest extends TestCase $curl = new \CoreLibs\UrlRequests\Curl(["http_errors" => true]); $this->expectException(\RuntimeException::class); $this->expectExceptionMessageMatches("/ClientError/"); - $curl->get($this->url_basic, [ + $curl->request('get', $this->url_basic, [ "headers" => [ "Authorization" => "schmalztiegel", "RunAuthTest" => "yes", @@ -1044,6 +1096,7 @@ final class CoreLibsUrlRequestsCurlTest extends TestCase /** * Exception:ClientError * + * @covers ::request * @testdox UrlRequests\Curl Exception:ClientError on call enable * * @return void @@ -1053,7 +1106,7 @@ final class CoreLibsUrlRequestsCurlTest extends TestCase $curl = new \CoreLibs\UrlRequests\Curl(["http_errors" => false]); $this->expectException(\RuntimeException::class); $this->expectExceptionMessageMatches("/ClientError/"); - $curl->get($this->url_basic, [ + $curl->request('get', $this->url_basic, [ "headers" => [ "Authorization" => "schmalztiegel", "RunAuthTest" => "yes", @@ -1065,6 +1118,7 @@ final class CoreLibsUrlRequestsCurlTest extends TestCase /** * Exception:ClientError * + * @covers ::request * @testdox UrlRequests\Curl Exception:ClientError unset on call * * @return void @@ -1073,7 +1127,7 @@ final class CoreLibsUrlRequestsCurlTest extends TestCase { // if true, with false it has to be off $curl = new \CoreLibs\UrlRequests\Curl(["http_errors" => true]); - $response = $curl->get($this->url_basic, [ + $response = $curl->request('get', $this->url_basic, [ "headers" => [ "Authorization" => "schmalztiegel", "RunAuthTest" => "yes", @@ -1087,7 +1141,7 @@ final class CoreLibsUrlRequestsCurlTest extends TestCase ); // if false, null should not change it $curl = new \CoreLibs\UrlRequests\Curl(["http_errors" => false]); - $response = $curl->get($this->url_basic, [ + $response = $curl->request('get', $this->url_basic, [ "headers" => [ "Authorization" => "schmalztiegel", "RunAuthTest" => "yes", @@ -1100,24 +1154,6 @@ final class CoreLibsUrlRequestsCurlTest extends TestCase 'Unset Exception failed with null' ); } - - /** - * Undocumented function - * - * @testdox UrlRequests\Curl Exception:DuplicatedArrayKey - * - * @return void - */ - public function testExceptionDuplicatedArrayKey(): void - { - $curl = new \CoreLibs\UrlRequests\Curl(); - $this->expectException(\UnexpectedValueException::class); - $this->expectExceptionMessageMatches("/DuplicatedArrayKey/"); - $curl->prepareHeaders([ - 'header-double:a', - 'header-double:b', - ]); - } } // __END__ diff --git a/www/admin/class_test.url-requests.curl.php b/www/admin/class_test.url-requests.curl.php index 87fa0053..f4a0848a 100644 --- a/www/admin/class_test.url-requests.curl.php +++ b/www/admin/class_test.url-requests.curl.php @@ -40,11 +40,11 @@ $data = $client->get( 'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/UrlRequests.target.php' . '?other=get_a', [ - 'headers' => $client->prepareHeaders([ - 'test-header: ABC', - 'info-request-type: _GET', + 'headers' => [ + 'test-header' => 'ABC', + 'info-request-type' => '_GET', 'Funk-pop' => 'Semlly god' - ]), + ], 'query' => ['foo' => 'BAR'] ] ); @@ -78,11 +78,11 @@ $data = $client->request( . 'trunk/www/admin/UrlRequests.target.php' . '?other=get_a', [ - "headers" => $client->prepareHeaders([ - 'test-header: ABC', - 'info-request-type: _GET', + "headers" => [ + 'test-header' => 'ABC', + 'info-request-type' => '_GET', 'Funk-pop' => 'Semlly god' - ]), + ], "query" => ['foo' => 'BAR'], ], ); @@ -94,12 +94,12 @@ $data = $client->post( . '?other=post_a', [ 'body' => ['payload' => 'data post'], - 'headers' => $client->prepareHeaders([ - 'Content-Type: application/json', - 'Accept: application/json', - 'test-header: ABC', - 'info-request-type: _POST' - ]), + 'headers' => [ + 'Content-Type' => 'application/json', + 'Accept' => 'application/json', + 'test-header' => 'ABC', + 'info-request-type' => '_POST', + ], 'query' => ['foo' => 'BAR post'], ] ); @@ -111,12 +111,12 @@ $data = $client->request( . '?other=post_a', [ "body" => ['payload' => 'data post', 'request' => 'I am the request body'], - "headers" => $client->prepareHeaders([ - 'Content-Type: application/json', - 'Accept: application/json', - 'test-header: ABC', - 'info-request-type: _POST' - ]), + "headers" => [ + 'Content-Type' => 'application/json', + 'Accept' => 'application/json', + 'test-header' => 'ABC', + 'info-request-type' => '_POST', + ], "query" => ['foo' => 'BAR post'], ] ); @@ -128,12 +128,12 @@ $data = $client->put( . '?other=put_a', [ "body" => ['payload' => 'data put'], - "headers" => $client->prepareHeaders([ - 'Content-Type: application/json', - 'Accept: application/json', - 'test-header: ABC', - 'info-request-type: _PUT' - ]), + "headers" => [ + 'Content-Type' => 'application/json', + 'Accept' => 'application/json', + 'test-header' => 'ABC', + 'info-request-type' => '_PUT', + ], 'query' => ['foo' => 'BAR put'], ] ); @@ -145,12 +145,12 @@ $data = $client->patch( . '?other=patch_a', [ "body" => ['payload' => 'data patch'], - "headers" => $client->prepareHeaders([ - 'Content-Type: application/json', - 'Accept: application/json', - 'test-header: ABC', - 'info-request-type: _PATCH' - ]), + "headers" => [ + 'Content-Type' => 'application/json', + 'Accept' => 'application/json', + 'test-header' => 'ABC', + 'info-request-type' => '_PATCH', + ], 'query' => ['foo' => 'BAR patch'], ] ); @@ -162,12 +162,12 @@ $data = $client->delete( . '?other=delete_no_body_a', [ "body" => null, - "headers" => $client->prepareHeaders([ - 'Content-Type: application/json', - 'Accept: application/json', - 'test-header: ABC', - 'info-request-type: _DELETE' - ]), + "headers" => [ + 'Content-Type' => 'application/json', + 'Accept' => 'application/json', + 'test-header' => 'ABC', + 'info-request-type' => '_DELETE', + ], "query" => ['foo' => 'BAR delete'], ] ); @@ -179,12 +179,12 @@ $data = $client->delete( . '?other=delete_body_a', [ "body" => ['payload' => 'data delete'], - "headers" => $client->prepareHeaders([ - 'Content-Type: application/json', - 'Accept: application/json', - 'test-header: ABC', - 'info-request-type: _DELETE' - ]), + "headers" => [ + 'Content-Type' => 'application/json', + 'Accept' => 'application/json', + 'test-header' => 'ABC', + 'info-request-type' => '_DELETE', + ], "query" => ['foo' => 'BAR delete'], ] ); @@ -294,6 +294,24 @@ try { } catch (Exception $e) { print "Exception:
" . print_r(json_decode($e->getMessage(), true), true) . "
" . print_r($response, true) . ""; + print "[uc] SENT URL: " . $uc->getUrlSent() . "
" . print_r($uc->getUrlParsedSent(), true) . ""; + print "[uc] SENT HEADERS:
" . print_r($uc->getHeadersSent(), true) . ""; +} catch (Exception $e) { + print "Exception:
" . print_r(json_decode($e->getMessage(), true), true) . "
" . print_r($uc->getUrlParsedSent(), true) . ""; print "[uc] SENT HEADERS:
" . print_r($uc->getHeadersSent(), true) . ""; +print "
" . print_r($response, true) . ""; +print "[uc] SENT URL: " . $uc->getUrlSent() . "
" . print_r($uc->getUrlParsedSent(), true) . ""; +print "[uc] SENT HEADERS:
" . print_r($uc->getHeadersSent(), true) . ""; + print "