Merge branch 'R_2_1'
[m6w6/ext-http] / tests / messageparser001.phpt
1 --TEST--
2 message parser
3 --SKIPIF--
4 <?php
5 include "skipif.inc";
6 ?>
7 --FILE--
8 <?php
9
10 echo "Test\n";
11
12 use http\Message\Parser;
13
14 foreach (glob(__DIR__."/data/message_*.txt") as $file) {
15 $parser = new Parser;
16 $fd = fopen($file, "r") or die("Could not open $file");
17 while (!feof($fd)) {
18 switch ($parser->parse(fgets($fd), 0, $message)) {
19 case Parser::STATE_DONE:
20 $string = (string) $message;
21 break 2;
22 case Parser::STATE_FAILURE:
23 throw new Exception(($e = error_get_last()) ? $e["message"] : "Could not parse $file");
24 }
25 }
26
27 $parser = new Parser;
28 rewind($fd);
29 unset($message);
30
31 switch ($parser->stream($fd, 0, $message)) {
32 case Parser::STATE_DONE:
33 case Parser::STATE_START:
34 break;
35 default:
36 printf("Expected parser state 0 or 8, got %d", $parser->getState());
37 }
38 if ($string !== (string) $message) {
39 $a = explode("\n", $string);
40 $b = explode("\n", (string) $message);
41 while ((null !== ($aa = array_shift($a))) || (null !== ($bb = array_shift($b)))) {
42 if ($aa !== $bb) {
43 isset($aa) and printf("-- %s\n", $aa);
44 isset($bb) and printf("++ %s\n", $bb);
45 }
46 }
47 }
48 }
49 ?>
50 DONE
51 --EXPECT--
52 Test
53 DONE