release v3.2.2
[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 $string = "";
16 $parser = new Parser;
17 $fd = fopen($file, "r") or die("Could not open $file");
18 while (!feof($fd)) {
19 switch ($parser->parse(fgets($fd), 0, $message)) {
20 case Parser::STATE_DONE:
21 $string = (string) $message;
22 break 2;
23 case Parser::STATE_FAILURE:
24 throw new Exception(($e = error_get_last()) ? $e["message"] : "Could not parse $file");
25 }
26 }
27
28 if (!$string) {
29 $s = array("START", "HEADER", "HEADER_DONE", "BODY", "BODY_DUMB", "BODY_LENGTH", "BODY_CHUNK", "BODY_DONE", "UPDATE_CL", "DONE");
30 printf("Unexpected state: %s (%s)\n", $s[$parser->getState()], $file);
31 }
32
33 $parser = new Parser;
34 rewind($fd);
35 unset($message);
36
37 switch ($parser->stream($fd, 0, $message)) {
38 case Parser::STATE_DONE:
39 case Parser::STATE_START:
40 break;
41 default:
42 printf("Expected parser state 0 or 8, got %d", $parser->getState());
43 }
44 if ($string !== (string) $message) {
45 $a = explode("\n", $string);
46 $b = explode("\n", (string) $message);
47 do {
48 $aa = array_shift($a);
49 $bb = array_shift($b);
50 if ($aa !== $bb) {
51 isset($aa) and printf("-- %s\n", $aa);
52 isset($bb) and printf("++ %s\n", $bb);
53 }
54 } while ($a || $b);
55 }
56 }
57 ?>
58 DONE
59 --EXPECT--
60 Test
61 DONE