|
| 1 | +<?php |
| 2 | +namespace ipinfo\ipinfolaravel\Tests; |
| 3 | + |
| 4 | +use Orchestra\Testbench\TestCase; |
| 5 | +use Illuminate\Http\Request; |
| 6 | +use Illuminate\Http\Response; |
| 7 | +use ipinfo\ipinfo\IPinfoCore as IPinfoCoreClient; |
| 8 | +use ipinfo\ipinfolaravel\iphandler\IPHandlerInterface; |
| 9 | +use ipinfo\ipinfolaravel\core\ipinfocorelaravel; |
| 10 | + |
| 11 | +class IpinfocorelaravelTest extends TestCase |
| 12 | +{ |
| 13 | + protected function getPackageProviders($app) |
| 14 | + { |
| 15 | + return [ |
| 16 | + \ipinfo\ipinfolaravel\core\ipinfocorelaravelServiceProvider::class, |
| 17 | + ]; |
| 18 | + } |
| 19 | + |
| 20 | + /** Create a middleware with injected mocks, stubbing configure() */ |
| 21 | + protected function makeMiddleware( |
| 22 | + $client, |
| 23 | + $selector, |
| 24 | + $filter = null, |
| 25 | + $noExcept = false, |
| 26 | + ) { |
| 27 | + $mw = $this->getMockBuilder(ipinfocorelaravel::class) |
| 28 | + ->onlyMethods(["configure"]) |
| 29 | + ->getMock(); |
| 30 | + $mw->method("configure")->willReturn(null); |
| 31 | + $mw->ipinfo = $client; |
| 32 | + $mw->ip_selector = $selector; |
| 33 | + $mw->filter = $filter; |
| 34 | + $mw->no_except = $noExcept; |
| 35 | + return $mw; |
| 36 | + } |
| 37 | + |
| 38 | + public function test_handle_merges_details_on_success() |
| 39 | + { |
| 40 | + $details = (object) ["country" => "US", "ip" => "8.8.8.8"]; |
| 41 | + $client = $this->createMock(IPinfoCoreClient::class); |
| 42 | + $client |
| 43 | + ->expects($this->once()) |
| 44 | + ->method("getDetails") |
| 45 | + ->with("8.8.8.8") |
| 46 | + ->willReturn($details); |
| 47 | + |
| 48 | + $selector = $this->createMock(IPHandlerInterface::class); |
| 49 | + $selector->method("getIP")->willReturn("8.8.8.8"); |
| 50 | + |
| 51 | + $mw = $this->makeMiddleware($client, $selector); |
| 52 | + |
| 53 | + $request = Request::create("/foo", "GET"); |
| 54 | + $next = function ($req) use (&$out) { |
| 55 | + $out = $req->get("ipinfo"); |
| 56 | + return new Response("OK", 200); |
| 57 | + }; |
| 58 | + |
| 59 | + $resp = $mw->handle($request, $next); |
| 60 | + |
| 61 | + $this->assertSame($details, $out); |
| 62 | + $this->assertEquals(200, $resp->getStatusCode()); |
| 63 | + $this->assertEquals("OK", $resp->getContent()); |
| 64 | + } |
| 65 | + |
| 66 | + public function test_handle_skips_lookup_when_filter_true() |
| 67 | + { |
| 68 | + $client = $this->createMock(IPinfoCoreClient::class); |
| 69 | + $client->expects($this->never())->method("getDetails"); |
| 70 | + |
| 71 | + $selector = $this->createMock(IPHandlerInterface::class); |
| 72 | + $selector->expects($this->never())->method("getIP"); |
| 73 | + |
| 74 | + $filter = fn($req) => true; |
| 75 | + $mw = $this->makeMiddleware($client, $selector, $filter); |
| 76 | + |
| 77 | + $request = Request::create("/", "GET"); |
| 78 | + $request->headers->set("user-agent", "GoogleBot"); |
| 79 | + |
| 80 | + $next = function ($req) use (&$out) { |
| 81 | + $out = $req->get("ipinfo"); |
| 82 | + return new Response(); |
| 83 | + }; |
| 84 | + |
| 85 | + $mw->handle($request, $next); |
| 86 | + $this->assertNull($out); |
| 87 | + } |
| 88 | + |
| 89 | + public function test_handle_throws_if_client_throws_and_no_except_false() |
| 90 | + { |
| 91 | + $this->expectException(\Exception::class); |
| 92 | + |
| 93 | + $client = $this->createMock(IPinfoCoreClient::class); |
| 94 | + $client |
| 95 | + ->method("getDetails") |
| 96 | + ->willThrowException(new \Exception("boom")); |
| 97 | + |
| 98 | + $selector = $this->createMock(IPHandlerInterface::class); |
| 99 | + $selector->method("getIP")->willReturn("1.1.1.1"); |
| 100 | + |
| 101 | + $mw = $this->makeMiddleware($client, $selector, null, false); |
| 102 | + $mw->handle(Request::create("/", "GET"), fn($r) => new Response()); |
| 103 | + } |
| 104 | + |
| 105 | + public function test_handle_swallows_if_client_throws_and_no_except_true() |
| 106 | + { |
| 107 | + $client = $this->createMock(IPinfoCoreClient::class); |
| 108 | + $client |
| 109 | + ->method("getDetails") |
| 110 | + ->willThrowException(new \Exception("boom")); |
| 111 | + |
| 112 | + $selector = $this->createMock(IPHandlerInterface::class); |
| 113 | + $selector->method("getIP")->willReturn("1.1.1.1"); |
| 114 | + |
| 115 | + $mw = $this->makeMiddleware($client, $selector, null, true); |
| 116 | + |
| 117 | + $next = function ($req) use (&$out) { |
| 118 | + $out = $req->get("ipinfo"); |
| 119 | + return new Response(); |
| 120 | + }; |
| 121 | + |
| 122 | + $mw->handle(Request::create("/", "GET"), $next); |
| 123 | + $this->assertNull($out); |
| 124 | + } |
| 125 | + |
| 126 | + public function test_defaultFilter_detects_bots_and_spiders() |
| 127 | + { |
| 128 | + $mw = new ipinfocorelaravel(); |
| 129 | + |
| 130 | + $r1 = Request::create("/", "GET"); |
| 131 | + $r1->headers->set("user-agent", "MySpider"); |
| 132 | + $this->assertTrue($mw->defaultFilter($r1)); |
| 133 | + |
| 134 | + $r2 = Request::create("/", "GET"); |
| 135 | + $r2->headers->set("user-agent", "someBOT/2.0"); |
| 136 | + $this->assertTrue($mw->defaultFilter($r2)); |
| 137 | + |
| 138 | + $r3 = Request::create("/", "GET"); |
| 139 | + $r3->headers->set("user-agent", "normal"); |
| 140 | + $this->assertFalse($mw->defaultFilter($r3)); |
| 141 | + |
| 142 | + $r4 = Request::create("/", "GET"); |
| 143 | + $this->assertFalse($mw->defaultFilter($r4)); |
| 144 | + } |
| 145 | +} |
0 commit comments