Example #1
0
        it("negotiates a format from a request", function () {
            $request = new Request();
            $response = new Response();
            $request->headers['Accept'] = "text/html;q=0.2,application/json,application/xml;q=0.9,*/*;q=0.8";
            $response->negotiate($request);
            expect($response->format())->toBe('json');
            $request->headers['Accept'] = "text/html,application/json;q=0.2,application/xml;q=0.9,*/*;q=0.8";
            $response->negotiate($request);
            expect($response->format())->toBe('html');
        });
        it("throws an exception if the response format can't be negotiated", function () {
            $closure = function () {
                $request = new Request();
                $response = new Response();
                $request->headers['Accept'] = "application/vnd.api+json";
                $response->negotiate($request);
            };
            expect($closure)->toThrow(new NetException('Unsupported Media Type: ["application/vnd.api+json"].', 415));
        });
    });
    describe("->toMessage()", function () {
        it("casts the response as a string", function () {
            $response = new Response(['format' => 'json', 'data' => ['hello' => 'world']]);
            $cookies = $response->headers->cookies;
            $cookies['foo'] = 'bar';
            $cookies['bin'] = 'baz';
            $cookies['foo'] = ['value' => 'bin', 'path' => '/foo'];
            $expected = <<<EOD
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 17