Skip to content

Commit 34a0632

Browse files
authored
[9.x] Improve test for HttpRequestClass (#44039)
* [9.x] Improve test for HttpRequestClass * [9.x] Improve test for HttpRequestClass * [9.x] Improve test for HttpRequestClass solve test fail in 8.1
1 parent de80ce6 commit 34a0632

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

tests/Http/HttpRequestTest.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,12 @@ public function testHostMethod()
292292

293293
$request = Request::create('https://example.com');
294294
$this->assertSame('example.com', $request->host());
295+
296+
$request = Request::create('https://example.com:8080');
297+
$this->assertSame('example.com', $request->host());
298+
299+
$request = Request::create('http://example.com:8080');
300+
$this->assertSame('example.com', $request->host());
295301
}
296302

297303
public function testHttpHostMethod()
@@ -336,6 +342,7 @@ public function testHasMethod()
336342
$request = Request::create('/', 'GET', ['name' => 'Taylor', 'email' => 'foo']);
337343
$this->assertTrue($request->has('name'));
338344
$this->assertTrue($request->has('name', 'email'));
345+
$this->assertTrue($request->has(['name', 'email']));
339346

340347
$request = Request::create('/', 'GET', ['foo' => ['bar', 'bar']]);
341348
$this->assertTrue($request->has('foo'));
@@ -427,6 +434,7 @@ public function testMissingMethod()
427434
$this->assertFalse($request->missing('city'));
428435
$this->assertTrue($request->missing('foo'));
429436
$this->assertTrue($request->missing('name', 'email'));
437+
$this->assertTrue($request->missing(['name', 'email']));
430438

431439
$request = Request::create('/', 'GET', ['name' => 'Taylor', 'email' => 'foo']);
432440
$this->assertFalse($request->missing('name'));
@@ -478,6 +486,7 @@ public function testFilledMethod()
478486
$request = Request::create('/', 'GET', ['name' => 'Taylor', 'email' => 'foo']);
479487
$this->assertTrue($request->filled('name'));
480488
$this->assertTrue($request->filled('name', 'email'));
489+
$this->assertTrue($request->filled(['name', 'email']));
481490

482491
// test arrays within query string
483492
$request = Request::create('/', 'GET', ['foo' => ['bar', 'baz']]);
@@ -523,10 +532,10 @@ public function testFilledAnyMethod()
523532
$this->assertTrue($request->anyFilled(['foo', 'name']));
524533
$this->assertTrue($request->anyFilled('foo', 'name'));
525534

526-
$this->assertFalse($request->anyFilled('age', 'city'));
535+
$this->assertFalse($request->anyFilled(['age', 'city']));
527536
$this->assertFalse($request->anyFilled('age', 'city'));
528537

529-
$this->assertFalse($request->anyFilled('foo', 'bar'));
538+
$this->assertFalse($request->anyFilled(['foo', 'bar']));
530539
$this->assertFalse($request->anyFilled('foo', 'bar'));
531540
}
532541

@@ -543,12 +552,14 @@ public function testInputMethod()
543552

544553
public function testBooleanMethod()
545554
{
546-
$request = Request::create('/', 'GET', ['with_trashed' => 'false', 'download' => true, 'checked' => 1, 'unchecked' => '0']);
555+
$request = Request::create('/', 'GET', ['with_trashed' => 'false', 'download' => true, 'checked' => 1, 'unchecked' => '0', 'with_on' => 'on', 'with_yes'=> 'yes']);
547556
$this->assertTrue($request->boolean('checked'));
548557
$this->assertTrue($request->boolean('download'));
549558
$this->assertFalse($request->boolean('unchecked'));
550559
$this->assertFalse($request->boolean('with_trashed'));
551560
$this->assertFalse($request->boolean('some_undefined_key'));
561+
$this->assertTrue($request->boolean('with_on'));
562+
$this->assertTrue($request->boolean('with_yes'));
552563
}
553564

