HttpTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. //content-length exceeds connection max package size
  36. testWithConnectionClose(function (TcpConnection $tcpConnection) use ($buffer) {
  37. $tcpConnection->maxPackageSize = 10;
  38. expect(Http::input($buffer, $tcpConnection))
  39. ->toBe(0);
  40. }, '413 Payload Too Large');
  41. });
  42. it('tests ::encode for non-object response', function () {
  43. /** @var TcpConnection $tcpConnection */
  44. $tcpConnection = Mockery::mock(TcpConnection::class);
  45. $tcpConnection->headers = [
  46. 'foo' => 'bar',
  47. 'jhdxr' => ['a', 'b'],
  48. ];
  49. $extHeader = "foo: bar\r\n" .
  50. "jhdxr: a\r\n" .
  51. "jhdxr: b\r\n";
  52. expect(Http::encode('xiami', $tcpConnection))
  53. ->toBe("HTTP/1.1 200 OK\r\n" .
  54. "Server: workerman\r\n" .
  55. "{$extHeader}Connection: keep-alive\r\n" .
  56. "Content-Type: text/html;charset=utf-8\r\n" .
  57. "Content-Length: 5\r\n\r\nxiami");
  58. });
  59. it('tests ::encode for ' . Response::class, function () {
  60. /** @var TcpConnection $tcpConnection */
  61. $tcpConnection = Mockery::mock(TcpConnection::class);
  62. $tcpConnection->headers = [
  63. 'foo' => 'bar',
  64. 'jhdxr' => ['a', 'b'],
  65. ];
  66. $extHeader = "foo: bar\r\n" .
  67. "jhdxr: a\r\n" .
  68. "jhdxr: b\r\n";
  69. $response = new Response(body: 'xiami');
  70. expect(Http::encode($response, $tcpConnection))
  71. ->toBe("HTTP/1.1 200 OK\r\n" .
  72. "Server: workerman\r\n" .
  73. "{$extHeader}Connection: keep-alive\r\n" .
  74. "Content-Type: text/html;charset=utf-8\r\n" .
  75. "Content-Length: 5\r\n\r\nxiami");
  76. });
  77. it('tests ::decode', function () {
  78. /** @var TcpConnection $tcpConnection */
  79. $tcpConnection = Mockery::mock(TcpConnection::class);
  80. //example request from ChatGPT :)
  81. $buffer = "POST /path/to/resource HTTP/1.1\r\n" .
  82. "Host: example.com\r\n" .
  83. "Content-Type: application/json\r\n" .
  84. "Content-Length: 27\r\n" .
  85. "\r\n" .
  86. '{"key": "value", "foo": "bar"}';
  87. $value = expect(Http::decode($buffer, $tcpConnection))
  88. ->toBeInstanceOf(Request::class)
  89. ->value;
  90. //test cache
  91. expect($value == Http::decode($buffer, $tcpConnection))
  92. ->toBeTrue();
  93. });