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