diff --git a/tests/Unit/Protocols/Http/ResponseTest.php b/tests/Unit/Protocols/Http/ResponseTest.php new file mode 100644 index 000000000..aefa26ba6 --- /dev/null +++ b/tests/Unit/Protocols/Http/ResponseTest.php @@ -0,0 +1,56 @@ + 'bar'], 'hello, xiami'); + + expect($response->getStatusCode())->toBe(201); + expect($response->getHeaders())->toBe(['X-foo' => 'bar']); + expect($response->rawBody())->toBe('hello, xiami'); + + //headers + $response->header('abc', '123'); + $response->withHeader('X-foo', 'baz'); + $response->withHeaders(['def' => '456']); + expect((string)$response) + ->toContain('X-foo: baz') + ->toContain('abc: 123') + ->toContain('def: 456'); + $response->withoutHeader('def'); + expect((string)$response)->not->toContain('def: 456'); + expect($response->getHeader('abc'))->toBe('def'); + + $response->withStatus(202, 'some reason'); + expect($response->getReasonPhrase())->toBe('some reason'); + + $response->withProtocolVersion('1.0'); + $response->withBody('hello, world'); + expect((string)$response) + ->toContain('HTTP/1.0') + ->toContain('hello, world') + ->toContain('Content-Type: ') + ->toContain('Content-Length: 12') + ->not()->toContain('Transfer-Encoding: '); + + + //cookie + $response->cookie('foo', 'bar', httpOnly: true, domain: 'xia.moe'); + expect((string)$response) + ->toContain('Set-Cookie: foo=bar; Domain=xia.moe; HttpOnly'); +}); + +it('tests file', function (){ + //todo may have to redo the simple test, + // as the implementation of headers is a different function for files. + // or actually maybe the Response is the one should be rewritten to reuse? + $response = new Response(); + $tmpFile = tempnam(sys_get_temp_dir(), 'test'); + rename($tmpFile, $tmpFile .'.jpg'); + $tmpFile .= '.jpg'; + file_put_contents($tmpFile, 'hello, xiami'); + $response->withFile($tmpFile, 0, 12); + expect((string)$response) + ->toContain('Content-Type: image/jpeg') + ->toContain('Last-Modified: '); +}); \ No newline at end of file