ResponseTest.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. use Workerman\Protocols\Http\Response;
  3. it('test some simple case', function () {
  4. $response = new Response(201, ['X-foo' => 'bar'], 'hello, xiami');
  5. expect($response->getStatusCode())->toBe(201)
  6. ->and($response->getHeaders())->toBe(['X-foo' => 'bar'])
  7. ->and($response->rawBody())->toBe('hello, xiami');
  8. //headers
  9. $response->header('abc', '123');
  10. $response->withHeader('X-foo', 'baz');
  11. $response->withHeaders(['def' => '456']);
  12. expect((string)$response)
  13. ->toContain('X-foo: baz')
  14. ->toContain('abc: 123')
  15. ->toContain('def: 456');
  16. $response->withoutHeader('def');
  17. expect((string)$response)->not->toContain('def: 456')
  18. ->and($response->getHeader('abc'))
  19. ->toBe('123');
  20. $response->withStatus(202, 'some reason');
  21. expect($response->getReasonPhrase())->toBe('some reason');
  22. $response->withProtocolVersion('1.0');
  23. $response->withBody('hello, world');
  24. expect((string)$response)
  25. ->toContain('HTTP/1.0')
  26. ->toContain('hello, world')
  27. ->toContain('Content-Type: ')
  28. ->toContain('Content-Length: 12')
  29. ->not()->toContain('Transfer-Encoding: ');
  30. //cookie
  31. $response->cookie('foo', 'bar', domain: 'xia.moe', httpOnly: true);
  32. expect((string)$response)
  33. ->toContain('Set-Cookie: foo=bar; Domain=xia.moe; HttpOnly');
  34. });
  35. it('tests file', function (){
  36. //todo may have to redo the simple test,
  37. // as the implementation of headers is a different function for files.
  38. // or actually maybe the Response is the one should be rewritten to reuse?
  39. $response = new Response();
  40. $tmpFile = tempnam(sys_get_temp_dir(), 'test');
  41. rename($tmpFile, $tmpFile .'.jpg');
  42. $tmpFile .= '.jpg';
  43. file_put_contents($tmpFile, 'hello, xiami');
  44. $response->withFile($tmpFile, 0, 12);
  45. expect((string)$response)
  46. ->toContain('Content-Type: image/jpeg')
  47. ->toContain('Last-Modified: ');
  48. });