1
0

RequestTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /*
  3. * Copyright 2026 Alessandro Chiapparini <sprint_planer_web@chiapparini.org>
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * See the LICENSE file in the project root for the full license text.
  9. */
  10. declare(strict_types=1);
  11. namespace App\Tests\Http;
  12. use App\Http\Request;
  13. use App\Tests\TestCase;
  14. /**
  15. * Smoke tests that `Request::ip()` and `Request::isHttps()` actually consult
  16. * `TRUSTED_PROXIES` (R01-N05 / R01-N07). The detail-level CIDR / XFF logic
  17. * is exercised in `TrustedProxiesTest`; here we only check the wiring.
  18. */
  19. final class RequestTest extends TestCase
  20. {
  21. /**
  22. * @param array<string,mixed> $server
  23. */
  24. private function makeRequest(array $server): Request
  25. {
  26. return new Request(
  27. method: 'GET',
  28. path: '/',
  29. query: [],
  30. post: [],
  31. rawBody: '',
  32. headers: [],
  33. server: $server,
  34. );
  35. }
  36. public function testIpReturnsRemoteAddrWhenNoTrustedProxiesConfigured(): void
  37. {
  38. $prev = getenv('TRUSTED_PROXIES');
  39. try {
  40. putenv('TRUSTED_PROXIES');
  41. $req = $this->makeRequest([
  42. 'REMOTE_ADDR' => '203.0.113.42',
  43. 'HTTP_X_FORWARDED_FOR' => '198.51.100.7',
  44. ]);
  45. self::assertSame('203.0.113.42', $req->ip());
  46. } finally {
  47. $prev === false ? putenv('TRUSTED_PROXIES') : putenv('TRUSTED_PROXIES=' . $prev);
  48. }
  49. }
  50. public function testIpHonoursXffWhenRemoteIsTrusted(): void
  51. {
  52. $prev = getenv('TRUSTED_PROXIES');
  53. try {
  54. putenv('TRUSTED_PROXIES=10.0.0.0/8');
  55. $req = $this->makeRequest([
  56. 'REMOTE_ADDR' => '10.0.0.1',
  57. 'HTTP_X_FORWARDED_FOR' => '198.51.100.7',
  58. ]);
  59. self::assertSame('198.51.100.7', $req->ip());
  60. } finally {
  61. $prev === false ? putenv('TRUSTED_PROXIES') : putenv('TRUSTED_PROXIES=' . $prev);
  62. }
  63. }
  64. public function testIsHttpsHonoursXfpOnlyFromTrustedProxy(): void
  65. {
  66. $prev = getenv('TRUSTED_PROXIES');
  67. try {
  68. putenv('TRUSTED_PROXIES=10.0.0.0/8');
  69. $trustedReq = $this->makeRequest([
  70. 'REMOTE_ADDR' => '10.0.0.1',
  71. 'HTTP_X_FORWARDED_PROTO' => 'https',
  72. ]);
  73. self::assertTrue($trustedReq->isHttps());
  74. $untrustedReq = $this->makeRequest([
  75. 'REMOTE_ADDR' => '203.0.113.5',
  76. 'HTTP_X_FORWARDED_PROTO' => 'https',
  77. ]);
  78. self::assertFalse($untrustedReq->isHttps());
  79. } finally {
  80. $prev === false ? putenv('TRUSTED_PROXIES') : putenv('TRUSTED_PROXIES=' . $prev);
  81. }
  82. }
  83. public function testIsHttpsRecognisesDirectTls(): void
  84. {
  85. $req = $this->makeRequest(['HTTPS' => 'on']);
  86. self::assertTrue($req->isHttps());
  87. $req = $this->makeRequest(['HTTPS' => 'off']);
  88. self::assertFalse($req->isHttps());
  89. }
  90. // ------------------------------------------------------------------
  91. // R01-N24: body size cap
  92. // ------------------------------------------------------------------
  93. public function testMaxBodyBytesCapIsExactlyOneMebibyte(): void
  94. {
  95. // Drift fence — bumping this changes a published HTTP contract
  96. // (clients that rely on the 413 boundary). Update REVIEW_01.md
  97. // §R01-N24, public/index.php's error message, and any per-cap
  98. // operator docs alongside the value here.
  99. self::assertSame(1024 * 1024, Request::MAX_BODY_BYTES);
  100. }
  101. public function testBodyTooLargeFlagDefaultsToFalse(): void
  102. {
  103. $req = $this->makeRequest([]);
  104. self::assertFalse($req->bodyTooLarge);
  105. }
  106. public function testBodyTooLargeFlagWiresThroughConstructor(): void
  107. {
  108. // The front controller (`public/index.php`) reads this property to
  109. // emit a 413 before dispatch. A future refactor must not lose the
  110. // wiring or the cap silently disappears.
  111. $req = new Request(
  112. method: 'POST',
  113. path: '/sprints/1/week-cells',
  114. query: [],
  115. post: [],
  116. rawBody: '',
  117. headers: [],
  118. server: [],
  119. bodyTooLarge: true,
  120. );
  121. self::assertTrue($req->bodyTooLarge);
  122. }
  123. public function testJsonReturnsNullWhenBodyWasOversized(): void
  124. {
  125. // `fromGlobals()` blanks `rawBody` once it decides the request was
  126. // oversized, so the existing `json()` parser naturally returns null.
  127. // Pin that downstream-safety contract — controllers that fall back
  128. // to `?? []` continue to work; the front-controller 413 has
  129. // already replied so this branch should never run in production,
  130. // but defending against a misuse path is cheap.
  131. $req = new Request(
  132. method: 'POST',
  133. path: '/x',
  134. query: [],
  135. post: [],
  136. rawBody: '',
  137. headers: ['content-type' => 'application/json'],
  138. server: [],
  139. bodyTooLarge: true,
  140. );
  141. self::assertNull($req->json());
  142. }
  143. }