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