xref: /php-src/ext/standard/tests/http/bug38802.phpt (revision f39b5c4c)
1--TEST--
2Bug #38802 (ignore_errors and max_redirects)
3--INI--
4allow_url_fopen=1
5--SKIPIF--
6<?php
7require 'server.inc'; http_server_skipif();
8?>
9--FILE--
10<?php
11require 'server.inc';
12
13function genResponses($server) {
14    $uri = 'http://' . stream_socket_get_name($server, false);
15    yield "data://text/plain,HTTP/1.1 302 Moved Temporarily\r\nLocation: $uri/foo/bar2\r\n\r\n1";
16    yield "data://text/plain,HTTP/1.1 301 Moved Permanently\r\nLocation: $uri/foo/bar3\r\n\r\n";
17    yield "data://text/plain,HTTP/1.1 302 Moved Temporarily\r\nLocation: $uri/foo/bar4\r\n\r\n3";
18    yield "data://text/plain,HTTP/1.1 200 OK\r\n\r\ndone.";
19}
20
21function do_test($context_options) {
22
23    $context = stream_context_create(array('http' => $context_options));
24
25	$uri = null;
26    ['pid' => $pid, 'uri' => $uri ] = http_server('genResponses', $output);
27
28    $fd = fopen("$uri/foo/bar", 'rb', false, $context);
29    var_dump($fd);
30
31    if ($fd) {
32        $meta_data = stream_get_meta_data($fd);
33        var_dump($meta_data['wrapper_data']);
34
35        var_dump(stream_get_contents($fd));
36    }
37
38    fseek($output, 0, SEEK_SET);
39    var_dump(stream_get_contents($output));
40
41    http_server_kill($pid);
42}
43
44echo "-- Test: follow all redirections --\n";
45
46do_test(array(), 4);
47
48echo "-- Test: fail after 2 redirections --\n";
49
50do_test(array('max_redirects' => 2), 2);
51
52echo "-- Test: fail at first redirection --\n";
53
54do_test(array('max_redirects' => 0), 1);
55
56echo "-- Test: fail at first redirection (2) --\n";
57
58do_test(array('max_redirects' => 1), 1);
59
60echo "-- Test: return at first redirection --\n";
61
62do_test(array('max_redirects' => 0, 'ignore_errors' => 1), 1);
63
64echo "-- Test: return at first redirection (2) --\n";
65
66do_test(array('max_redirects' => 1, 'ignore_errors' => 1), 1);
67
68echo "-- Test: return at second redirection --\n";
69
70do_test(array('max_redirects' => 2, 'ignore_errors' => 1), 2);
71
72?>
73--EXPECTF--
74-- Test: follow all redirections --
75resource(%d) of type (stream)
76array(7) {
77  [0]=>
78  string(30) "HTTP/1.1 302 Moved Temporarily"
79  [1]=>
80  string(%d) "Location: http://%s:%d/foo/bar2"
81  [2]=>
82  string(30) "HTTP/1.1 301 Moved Permanently"
83  [3]=>
84  string(%d) "Location: http://%s:%d/foo/bar3"
85  [4]=>
86  string(30) "HTTP/1.1 302 Moved Temporarily"
87  [5]=>
88  string(%d) "Location: http://%s:%d/foo/bar4"
89  [6]=>
90  string(15) "HTTP/1.1 200 OK"
91}
92string(5) "done."
93string(%d) "GET /foo/bar HTTP/1.1
94Host: %s:%d
95Connection: close
96
97GET /foo/bar2 HTTP/1.1
98Host: %s:%d
99Connection: close
100
101GET /foo/bar3 HTTP/1.1
102Host: %s:%d
103Connection: close
104
105GET /foo/bar4 HTTP/1.1
106Host: %s:%d
107Connection: close
108
109"
110-- Test: fail after 2 redirections --
111
112Warning: fopen(http://%s:%d/foo/bar): Failed to open stream: Redirection limit reached, aborting in %s
113bool(false)
114string(%d) "GET /foo/bar HTTP/1.1
115Host: %s:%d
116Connection: close
117
118GET /foo/bar2 HTTP/1.1
119Host: %s:%d
120Connection: close
121
122"
123-- Test: fail at first redirection --
124
125Warning: fopen(http://%s:%d/foo/bar): Failed to open stream: Redirection limit reached, aborting in %s
126bool(false)
127string(%d) "GET /foo/bar HTTP/1.1
128Host: %s:%d
129Connection: close
130
131"
132-- Test: fail at first redirection (2) --
133
134Warning: fopen(http://%s:%d/foo/bar): Failed to open stream: Redirection limit reached, aborting in %s
135bool(false)
136string(%d) "GET /foo/bar HTTP/1.1
137Host: %s:%d
138Connection: close
139
140"
141-- Test: return at first redirection --
142resource(%d) of type (stream)
143array(2) {
144  [0]=>
145  string(30) "HTTP/1.1 302 Moved Temporarily"
146  [1]=>
147  string(%d) "Location: http://%s:%d/foo/bar2"
148}
149string(1) "1"
150string(%d) "GET /foo/bar HTTP/1.1
151Host: %s:%d
152Connection: close
153
154"
155-- Test: return at first redirection (2) --
156resource(%d) of type (stream)
157array(2) {
158  [0]=>
159  string(30) "HTTP/1.1 302 Moved Temporarily"
160  [1]=>
161  string(%d) "Location: http://%s:%d/foo/bar2"
162}
163string(1) "1"
164string(%d) "GET /foo/bar HTTP/1.1
165Host: %s:%d
166Connection: close
167
168"
169-- Test: return at second redirection --
170resource(%d) of type (stream)
171array(4) {
172  [0]=>
173  string(30) "HTTP/1.1 302 Moved Temporarily"
174  [1]=>
175  string(%d) "Location: http://%s:%d/foo/bar2"
176  [2]=>
177  string(30) "HTTP/1.1 301 Moved Permanently"
178  [3]=>
179  string(%d) "Location: http://%s:%d/foo/bar3"
180}
181string(0) ""
182string(%d) "GET /foo/bar HTTP/1.1
183Host: %s:%d
184Connection: close
185
186GET /foo/bar2 HTTP/1.1
187Host: %s:%d
188Connection: close
189
190"
191