Skip to content

Commit 5f35f88

Browse files
committed
test: sync two test cases
But not exactly the same.
1 parent ea7c719 commit 5f35f88

File tree

2 files changed

+132
-6
lines changed

2 files changed

+132
-6
lines changed

tests/system/HTTP/CURLRequestDoNotShareOptionsTest.php

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ protected function setUp(): void
3535
{
3636
parent::setUp();
3737

38-
Services::reset();
38+
$this->resetServices();
3939
$this->request = $this->getRequest();
4040
}
4141

@@ -552,6 +552,21 @@ public function testSSLVerification(): void
552552
$this->assertSame(2, $options[CURLOPT_SSL_VERIFYHOST]);
553553
}
554554

555+
public function testNoSSL(): void
556+
{
557+
$this->request->request('get', 'http://example.com', [
558+
'verify' => false,
559+
]);
560+
561+
$options = $this->request->curl_options;
562+
563+
$this->assertArrayHasKey(CURLOPT_SSL_VERIFYPEER, $options);
564+
$this->assertFalse($options[CURLOPT_SSL_VERIFYPEER]);
565+
566+
$this->assertArrayHasKey(CURLOPT_SSL_VERIFYHOST, $options);
567+
$this->assertSame(0, $options[CURLOPT_SSL_VERIFYHOST]);
568+
}
569+
555570
public function testSSLWithBadKey(): void
556571
{
557572
$file = 'something_obviously_bogus';
@@ -799,6 +814,21 @@ public function testSendContinuedWithManyHeaders(): void
799814
$this->assertSame(200, $response->getStatusCode());
800815
}
801816

817+
public function testSendProxied(): void
818+
{
819+
$request = $this->getRequest([
820+
'base_uri' => 'http://www.foo.com/api/v1/',
821+
'delay' => 100,
822+
]);
823+
824+
$output = "HTTP/1.1 200 Connection established
825+
Proxy-Agent: Fortinet-Proxy/1.0\x0d\x0a\x0d\x0aHTTP/1.1 200 OK\x0d\x0a\x0d\x0aHi there";
826+
$request->setOutput($output);
827+
828+
$response = $request->get('answer');
829+
$this->assertSame('Hi there', $response->getBody());
830+
}
831+
802832
/**
803833
* See: https://github.com/codeigniter4/CodeIgniter4/issues/7394
804834
*/
@@ -1082,7 +1112,6 @@ public function testSetJSON(): void
10821112
$expected,
10831113
$this->request->curl_options[CURLOPT_POSTFIELDS]
10841114
);
1085-
10861115
$this->assertSame(
10871116
'Content-Type: application/json',
10881117
$this->request->curl_options[CURLOPT_HTTPHEADER][0]
@@ -1113,6 +1142,18 @@ public function testHTTPv11(): void
11131142
$this->assertSame(CURL_HTTP_VERSION_1_1, $options[CURLOPT_HTTP_VERSION]);
11141143
}
11151144

1145+
public function testHTTPv2(): void
1146+
{
1147+
$this->request->request('POST', '/post', [
1148+
'version' => 2.0,
1149+
]);
1150+
1151+
$options = $this->request->curl_options;
1152+
1153+
$this->assertArrayHasKey(CURLOPT_HTTP_VERSION, $options);
1154+
$this->assertSame(CURL_HTTP_VERSION_2_0, $options[CURLOPT_HTTP_VERSION]);
1155+
}
1156+
11161157
public function testCookieOption(): void
11171158
{
11181159
$holder = SUPPORTPATH . 'HTTP/Files/CookiesHolder.txt';
@@ -1142,6 +1183,32 @@ public function testUserAgentOption(): void
11421183
$this->assertSame($agent, $options[CURLOPT_USERAGENT]);
11431184
}
11441185

