HttpTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. /** @var TcpConnection $tcpConnection */
  49. $tcpConnection = Mockery::mock(TcpConnection::class);
  50. $tcpConnection->headers = [
  51. 'foo' => 'bar',
  52. 'jhdxr' => ['a', 'b'],
  53. ];
  54. $extHeader = "foo: bar\r\n" .
  55. "jhdxr: a\r\n" .
  56. "jhdxr: b\r\n";
  57. expect(Http::encode('xiami', $tcpConnection))
  58. ->toBe("HTTP/1.1 200 OK\r\n" .
  59. "Server: workerman\r\n" .
  60. "{$extHeader}Connection: keep-alive\r\n" .
  61. "Content-Type: text/html;charset=utf-8\r\n" .
  62. "Content-Length: 5\r\n\r\nxiami");
  63. });
  64. it('tests ::encode for ' . Response::class, function () {
  65. /** @var TcpConnection $tcpConnection */
  66. $tcpConnection = Mockery::mock(TcpConnection::class);
  67. $tcpConnection->headers = [
  68. 'foo' => 'bar',
  69. 'jhdxr' => ['a', 'b'],
  70. ];
  71. $extHeader = "foo: bar\r\n" .
  72. "jhdxr: a\r\n" .
  73. "jhdxr: b\r\n";
  74. $response = new Response(body: 'xiami');
  75. expect(Http::encode($response, $tcpConnection))
  76. ->toBe("HTTP/1.1 200 OK\r\n" .
  77. "Server: workerman\r\n" .
  78. "{$extHeader}Connection: keep-alive\r\n" .
  79. "Content-Type: text/html;charset=utf-8\r\n" .
  80. "Content-Length: 5\r\n\r\nxiami");
  81. });
  82. it('tests ::decode', function () {
  83. /** @var TcpConnection $tcpConnection */
  84. $tcpConnection = Mockery::mock(TcpConnection::class);
  85. //example request from ChatGPT :)
  86. $buffer = "POST /path/to/resource HTTP/1.1\r\n" .
  87. "Host: example.com\r\n" .
  88. "Content-Type: application/json\r\n" .
  89. "Content-Length: 27\r\n" .
  90. "\r\n" .
  91. '{"key": "value", "foo": "bar"}';
  92. $value = expect(Http::decode($buffer, $tcpConnection))
  93. ->toBeInstanceOf(Request::class)
  94. ->value;
  95. //test cache
  96. expect($value == Http::decode($buffer, $tcpConnection))
  97. ->toBeTrue();
  98. });