describe("Media", function () { 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);
$response = new Response(); expect($response->getStatusCode())->toEqual(200); }); }); describe("->getReasonPhrase()", function () { it("gets the reason phrase code", function () { $response = new Response(); expect($response->getReasonPhrase())->toEqual('OK'); }); }); describe("->withStatus()", function () { it("creates a new response with the provided status code & reason", function () { $response = new Response(); $new = $response->withStatus(404, 'Page Not Found'); expect($response->getStatusCode())->toBe(200); expect($response->getReasonPhrase())->toBe('OK'); expect($new)->not->toBe($response); expect($new->getStatusCode())->toBe(404); expect($new->getReasonPhrase())->toBe('Page Not Found'); }); it("creates a new response with the provided status code", function () { $response = new Response(); $new = $response->withStatus(404); expect($response->getStatusCode())->toBe(200); expect($response->getReasonPhrase())->toBe('OK'); expect($new)->not->toBe($response); expect($new->getStatusCode())->toBe(404); expect($new->getReasonPhrase())->toBe('Not Found'); }); }); });
$response = Response::parse($message); expect($response->headers->cookies->data())->toBe($cookies); expect($response->toMessage())->toBe($message); }); it("decodes chunked body", function () { $headers = join("\r\n", ['HTTP/1.1 200 OK', 'Date: Mon, 22 Mar 2004 11:15:03 GMT', 'Content-Type: text/html', 'Transfer-Encoding: chunked', '', '']); $body = "29\r\n"; $body .= "<html><body><p>The file you requested is \r\n"; $body .= "6\r\n"; $body .= "3,400 \r\n"; $body .= "22\r\n"; $body .= "bytes long and was last modified: \r\n"; $body .= "1d\r\n"; $body .= "Fri, 25 Dec 2015 00:00:00 GMT\r\n"; $body .= "13\r\n"; $body .= ".</p></body></html>\r\n"; $body .= "0\r\n"; $response = Response::parse($headers . $body); $expected = <<<EOD <html><body><p>The file you requested is 3,400 bytes long and was last modified: Fri, 25 Dec 2015 00:00:00 GMT.</p></body></html> EOD; expect($response->body())->toBe($expected); }); it("throws an exception if the message can't be parsed", function () { $closure = function () { Response::parse(''); }; expect($closure)->toThrow(new NetException('The CRLFCRLF separator between headers and body is missing.')); }); }); });