1186+
/**
1187+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/8347
1188+
*/
1189+
public function testMultipleHTTP100(): void
1190+
{
1191+
$jsonBody = '{"name":"John Doe","age":30}';
1192+
1193+
$output = "HTTP/1.1 100 Continue
1194+
Mark bundle as not supporting multiuse
1195+
HTTP/1.1 100 Continue
1196+
Mark bundle as not supporting multiuse
1197+
HTTP/1.1 200 OK
1198+
Server: Werkzeug/2.2.2 Python/3.7.17
1199+
Date: Sun, 28 Jan 2024 06:05:36 GMT
1200+
Content-Type: application/json
1201+
Content-Length: 33\r\n\r\n" . $jsonBody;
1202+
1203+
$this->request->setOutput($output);
1204+
1205+
$response = $this->request->request('GET', 'http://example.com');
1206+
1207+
$this->assertSame($jsonBody, $response->getBody());
1208+
1209+
$this->assertSame(200, $response->getStatusCode());
1210+
}
1211+
11451212
public function testGetHeaderLineContentType(): void
11461213
{
11471214
$output = 'HTTP/2 200

tests/system/HTTP/CURLRequestTest.php

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
*/
3030
final class CURLRequestTest extends CIUnitTestCase
3131
{
32-
private CURLRequest $request;
32+
private MockCURLRequest $request;
3333

3434
protected function setUp(): void
3535
{
@@ -39,7 +39,7 @@ protected function setUp(): void
3939
$this->request = $this->getRequest();
4040
}
4141

42-
protected function getRequest(array $options = [])
42+
protected function getRequest(array $options = []): MockCURLRequest
4343
{
4444
$uri = isset($options['base_uri']) ? new URI($options['base_uri']) : new URI();
4545
$app = new App();
@@ -205,7 +205,7 @@ public function testOptionsHeadersNotUsingPopulate(): void
205205
$this->assertSame('', $request->header('Accept-Encoding')->getValue());
206206
}
207207

208-
public function testOptionsAreSharedBetweenRequests(): void
208+
public function testDefaultOptionsAreSharedBetweenRequests(): void
209209
{
210210
$options = [
211211
'form_params' => ['studio' => 1],
@@ -868,6 +868,38 @@ public function testResponseHeadersWithMultipleRequests(): void
868868
$this->assertSame(200, $response->getStatusCode());
869869
}
870870

871+
public function testResponseHeadersWithMultipleSetCookies(): void
872+
{
873+
$request = $this->getRequest([
874+
'base_uri' => 'https://github.com/',
875+
]);
876+
877+
$output = "HTTP/2 200
878+
server: GitHub.com
879+
date: Sat, 11 Nov 2023 02:26:55 GMT
880+
content-type: text/html; charset=utf-8
881+
set-cookie: _gh_sess=PlRlha1YumlLhLuo5MuNbIWJRO9RRuR%2FHfYsWRh5B0mkalFIZstlAbTmSstl8q%2FAC57IsWMVuFHWQc6L4qDHQJrwhuYVO5ZaigPCUjAStnhh%2FieZQVqIf92Al7vusuzx2o8XH%2Fv6nd9qzMTAWc2%2FkRsl8jxPQYGNaWeuUBY2w3%2FDORSikN4c0vHOyedhU7Xcv3Ryz5xD3DNxK9R8xKNZ6OSXLJ6bjX8iIT6LxvroVIf2HjvowW9cQsq0kN08mS6KtTnH0mD3ANWqsVVWeMzFNA%3D%3D--Jx830Q9Nmkfz9OGA--kEcPtNphvjNMopYqFDxUbw%3D%3D; Path=/; HttpOnly; Secure; SameSite=Lax
882+
set-cookie: _octo=GH1.1.599292127.1699669625; Path=/; Domain=github.com; Expires=Mon, 11 Nov 2024 02:27:05 GMT; Secure; SameSite=Lax
883+
set-cookie: logged_in=no; Path=/; Domain=github.com; Expires=Mon, 11 Nov 2024 02:27:05 GMT; HttpOnly; Secure; SameSite=Lax
884+
accept-ranges: bytes\x0d\x0a\x0d\x0a";
885+
$request->setOutput($output);
886+
887+
$response = $request->get('/');
888+
889+
$setCookieHeaders = $response->header('set-cookie');
890+
891+
$this->assertCount(3, $setCookieHeaders);
892+
$this->assertSame(
893+
'logged_in=no; Path=/; Domain=github.com; Expires=Mon, 11 Nov 2024 02:27:05 GMT; HttpOnly; Secure; SameSite=Lax',
894+
$setCookieHeaders[2]->getValue()
895+
);
896+
897+
$this->assertSame(
898+
'_octo=GH1.1.599292127.1699669625; Path=/; Domain=github.com; Expires=Mon, 11 Nov 2024 02:27:05 GMT; Secure; SameSite=Lax',
899+
$setCookieHeaders[1]->getValueLine()
900+
);
901+
}
902+
871903
public function testSplitResponse(): void
872904
{
873905
$request = $this->getRequest([
@@ -1026,6 +1058,10 @@ public function testJSONData(): void
10261058
$this->assertSame('POST', $this->request->getMethod());
10271059

10281060
$expected = json_encode($params);
1061+
$this->assertSame(
1062+
$expected,
1063+
$this->request->curl_options[CURLOPT_POSTFIELDS]
1064+
);
10291065
$this->assertSame($expected, $this->request->getBody());
10301066
}
10311067

@@ -1040,7 +1076,13 @@ public function testSetJSON(): void
10401076
];
10411077
$this->request->setJSON($params)->post('/post');
10421078

1043-
$this->assertSame(json_encode($params), $this->request->getBody());
1079+
$expected = json_encode($params);
1080+
$this->assertSame(
1081+
$expected,
1082+
$this->request->curl_options[CURLOPT_POSTFIELDS]
1083+
);
1084+
$this->assertSame($expected, $this->request->getBody());
1085+
10441086
$this->assertSame(
10451087
'Content-Type: application/json',
10461088
$this->request->curl_options[CURLOPT_HTTPHEADER][0]
@@ -1137,4 +1179,21 @@ public function testMultipleHTTP100(): void
11371179

11381180
$this->assertSame(200, $response->getStatusCode());
11391181
}
1182+
1183+
public function testGetHeaderLineContentType(): void
1184+
{
1185+
$output = 'HTTP/2 200
1186+
date: Thu, 11 Apr 2024 07:26:00 GMT
1187+
content-type: text/html; charset=UTF-8
1188+
cache-control: no-store, max-age=0, no-cache
1189+
server: cloudflare
1190+
content-encoding: br
1191+
alt-svc: h3=":443"; ma=86400' . "\x0d\x0a\x0d\x0aResponse Body";
1192+
1193+
$this->request->setOutput($output);
1194+
1195+
$response = $this->request->request('get', 'http://example.com');
1196+
1197+
$this->assertSame('text/html; charset=UTF-8', $response->getHeaderLine('Content-Type'));
1198+
}
11401199
}

0 commit comments

Comments
 (0)