Example #1
0
 afterEach(function () {
     Media::reset();
 });
 describe("::set()", function () {
     it("supports custom handlers", function () {
         Media::set('csv', ['application/csv'], ['encode' => function ($data) {
             ob_start();
             $out = fopen('php://output', 'w');
             foreach ($data as $record) {
                 fputcsv($out, $record);
             }
             fclose($out);
             return ob_get_clean();
         }]);
         $response = new Response();
         $response->format('csv');
         $data = [['John', 'Doe', '123 Main St.', 'Anytown, CA', '91724'], ['Jane', 'Doe', '124 Main St.', 'Anytown, CA', '91724']];
         $response->set($data);
         $expected = 'John,Doe,"123 Main St.","Anytown, CA",91724' . "\n";
         $expected .= 'Jane,Doe,"124 Main St.","Anytown, CA",91724' . "\n";
         expect($response->body())->toBe($expected);
         expect((string) $response->headers['Content-Type'])->toBe('Content-Type: application/csv');
     });
 });
 describe("::remove()", function () {
     it("remove a format", function () {
         Media::set('csv', []);
         Media::set('pdf', []);
         expect(Media::get('csv'))->not->toBe(null);
         expect(Media::get('pdf'))->not->toBe(null);
         Media::remove(['csv', 'pdf']);
Example #2
0
Set-Cookie: foo1=bar1; Path=/
Set-Cookie: foo2=bar2; Path=/
Set-Cookie: foo3=bar3; Path=/


EOD;
            expect($response->headers->to('header'))->toBe($expected);
        });
    });
    describe("->negotiate()", function () {
        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 () {