xref: /PHP-8.3/ext/standard/tests/http/gh11274.phpt (revision 1ede3137)
1--TEST--
2GH-11274 (POST/PATCH request via file_get_contents + stream_context_create switches to GET after a HTTP 308 redirect)
3--INI--
4allow_url_fopen=1
5--CONFLICTS--
6server
7--FILE--
8<?php
9$serverCode = <<<'CODE'
10$uri = $_SERVER['REQUEST_URI'];
11if (isset($_GET["desired_status"]) && $uri[strlen($uri) - 1] !== '/') {
12    $desired_status = (int) $_GET["desired_status"];
13    http_response_code($desired_status);
14    header("Location: $uri/");
15    exit;
16}
17
18echo "method: ", $_SERVER['REQUEST_METHOD'], "; body: ", file_get_contents('php://input'), "\n";
19CODE;
20
21include __DIR__."/../../../../sapi/cli/tests/php_cli_server.inc";
22php_cli_server_start($serverCode, null, []);
23
24foreach ([null, 301, 302, 307, 308] as $status) {
25    if (is_null($status)) {
26        echo "-- Testing unredirected request --\n";
27    } else {
28        echo "-- Testing redirect status code $status --\n";
29    }
30    $suffix = $status ? "?desired_status=$status" : "";
31    echo file_get_contents("http://" . PHP_CLI_SERVER_ADDRESS . "/test$suffix", false, stream_context_create(['http' => ['method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => http_build_query(['hello' => 'world'])]]));
32    echo file_get_contents("http://" . PHP_CLI_SERVER_ADDRESS . "/test$suffix", false, stream_context_create(['http' => ['method' => 'PATCH', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => http_build_query(['hello' => 'world'])]]));
33    echo file_get_contents("http://" . PHP_CLI_SERVER_ADDRESS . "/test/$suffix", false, stream_context_create(['http' => ['method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => http_build_query(['hello' => 'world'])]]));
34    echo file_get_contents("http://" . PHP_CLI_SERVER_ADDRESS . "/test/$suffix", false, stream_context_create(['http' => ['method' => 'PATCH', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => http_build_query(['hello' => 'world'])]]));
35}
36?>
37--EXPECT--
38-- Testing unredirected request --
39method: POST; body: hello=world
40method: PATCH; body: hello=world
41method: POST; body: hello=world
42method: PATCH; body: hello=world
43-- Testing redirect status code 301 --
44method: GET; body:
45method: GET; body:
46method: GET; body:
47method: GET; body:
48-- Testing redirect status code 302 --
49method: GET; body:
50method: GET; body:
51method: GET; body:
52method: GET; body:
53-- Testing redirect status code 307 --
54method: POST; body: hello=world
55method: PATCH; body: hello=world
56method: POST; body: hello=world
57method: PATCH; body: hello=world
58-- Testing redirect status code 308 --
59method: POST; body: hello=world
60method: PATCH; body: hello=world
61method: POST; body: hello=world
62method: PATCH; body: hello=world
63