/**
     * Test so 201 statuscode will update the location on the resource.
     *
     * @return void
     */
    public function testApplyPost201UpdatedLocation()
    {
        $curl = new Klarna_Checkout_HTTP_TransportStub;

        $payload = '{"flobadob":["bobcat","wookie"]}';
        $location = 'not localhost';

        $data = array(
            'code' => 201,
            'headers' => array('Location' => $location),
            'payload' => $payload
        );
        $curl->addResponse($data);

        $this->orderStub->parse(json_decode($payload, true));

        $expected = 'stnaeu\eu2341aoaaoae==';

        $this->digest->expects($this->once())
            ->method('create')
            ->with("{$payload}aboogie")
            ->will($this->returnValue($expected));

        $object = new Klarna_Checkout_BasicConnector(
            $curl,
            $this->digest,
            'aboogie'
        );

        $this->assertNull($this->orderStub->getLocation(), 'Original Location');

        $result = $object->apply('POST', $this->orderStub);

        $this->assertEquals($payload, $result->getData(), 'Response payload');
        $this->assertEquals(
            "Klarna {$expected}",
            $curl->request->getHeader('Authorization'),
            'Header'
        );

        $this->assertEquals(
            $location,
            $this->orderStub->getLocation(),
            'Location should have beet updated'
        );

        $this->assertEquals(
            json_decode($payload, true),
            $this->orderStub->marshal(),
            'The data on the resource should not be updated!'
        );

        $this->assertEquals(
            $this->orderStub->getContentType(),
            $curl->request->getHeader('Accept'),
            'Accept Content Type'
        );
    }
    /**
     * Test apply with a 200 code
     *
     * @return void
     */
    public function testApplyGet200()
    {
        $curl = new Klarna_Checkout_HTTP_TransportStub;

        $payload = '{"flobadob":["bobcat","wookie"]}';
        $data = array(
            'code' => 200,
            'headers' => array(),
            'payload' => $payload
        );
        $curl->addResponse($data);

        $expectedDigest = 'stnaeu\eu2341aoaaoae==';

        $this->digest->expects($this->once())
            ->method('create')
            ->with('aboogie')
            ->will($this->returnValue($expectedDigest));

        $object = new Klarna_Checkout_BasicConnector(
            $curl,
            $this->digest,
            'aboogie'
        );
        $result = $object->apply('GET', $this->orderStub);

        $this->assertEquals($payload, $result->getData(), 'Response payload');
        $this->assertEquals(
            "Klarna {$expectedDigest}",
            $curl->request->getHeader('Authorization'),
            'Header'
        );

        $this->assertEquals(
            json_decode($payload, true),
            $this->orderStub->marshal(),
            'Content'
        );

        $this->assertEquals(
            $this->orderStub->getContentType(),
            $curl->request->getHeader('Accept'),
            'Accept Content Type'
        );
    }
Esempio n. 3
0
    /**
     * Set content (headers, payload) on a request
     *
     * @param Klarna_Checkout_ResourceInterface $resource Klarna Checkout Resource
     * @param string                            $method   HTTP Method
     * @param string                            $payload  Payload to send with the
     *                                                    request
     * @param string                            $url      URL for request
     *
     * @return Klarna_Checkout_HTTP_Request
     */
    protected function createRequest(
        Klarna_Checkout_ResourceInterface $resource,
        $method,
        $payload,
        $url
    ) {
        // Generate the digest string
        $digest = $this->digester->create($payload . $this->_secret);

        $request = $this->http->createRequest($url);

        $request->setMethod($method);

        // Set HTTP Headers
        $request->setHeader('User-Agent', (string)$this->userAgent());
        $request->setHeader('Authorization', "Klarna {$digest}");
        $request->setHeader('Accept', $resource->getContentType());
        if (strlen($payload) > 0) {
            $request->setHeader('Content-Type', $resource->getContentType());
            $request->setData($payload);
        }

        return $request;
    }