From ffa618c932a8745730117d4646c5089a33e58588 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Thing=20Andersen?= Date: Mon, 6 Jul 2015 09:46:32 +0200 Subject: [PATCH 1/3] Support for storing the rheaders sent and received. Useful for debuging. --- QuickPay/api/Request.php | 88 ++++++++++++++++++++++++++++++++++++++++ README.md | 14 ++++++- 2 files changed, 101 insertions(+), 1 deletion(-) diff --git a/QuickPay/api/Request.php b/QuickPay/api/Request.php index c58c441..aafe03b 100644 --- a/QuickPay/api/Request.php +++ b/QuickPay/api/Request.php @@ -20,6 +20,27 @@ class Request */ protected $client; + /** + * Should sent and received headers be available after the request? + * + * @var boolean + */ + protected $store_headers = false; + + /** + * The headers sent during the last request. + * + * @var string + */ + protected $sent_headers; + + /** + * The headers received during the last request. + * + * @var string + */ + protected $received_headers; + /** * __construct function. @@ -175,6 +196,16 @@ protected function execute( $request_type, $form = array() ) curl_setopt( $this->client->ch, CURLOPT_POSTFIELDS, http_build_query($form) ); } + if ($this->store_headers) + { + // Store headers in temporary memory file + $fh_header = fopen('php://memory', 'w+'); + curl_setopt($this->client->ch, CURLOPT_WRITEHEADER, $fh_header); + curl_setopt($this->client->ch, CURLINFO_HEADER_OUT, true); + $this->sent_headers = null; + $this->received_headers = null; + } + // Execute the request $response_data = curl_exec( $this->client->ch ); @@ -183,10 +214,67 @@ protected function execute( $request_type, $form = array() ) throw new Exception(curl_error($this->client->ch), curl_errno($this->client->ch)); } + if ($this->store_headers) + { + $this->sent_headers = curl_getinfo($this->client->ch, CURLINFO_HEADER_OUT); + rewind($fh_header); + $this->received_headers = stream_get_contents($fh_header); + fclose($fh_header); + } + // Retrieve the HTTP response code $response_code = (int) curl_getinfo( $this->client->ch, CURLINFO_HTTP_CODE ); // Return the response object. return new Response( $response_code, $response_data ); } + + /** + * Enable or disable the storing of headers during requests. + * + * @param boolean $enable + */ + public function store_headers( $enable = true ) + { + $this->store_headers = (boolean) $enable; + } + + /** + * Returns the headers sent in the last request. + * Requires a call to $this->store_headers() before performing the request. + * + * @param boolan $keep_authorization_value Normally the value of the Authorization: header is masked. True keeps the sent value. + * @return string + */ + public function get_sent_headers( $keep_authorization_value = false) + { + if ($keep_authorization_value ) + { + return $this->sent_headers; + } + else + { + // Avoid dependency on mbstring + $lines = explode( "\n", $this->sent_headers ); + foreach ( $lines as &$line ) + { + if (strpos($line, 'Authorization: ') === 0) { + $line = 'Authorization: '; + } + } + return implode("\n", $lines); + } + } + + /** + * Returns the headers received in the last request. + * Requires a call to $this->store_headers() before performing the request. + * + * @return string + */ + public function get_received_headers() + { + return $this->received_headers; + } + } diff --git a/README.md b/README.md index 74a5b76..18c222b 100644 --- a/README.md +++ b/README.md @@ -118,4 +118,16 @@ foreach( $payments as $payment ) { ``` -You can read more about api responses at [http://tech.quickpay.net/api/](http://tech.quickpay.net/api). \ No newline at end of file +You can read more about api responses at [http://tech.quickpay.net/api/](http://tech.quickpay.net/api). + +### Debugging +To see the headers sent and received use the three functions `store_headers()`, `get_sent_headers()`, and `get_received_headers()`. + +```php5 +$client->request->store_headers(); +$response = $client->request->get('/payments'); +$sent_headers = $client->request->get_sent_headers(); // Masks the value of the Authorization: header. +$sent_headers = $client->request->get_sent_headers(true); // Keeps the value of the Authorization: header. +$received_headers = $client->request->get_received_headers(); +$client->request->store_headers(false); // Disable storing the headers +``` From 0a0a875a30d9e1aaa1b96afd3a4904c5753d1495 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Thing=20Andersen?= Date: Mon, 6 Jul 2015 11:32:32 +0200 Subject: [PATCH 2/3] Reworked as requeted by Tonni. --- QuickPay/api/Request.php | 100 +++++--------------------------------- QuickPay/api/Response.php | 78 ++++++++++++++++++++++++----- README.md | 16 +----- 3 files changed, 80 insertions(+), 114 deletions(-) diff --git a/QuickPay/api/Request.php b/QuickPay/api/Request.php index aafe03b..3910bcb 100644 --- a/QuickPay/api/Request.php +++ b/QuickPay/api/Request.php @@ -20,28 +20,6 @@ class Request */ protected $client; - /** - * Should sent and received headers be available after the request? - * - * @var boolean - */ - protected $store_headers = false; - - /** - * The headers sent during the last request. - * - * @var string - */ - protected $sent_headers; - - /** - * The headers received during the last request. - * - * @var string - */ - protected $received_headers; - - /** * __construct function. * @@ -196,85 +174,31 @@ protected function execute( $request_type, $form = array() ) curl_setopt( $this->client->ch, CURLOPT_POSTFIELDS, http_build_query($form) ); } - if ($this->store_headers) - { - // Store headers in temporary memory file - $fh_header = fopen('php://memory', 'w+'); - curl_setopt($this->client->ch, CURLOPT_WRITEHEADER, $fh_header); - curl_setopt($this->client->ch, CURLINFO_HEADER_OUT, true); - $this->sent_headers = null; - $this->received_headers = null; - } + // Store received headers in temporary memory file, remember sent headers + $fh_header = fopen('php://memory', 'w+'); + curl_setopt($this->client->ch, CURLOPT_WRITEHEADER, $fh_header); + curl_setopt($this->client->ch, CURLINFO_HEADER_OUT, true); // Execute the request $response_data = curl_exec( $this->client->ch ); - if (curl_errno($this->client->ch) !== 0) { + if (curl_errno($this->client->ch) !== 0) { //An error occurred + fclose($fh_header); throw new Exception(curl_error($this->client->ch), curl_errno($this->client->ch)); } - if ($this->store_headers) - { - $this->sent_headers = curl_getinfo($this->client->ch, CURLINFO_HEADER_OUT); - rewind($fh_header); - $this->received_headers = stream_get_contents($fh_header); - fclose($fh_header); - } + // Grab the headers + $sent_headers = curl_getinfo($this->client->ch, CURLINFO_HEADER_OUT); + rewind($fh_header); + $received_headers = stream_get_contents($fh_header); + fclose($fh_header); // Retrieve the HTTP response code $response_code = (int) curl_getinfo( $this->client->ch, CURLINFO_HTTP_CODE ); // Return the response object. - return new Response( $response_code, $response_data ); + return new Response( $response_code, $sent_headers, $received_headers, $response_data ); } - /** - * Enable or disable the storing of headers during requests. - * - * @param boolean $enable - */ - public function store_headers( $enable = true ) - { - $this->store_headers = (boolean) $enable; - } - - /** - * Returns the headers sent in the last request. - * Requires a call to $this->store_headers() before performing the request. - * - * @param boolan $keep_authorization_value Normally the value of the Authorization: header is masked. True keeps the sent value. - * @return string - */ - public function get_sent_headers( $keep_authorization_value = false) - { - if ($keep_authorization_value ) - { - return $this->sent_headers; - } - else - { - // Avoid dependency on mbstring - $lines = explode( "\n", $this->sent_headers ); - foreach ( $lines as &$line ) - { - if (strpos($line, 'Authorization: ') === 0) { - $line = 'Authorization: '; - } - } - return implode("\n", $lines); - } - } - - /** - * Returns the headers received in the last request. - * Requires a call to $this->store_headers() before performing the request. - * - * @return string - */ - public function get_received_headers() - { - return $this->received_headers; - } - } diff --git a/QuickPay/api/Response.php b/QuickPay/api/Response.php index 9be4f9f..56df190 100644 --- a/QuickPay/api/Response.php +++ b/QuickPay/api/Response.php @@ -10,20 +10,49 @@ */ class Response { + /** + * HTTP status code of request. + * + * @var integer + */ protected $status_code; + + /** + * The headers sent during the request. + * + * @var string + */ + protected $sent_headers; + + /** + * The headers received during the request. + * + * @var string + */ + protected $received_headers; + + /** + * Response body of last request. + * + * @var string + */ protected $response_data; - - /** + + /** * __construct * * Instantiates a new response object * - * @param int $response_code the http_status code + * @param int $status_code the HTTP status code + * @param string $sent_headers the headers sent + * @param string $received_headers the headers received * @param string $response_data the http response body */ - public function __construct( $response_code, $response_data ) + public function __construct( $status_code, $sent_headers, $received_headers, $response_data ) { - $this->response_code = $response_code; + $this->status_code = $status_code; + $this->sent_headers = $sent_headers; + $this->received_headers = $received_headers; $this->response_data = $response_data; } @@ -31,16 +60,41 @@ public function __construct( $response_code, $response_data ) /** * as_raw * - * Returns the raw response body + * Returns the HTTP status code, headers and response body. + * Usage: list($status_code, $headers, $response_body) = $response->as_raw(). * - * @return string + * @param boolan $keep_authorization_value Normally the value of the Authorization: header is masked. True keeps the sent value. + * @return array [integer, string[], string] */ - public function as_raw() + public function as_raw( $keep_authorization_value = false ) { - return $this->response_data; + // To avoid unintentional logging of credentials the default is to mask the value of the Authorization: header + if ($keep_authorization_value ) + { + $sent_headers = $this->sent_headers; + } + else + { + // Avoid dependency on mbstring + $lines = explode( "\n", $this->sent_headers ); + foreach ( $lines as &$line ) + { + if (strpos($line, 'Authorization: ') === 0) { + $line = 'Authorization: '; + } + } + $sent_headers = implode("\n", $lines); + } + + return [ + $this->status_code, + [ 'sent' => $sent_headers, + 'received' => $this->received_headers, + ], + $this->response_data, + ]; } - /** * as_array * @@ -86,7 +140,7 @@ public function as_object() */ public function http_status() { - return $this->response_code; + return $this->status_code; } @@ -99,7 +153,7 @@ public function http_status() */ public function is_success() { - if( $this->response_code > 299 ) + if( $this->status_code > 299 ) { return false; } diff --git a/README.md b/README.md index 18c222b..95b76c9 100644 --- a/README.md +++ b/README.md @@ -99,8 +99,8 @@ if( $status == 200 ) { The returned response object supports 3 different ways of returning the response body, `as_raw()`, `as_object`, `as_array()`. ```php5 -// Get the raw response body -$response_body = $client->request->get('/payments')->as_raw(); +// Get the HTTP status code, headers and raw response body. +list($status_code, $headers, $response_body) = $client->request->get('/payments')->as_raw(); // Get the response body as an object $response_body = $client->request->get('/payments')->as_object(); @@ -119,15 +119,3 @@ foreach( $payments as $payment ) { ``` You can read more about api responses at [http://tech.quickpay.net/api/](http://tech.quickpay.net/api). - -### Debugging -To see the headers sent and received use the three functions `store_headers()`, `get_sent_headers()`, and `get_received_headers()`. - -```php5 -$client->request->store_headers(); -$response = $client->request->get('/payments'); -$sent_headers = $client->request->get_sent_headers(); // Masks the value of the Authorization: header. -$sent_headers = $client->request->get_sent_headers(true); // Keeps the value of the Authorization: header. -$received_headers = $client->request->get_received_headers(); -$client->request->store_headers(false); // Disable storing the headers -``` From a6168e9e28d6d05f511a841b5d787d5d52defcfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Thing=20Andersen?= Date: Mon, 6 Jul 2015 12:54:18 +0200 Subject: [PATCH 3/3] Directory QuickPay/api/ upprcased to QuickPay/API/ to match namespace composer.json updated so the autoloader actually works. Use statements in test cases updated with correct case. ResponseTest updated for changes to Response in previous commit. --- QuickPay/{api => API}/Client.php | 0 QuickPay/{api => API}/Constants.php | 0 QuickPay/{api => API}/Exception.php | 0 QuickPay/{api => API}/Request.php | 16 ++++++++-------- QuickPay/{api => API}/Response.php | 0 QuickPay/QuickPay.php | 10 +++++----- QuickPay/Tests/api/ExceptionTest.php | 2 +- QuickPay/Tests/api/RequestTest.php | 6 +++--- QuickPay/Tests/api/ResponseTest.php | 20 +++++++++++--------- README.md | 8 ++++++++ composer.json | 4 ++-- 11 files changed, 38 insertions(+), 28 deletions(-) rename QuickPay/{api => API}/Client.php (100%) rename QuickPay/{api => API}/Constants.php (100%) rename QuickPay/{api => API}/Exception.php (100%) rename QuickPay/{api => API}/Request.php (96%) rename QuickPay/{api => API}/Response.php (100%) diff --git a/QuickPay/api/Client.php b/QuickPay/API/Client.php similarity index 100% rename from QuickPay/api/Client.php rename to QuickPay/API/Client.php diff --git a/QuickPay/api/Constants.php b/QuickPay/API/Constants.php similarity index 100% rename from QuickPay/api/Constants.php rename to QuickPay/API/Constants.php diff --git a/QuickPay/api/Exception.php b/QuickPay/API/Exception.php similarity index 100% rename from QuickPay/api/Exception.php rename to QuickPay/API/Exception.php diff --git a/QuickPay/api/Request.php b/QuickPay/API/Request.php similarity index 96% rename from QuickPay/api/Request.php rename to QuickPay/API/Request.php index 3910bcb..faf7ce7 100644 --- a/QuickPay/api/Request.php +++ b/QuickPay/API/Request.php @@ -1,8 +1,8 @@ is_success(); @@ -43,7 +43,7 @@ public function providerTestSuccessResponseHTTPCodes() */ public function testReturnOfHTTPStatusCodes($httpCode, $expectedCode) { - $response = new Response($httpCode, ''); + $response = new Response($httpCode, '', '', ''); $statusCode = $response->http_status(); @@ -61,7 +61,7 @@ public function providerTestReturnOfHTTPStatusCodes() public function testReturnOfResponseDataAsArray() { - $response = new Response(200, $this->responseTestData); + $response = new Response(200, '', '', $this->responseTestData); $responseArray = $response->as_array(); @@ -70,7 +70,7 @@ public function testReturnOfResponseDataAsArray() public function testReturnOfEmptyResponseDataAsArray() { - $response = new Response(200, ''); + $response = new Response(200, '', '', ''); $responseArray = $response->as_array(); @@ -79,7 +79,7 @@ public function testReturnOfEmptyResponseDataAsArray() public function testReturnOfResponseDataAsObject() { - $response = new Response(200, $this->responseTestData); + $response = new Response(200, '', '', $this->responseTestData); $responseObject = $response->as_object(); @@ -88,7 +88,7 @@ public function testReturnOfResponseDataAsObject() public function testReturnOfEmptyResponseDataAsObject() { - $response = new Response(200, ''); + $response = new Response(200, '', '', ''); $responseObject = $response->as_object(); @@ -97,10 +97,12 @@ public function testReturnOfEmptyResponseDataAsObject() public function testReturnOfResponseDataAsRaw() { - $response = new Response(200, $this->responseTestData); + $response = new Response(200, '', '', $this->responseTestData); - $responseRaw = $response->as_raw(); + list($statusCode, $headers, $responseRaw) = $response->as_raw(); + $this->assertTrue( is_int($statusCode) ); + $this->assertTrue( is_array($headers) ); $this->assertTrue( is_string($responseRaw) ); } diff --git a/README.md b/README.md index 95b76c9..797fcbe 100644 --- a/README.md +++ b/README.md @@ -119,3 +119,11 @@ foreach( $payments as $payment ) { ``` You can read more about api responses at [http://tech.quickpay.net/api/](http://tech.quickpay.net/api). + +## Tests + +Use composer to create an autoloader: +```command +$ composer update +$ phpunit --bootstrap vendor/autoload.php +``` diff --git a/composer.json b/composer.json index 7100635..3d1d90a 100644 --- a/composer.json +++ b/composer.json @@ -6,8 +6,8 @@ "phpunit/phpunit": "4.8.*@dev" }, "autoload": { - "psr-0": { - "QuickPay": "" + "psr-4": { + "QuickPay\\": "QuickPay/" } } } \ No newline at end of file