Esempio n. 1
0
    /**
     * Make sure that getting and setting headers work as intended.
     *
     * @return void
     */
    public function testSetGetHeader()
    {
        $this->assertEquals(0, count($this->request->getHeaders()));

        $this->request->setHeader('test', 'value');

        $this->assertEquals(1, count($this->request->getHeaders()));
        $this->assertEquals('value', $this->request->getHeader('test'));
        $this->assertEquals(
            array('test' => 'value'), $this->request->getHeaders()
        );

        $this->assertEquals(null, $this->request->getHeader('undefined'));
    }
Esempio n. 2
0
 /**
  * Performs a HTTP request.
  *
  * @param Klarna_Checkout_HTTP_Request $request the HTTP request to send.
  *
  * @throws RuntimeException                Thrown if a cURL handle cannot
  *                                         be initialized.
  * @throws Klarna_Checkout_ConnectionErrorException Thrown for unspecified
  *                                                  network or hardware issues.
  * @return Klarna_Checkout_HTTP_Response
  */
 public function send(Klarna_Checkout_HTTP_Request $request)
 {
     $curl = $this->curl->handle();
     if ($curl === false) {
         throw new RuntimeException('Failed to initialize a HTTP handle.');
     }
     $url = $request->getURL();
     $curl->setOption(CURLOPT_URL, $url);
     $method = $request->getMethod();
     if ($method === 'POST') {
         $curl->setOption(CURLOPT_POST, true);
         $curl->setOption(CURLOPT_POSTFIELDS, $request->getData());
     }
     // Convert headers to cURL format.
     $requestHeaders = array();
     foreach ($request->getHeaders() as $key => $value) {
         $requestHeaders[] = $key . ': ' . $value;
     }
     $curl->setOption(CURLOPT_HTTPHEADER, $requestHeaders);
     $curl->setOption(CURLOPT_RETURNTRANSFER, true);
     $curl->setOption(CURLOPT_CONNECTTIMEOUT, $this->timeout);
     $curl->setOption(CURLOPT_TIMEOUT, $this->timeout);
     $curlHeaders = new Klarna_Checkout_HTTP_CURLHeaders();
     $curl->setOption(CURLOPT_HEADERFUNCTION, array(&$curlHeaders, 'processHeader'));
     $curl->setOption(CURLOPT_SSL_VERIFYHOST, 2);
     $curl->setOption(CURLOPT_SSL_VERIFYPEER, true);
     // Override specific set options
     foreach ($this->options as $option => $value) {
         $curl->setOption($option, $value);
     }
     $payload = $curl->execute();
     $info = $curl->getInfo();
     $error = $curl->getError();
     $curl->close();
     /*
      * A failure occurred if:
      * payload is false (e.g. HTTP timeout?).
      * info is false, then it has no HTTP status code.
      */
     if ($payload === false || $info === false) {
         throw new Klarna_Checkout_ConnectionErrorException("Connection to '{$url}' failed: {$error}");
     }
     $headers = $curlHeaders->getHeaders();
     // Convert Content-Type into a normal header
     $headers['Content-Type'] = $info['content_type'];
     $response = new Klarna_Checkout_HTTP_Response($request, $headers, intval($info['http_code']), strval($payload));
     return $response;
 }
Esempio n. 3
0
 /**
  * Performs a HTTP request.
  *
  * @param  Klarna_Checkout_HTTP_Request $request the HTTP request to send.
  *
  * @throws Klarna_Checkout_ConnectionErrorException Thrown for unspecified
  *                                                    network or hardware issues.
  * @return Klarna_Checkout_HTTP_Response
  */
 public function send(Klarna_Checkout_HTTP_Request $request)
 {
     // Set arguments for wp_remote_request
     $args = array('method' => $request->getMethod(), 'headers' => $request->getHeaders(), 'body' => $request->getData(), 'timeout' => $this->getTimeout());
     // For GET requests we need to get Klarna order URI, set in WC session
     if ('GET' == $request->getMethod() && WC()->session->get('klarna_request_uri')) {
         $req_url = WC()->session->get('klarna_request_uri');
     } else {
         $req_url = $request->getURL();
     }
     $my_response = wp_remote_request($req_url, $args);
     if (!is_wp_error($my_response)) {
         $response = new Klarna_Checkout_HTTP_Response($request, $request->getHeaders(), intval($my_response['response']['code']), strval($my_response['body']));
     }
     // Set order URI as session value for GET request
     if ('POST' == $request->getMethod()) {
         WC()->session->__unset('klarna_request_uri');
         if (isset($my_response['headers']['location'])) {
             $klarna_request_uri = $my_response['headers']['location'];
         } else {
             $klarna_request_uri = $request->getURL();
         }
         WC()->session->set('klarna_request_uri', $klarna_request_uri);
     }
     return $response;
 }