prepare v4.2.5
[m6w6/ext-http] / tests / client021.phpt
1 --TEST--
2 client cookies
3 --SKIPIF--
4 <?php
5 include "skipif.inc";
6 skip_client_test();
7 if (0 === strpos(http\Client\Curl\Versions\CURL, "7.64.0")) {
8 die("skip - cookie handling broken or crashes with libcurl-7.64\n");
9 }
10 ?>
11 --FILE--
12 <?php
13
14 include "helper/server.inc";
15
16 echo "Test\n";
17
18 function dump() {
19 global $tmpfile, $section;
20 printf("# %s\n", $section);
21 foreach (file($tmpfile) as $line) {
22 if ($line[0] === "#" || $line === "\n") {
23 continue;
24 }
25 printf("%s:\t%s", $tmpfile, $line);
26 }
27 }
28
29 function send_and_check($client, $cmp) {
30 global $section, $request;
31 $client->requeue($request)->send();
32 foreach ($client->getResponse()->getCookies() as $list) {
33 foreach ($list->getCookies() as $name => $value) {
34 if ($cmp[$name] != $value) {
35 printf("# %s\nExpected %s=%s, got %s\n",
36 $section, $name, $cmp[$name], $value);
37 }
38 }
39 }
40 #dump();
41 }
42
43 $tmpfile = tempnam(sys_get_temp_dir(), "cookie.");
44 $request = new http\Client\Request("GET", "http://localhost");
45
46 $section = "distinct clients";
47
48 server("cookie.inc", function($port) use($request, $tmpfile) {
49 $request->setOptions(array("port" => $port));
50 $client = new http\Client;
51 send_and_check($client, ["counter" => 1]);
52 });
53 server("cookie.inc", function($port) use($request, $tmpfile) {
54 $request->setOptions(array("port" => $port));
55 $client = new http\Client;
56 send_and_check($client, ["counter" => 1]);
57 });
58 server("cookie.inc", function($port) use($request, $tmpfile) {
59 $request->setOptions(array("port" => $port));
60 $client = new http\Client;
61 send_and_check($client, ["counter" => 1]);
62 });
63
64 $section = "reusing curl handles";
65
66 server("cookie.inc", function($port) use($request, $tmpfile) {
67 $request->setOptions(array("port" => $port));
68 $client = new http\Client("curl", "test");
69 send_and_check($client, ["counter" => 1]);
70 });
71 server("cookie.inc", function($port) use($request, $tmpfile) {
72 $request->setOptions(array("port" => $port));
73 $client = new http\Client("curl", "test");
74 send_and_check($client, ["counter" => 2]);
75 });
76 server("cookie.inc", function($port) use($request, $tmpfile) {
77 $request->setOptions(array("port" => $port));
78 $client = new http\Client("curl", "test");
79 send_and_check($client, ["counter" => 3]);
80 });
81
82 $section = "distict client with persistent cookies";
83
84 $request->setOptions(array("cookiestore" => $tmpfile));
85
86 server("cookie.inc", function($port) use($request, $tmpfile) {
87 $request->setOptions(array("port" => $port));
88 $client = new http\Client;
89 send_and_check($client, ["counter" => 1]);
90 send_and_check($client, ["counter" => 2]);
91 send_and_check($client, ["counter" => 3]);
92 });
93 server("cookie.inc", function($port) use($request, $tmpfile) {
94 $request->setOptions(array("port" => $port));
95 $client = new http\Client;
96 send_and_check($client, ["counter" => 4]);
97 send_and_check($client, ["counter" => 5]);
98 send_and_check($client, ["counter" => 6]);
99 });
100
101 $section = "distinct client with persistent cookies, but session cookies removed";
102
103 server("cookie.inc", function($port) use($request, $tmpfile) {
104 $request->setOptions(array("port" => $port, "cookiesession" => true));
105 $client = new http\Client;
106 send_and_check($client, ["counter" => 1]);
107 send_and_check($client, ["counter" => 1]);
108 send_and_check($client, ["counter" => 1]);
109 });
110
111 $section = "distinct client with persistent cookies, and session cookies kept";
112
113 server("cookie.inc", function($port) use($request, $tmpfile) {
114 $request->setOptions(array("port" => $port, "cookiesession" => false));
115 $client = new http\Client;
116 send_and_check($client, ["counter" => 2]);
117 send_and_check($client, ["counter" => 3]);
118 send_and_check($client, ["counter" => 4]);
119 });
120
121 $section = "reusing curl handles without persistent cookies and disabling cookie_share";
122
123 $c = new http\Client("curl", "test");
124 $c->configure(array("share_cookies" => false));
125 $c = null;
126 $request->setOptions(array("cookiestore" => null));
127
128 server("cookie.inc", function($port) use($request, $tmpfile) {
129 $request->setOptions(array("port" => $port));
130 $client = new http\Client("curl", "test");
131 send_and_check($client, ["counter" => 1]);
132 });
133 server("cookie.inc", function($port) use($request, $tmpfile) {
134 $request->setOptions(array("port" => $port));
135 $client = new http\Client("curl", "test");
136 send_and_check($client, ["counter" => 1]);
137 });
138 server("cookie.inc", function($port) use($request, $tmpfile) {
139 $request->setOptions(array("port" => $port));
140 $client = new http\Client("curl", "test");
141 send_and_check($client, ["counter" => 1]);
142 });
143
144
145 unlink($tmpfile);
146
147 ?>
148 ===DONE===
149 --EXPECT--
150 Test
151 ===DONE===