HttpTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. use Workerman\Connection\TcpConnection;
  3. use Workerman\Protocols\Http;
  4. use Workerman\Protocols\Http\Request;
  5. use Workerman\Protocols\Http\Response;
  6. it('customizes request class', function () {
  7. //backup old request class
  8. $oldRequestClass = Http::requestClass();
  9. //actual test
  10. $class = new class {
  11. };
  12. Http::requestClass($class::class);
  13. expect(Http::requestClass())->toBe($class::class);
  14. //restore old request class
  15. Http::requestClass($oldRequestClass);
  16. });
  17. it('tests ::input', function () {
  18. //test 413 payload too large
  19. testWithConnectionClose(function (TcpConnection $tcpConnection) {
  20. expect(Http::input(str_repeat('jhdxr', 3333), $tcpConnection))
  21. ->toBe(0);
  22. }, '413 Payload Too Large');
  23. //example request from ChatGPT :)
  24. $buffer = "POST /path/to/resource HTTP/1.1\r\n" .
  25. "Host: example.com\r\n" .
  26. "Content-Type: application/json\r\n" .
  27. "Content-Length: 27\r\n" .
  28. "\r\n" .
  29. '{"key": "value", "foo": "bar"}';
  30. //unrecognized method
  31. testWithConnectionClose(function (TcpConnection $tcpConnection) use ($buffer) {
  32. expect(Http::input(str_replace('POST', 'MIAOWU', $buffer), $tcpConnection))
  33. ->toBe(0);
  34. }, '400 Bad Request');
  35. //HTTP 1.1 without Host header
  36. testWithConnectionClose(function (TcpConnection $tcpConnection) use ($buffer) {
  37. expect(Http::input(str_replace("Host: ", 'NotHost: ', $buffer), $tcpConnection))
  38. ->toBe(0);
  39. }, '400 Bad Request');
  40. //content-length exceeds connection max package size
  41. testWithConnectionClose(function (TcpConnection $tcpConnection) use ($buffer) {
  42. $tcpConnection->maxPackageSize = 10;
  43. expect(Http::input($buffer, $tcpConnection))
  44. ->toBe(0);
  45. }, '413 Payload Too Large');
  46. });
  47. it('tests ::encode for non-object response', function () {
  48. $tcpConnection = Mockery::mock(TcpConnection::class);
  49. $tcpConnection->headers = [
  50. 'foo' => 'bar',
  51. 'jhdxr' => ['a', 'b'],
  52. ];
  53. $extHeader = "foo: bar\r\n" .
  54. "jhdxr: a\r\n" .
  55. "jhdxr: b\r\n";
  56. expect(Http::encode('xiami', $tcpConnection))
  57. ->toBe("HTTP/1.1 200 OK\r\n" .
  58. "Server: workerman\r\n" .
  59. "{$extHeader}Connection: keep-alive\r\n" .
  60. "Content-Type: text/html;charset=utf-8\r\n" .
  61. "Content-Length: 5\r\n\r\nxiami");
  62. });
  63. it('tests ::encode for ' . Response::class, function () {
  64. $tcpConnection = Mockery::mock(TcpConnection::class);
  65. $tcpConnection->headers = [
  66. 'foo' => 'bar',
  67. 'jhdxr' => ['a', 'b'],
  68. ];
  69. $extHeader = "foo: bar\r\n" .
  70. "jhdxr: a\r\n" .
  71. "jhdxr: b\r\n";
  72. $response = new Response(body: 'xiami');
  73. expect(Http::encode($response, $tcpConnection))
  74. ->toBe("HTTP/1.1 200 OK\r\n" .
  75. "Server: workerman\r\n" .
  76. "{$extHeader}Connection: keep-alive\r\n" .
  77. "Content-Type: text/html;charset=utf-8\r\n" .
  78. "Content-Length: 5\r\n\r\nxiami");
  79. });
  80. it('tests ::decode', function () {
  81. $tcpConnection = Mockery::mock(TcpConnection::class);
  82. //example request from ChatGPT :)
  83. $buffer = "POST /path/to/resource HTTP/1.1\r\n" .
  84. "Host: example.com\r\n" .
  85. "Content-Type: application/json\r\n" .
  86. "Content-Length: 27\r\n" .
  87. "\r\n" .
  88. '{"key": "value", "foo": "bar"}';
  89. $value = expect(Http::decode($buffer, $tcpConnection))
  90. ->toBeInstanceOf(Request::class)
  91. ->value;
  92. //test cache
  93. expect($value == Http::decode($buffer, $tcpConnection))
  94. ->toBeTrue();
  95. });