554565
public function testCollectMethod()
@@ -739,12 +750,15 @@ public function testExceptMethod()
739750
$request = Request::create('/', 'GET', ['name' => 'Taylor', 'age' => 25]);
740751
$this->assertEquals(['name' => 'Taylor'], $request->except('age'));
741752
$this->assertEquals([], $request->except('age', 'name'));
753+
$this->assertEquals([], $request->except(['age', 'name']));
742754
}
743755

744756
public function testQueryMethod()
745757
{
746758
$request = Request::create('/', 'GET', ['name' => 'Taylor']);
759+
$this->assertSame(['name' => 'Taylor'], $request->query());
747760
$this->assertSame('Taylor', $request->query('name'));
761+
$this->assertSame('Taylor', $request->query('name', 'Amir'));
748762
$this->assertSame('Bob', $request->query('foo', 'Bob'));
749763
$all = $request->query(null);
750764
$this->assertSame('Taylor', $all['name']);
@@ -761,7 +775,9 @@ public function testQueryMethod()
761775
public function testPostMethod()
762776
{
763777
$request = Request::create('/', 'POST', ['name' => 'Taylor']);
778+
$this->assertSame(['name' => 'Taylor'], $request->post());
764779
$this->assertSame('Taylor', $request->post('name'));
780+
$this->assertSame('Taylor', $request->post('name', 'Amir'));
765781
$this->assertSame('Bob', $request->post('foo', 'Bob'));
766782
$all = $request->post(null);
767783
$this->assertSame('Taylor', $all['name']);
@@ -770,7 +786,9 @@ public function testPostMethod()
770786
public function testCookieMethod()
771787
{
772788
$request = Request::create('/', 'GET', [], ['name' => 'Taylor']);
789+
$this->assertSame(['name' => 'Taylor'], $request->cookie());
773790
$this->assertSame('Taylor', $request->cookie('name'));
791+
$this->assertSame('Taylor', $request->cookie('name', 'Amir'));
774792
$this->assertSame('Bob', $request->cookie('foo', 'Bob'));
775793
$all = $request->cookie(null);
776794
$this->assertSame('Taylor', $all['name']);
@@ -814,6 +832,7 @@ public function testHasFileMethod()
814832
];
815833
$request = Request::create('/', 'GET', [], [], $files);
816834
$this->assertTrue($request->hasFile('foo'));
835+
$this->assertFalse($request->hasFile('bar'));
817836
}
818837

819838
public function testServerMethod()
@@ -869,6 +888,7 @@ public function testHeaderMethod()
869888
{
870889
$request = Request::create('/', 'GET', [], [], [], ['HTTP_DO_THIS' => 'foo']);
871890
$this->assertSame('foo', $request->header('do-this'));
891+
$this->assertSame('default', $request->header('do-that', 'default'));
872892
$all = $request->header(null);
873893
$this->assertSame('foo', $all['do-this'][0]);
874894
}
@@ -883,13 +903,18 @@ public function testBearerTokenMethod()
883903

884904
$request = Request::create('/', 'GET', [], [], [], ['HTTP_AUTHORIZATION' => 'Bearer foo,bar']);
885905
$this->assertSame('foo', $request->bearerToken());
906+
907+
$request = Request::create('/', 'GET', [], [], [], ['HTTP_AUTHORIZATION' => 'foo,bar']);
908+
$this->assertNull($request->bearerToken());
886909
}
887910

888911
public function testJSONMethod()
889912
{
890913
$payload = ['name' => 'taylor'];
891914
$request = Request::create('/', 'GET', [], [], [], ['CONTENT_TYPE' => 'application/json'], json_encode($payload));
892915
$this->assertSame('taylor', $request->json('name'));
916+
$this->assertSame('taylor', $request->json('name', 'Otwell'));
917+
$this->assertSame('Moharami', $request->json('family', 'Moharami'));
893918
$this->assertSame('taylor', $request->input('name'));
894919
$data = $request->json()->all();
895920
$this->assertEquals($payload, $data);

0 commit comments

Comments
 (0)