Swoole 请求转 Leevel 请求

Testing Is Documentation

tests/Protocol/Swoole2LeevelTest.phpSwoole 请求转 Leevel 请求 - 图1

Swoole 请求转换 Leevel 的请求后,然后传递给 Kernel 完成请求到响应的过程。

Uses

  1. <?php
  2. use Leevel\Http\Request;
  3. use Leevel\Protocol\Swoole2Leevel;
  4. use Swoole\Http\Request as SwooleHttpRequest;

转换 Swoole 请求的 header

  1. public function testSwooleRequestWithHeader(): void
  2. {
  3. $swoole2Leevel = new Swoole2Leevel();
  4. $wooleRequest = new SwooleHttpRequest();
  5. $wooleRequest->header = [
  6. 'HOST' => '127.0.0.1',
  7. 'REFERER' => 'https://www.queryphp.com',
  8. 'foo' => 'bar',
  9. ];
  10. $request = $swoole2Leevel->createRequest($wooleRequest);
  11. $this->assertInstanceOf(Request::class, $request);
  12. $data = <<<'eot'
  13. {
  14. "HTTP_HOST": "127.0.0.1",
  15. "HTTP_REFERER": "https:\/\/www.queryphp.com",
  16. "HTTP_FOO": "bar"
  17. }
  18. eot;
  19. $this->assertSame(
  20. $data,
  21. $this->varJson(
  22. $request->server->all()
  23. )
  24. );
  25. $data = <<<'eot'
  26. {
  27. "host": [
  28. "127.0.0.1"
  29. ],
  30. "referer": [
  31. "https:\/\/www.queryphp.com"
  32. ],
  33. "foo": [
  34. "bar"
  35. ]
  36. }
  37. eot;
  38. $this->assertSame(
  39. $data,
  40. $this->varJson(
  41. $this->getFilterHeaders($request->headers->all()),
  42. 1
  43. )
  44. );
  45. }

转换 Swoole 请求的 server

  1. public function testSwooleRequestWithServer(): void
  2. {
  3. $swoole2Leevel = new Swoole2Leevel();
  4. $wooleRequest = new SwooleHttpRequest();
  5. $wooleRequest->header = [
  6. 'HOST' => '127.0.0.1',
  7. 'REFERER' => 'https://www.queryphp.com',
  8. 'foo' => 'bar',
  9. ];
  10. $wooleRequest->server = [
  11. 'SERVER_ADDR' => '127.0.0.1',
  12. 'SERVER_NAME' => 'localhost',
  13. 'SERVER_SOFTWARE' => 'Apache/2.2.22 (Win64) PHP/7.4.0',
  14. 'SERVER_PROTOCOL' => 'HTTP/1.1',
  15. 'REQUEST_METHOD' => 'GET',
  16. ];
  17. $request = $swoole2Leevel->createRequest($wooleRequest);
  18. $this->assertInstanceOf(Request::class, $request);
  19. $data = <<<'eot'
  20. {
  21. "HTTP_HOST": "127.0.0.1",
  22. "HTTP_REFERER": "https:\/\/www.queryphp.com",
  23. "HTTP_FOO": "bar",
  24. "SERVER_ADDR": "127.0.0.1",
  25. "SERVER_NAME": "localhost",
  26. "SERVER_SOFTWARE": "Apache\/2.2.22 (Win64) PHP\/7.4.0",
  27. "SERVER_PROTOCOL": "HTTP\/1.1",
  28. "REQUEST_METHOD": "GET"
  29. }
  30. eot;
  31. $this->assertSame(
  32. $data,
  33. $this->varJson(
  34. $request->server->all()
  35. )
  36. );
  37. $data = <<<'eot'
  38. {
  39. "host": [
  40. "127.0.0.1"
  41. ],
  42. "referer": [
  43. "https:\/\/www.queryphp.com"
  44. ],
  45. "foo": [
  46. "bar"
  47. ]
  48. }
  49. eot;
  50. $this->assertSame(
  51. $data,
  52. $this->varJson(
  53. $this->getFilterHeaders($request->headers->all()),
  54. 1
  55. )
  56. );
  57. }

转换 Swoole 请求的其它属性

  1. public function testSwooleRequestWithOther(): void
  2. {
  3. $swoole2Leevel = new Swoole2Leevel();
  4. $wooleRequest = new SwooleHttpRequest();
  5. $wooleRequest->get = [
  6. 'foo' => 'bar',
  7. ];
  8. $wooleRequest->post = [
  9. 'hello' => 'world',
  10. ];
  11. $request = $swoole2Leevel->createRequest($wooleRequest);
  12. $this->assertInstanceOf(Request::class, $request);
  13. $data = <<<'eot'
  14. {
  15. "foo": "bar"
  16. }
  17. eot;
  18. $this->assertSame(
  19. $data,
  20. $this->varJson(
  21. $request->query->all()
  22. )
  23. );
  24. $data = <<<'eot'
  25. {
  26. "hello": "world"
  27. }
  28. eot;
  29. $this->assertSame(
  30. $data,
  31. $this->varJson(
  32. $request->request->all(),
  33. 1
  34. )
  35. );
  36. }