From 7ab2dc61f00d6692acf1c6d41b32818f0811b414 Mon Sep 17 00:00:00 2001 From: Pedro Cunha Date: Thu, 3 Oct 2024 00:00:10 +0100 Subject: [PATCH] Properly hook up query parameters --- src/SolderClient.php | 2 +- tests/ClientTest.php | 37 ++++++++++++++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/SolderClient.php b/src/SolderClient.php index 7ba539d..1956d4e 100644 --- a/src/SolderClient.php +++ b/src/SolderClient.php @@ -63,7 +63,7 @@ private function request($url, $query = [], $useKey = true) } try { - $response = $this->client->get($url); + $response = $this->client->get($url, ['query' => $query]); } catch (TransferException | GuzzleException $e) { throw new ConnectionException('Request to \'' . $url . '\' failed. ' . $e->getMessage(), 0, $e); } diff --git a/tests/ClientTest.php b/tests/ClientTest.php index e3e0369..cca2c14 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -2,6 +2,7 @@ namespace TechnicPack\SolderClient\Tests; +use GuzzleHttp\Middleware; use TechnicPack\SolderClient\Exception\BadJSONException; use TechnicPack\SolderClient\Exception\ConnectionException; use TechnicPack\SolderClient\Exception\InvalidURLException; @@ -242,7 +243,23 @@ public function testGetBuildUnauthorized() public function testGetBuild() { - $body = '{"minecraft":"1.5.2","forge":null,"java":null,"memory":null,"mods":[{"name":"armorbar","version":"v0.7.1","md5":"f323a8d582302ea0abd615a223f8a68b","url":"https://mirror.technicpack.net/Technic/mods/armorbar/armorbar-v0.7.1.zip"}]}'; + $body = '{ + "minecraft":"1.5.2", + "forge":null, + "java":null, + "memory":null, + "mods":[{ + "id": 30, + "name":"armorbar", + "version":"v0.7.1", + "md5":"f323a8d582302ea0abd615a223f8a68b", + "url":"https://mirror.technicpack.net/Technic/mods/armorbar/armorbar-v0.7.1.zip", + "filesize": 25000, + "pretty_name": "Armor Bar", + "author": "Test", + "description": "Test description", + "link": "https://example.com/" + }]}'; // Create a mock and queue two responses. $mock = new MockHandler([ @@ -250,9 +267,12 @@ public function testGetBuild() new Response(200, ['Content-Length' => 0], $body), ]); - $handler = HandlerStack::create($mock); + $handlerStack = HandlerStack::create($mock); - $client = SolderClient::factory('http://localhost/api/', 'C3gy35Um2pBE97xn90z0sUNhH1KbzI99', [], $handler); + $historyContainer = []; + $handlerStack->push(Middleware::history($historyContainer)); + + $client = SolderClient::factory('http://localhost/api/', 'C3gy35Um2pBE97xn90z0sUNhH1KbzI99', [], $handlerStack); $build = $client->getBuild('hexxit', '1.0.1'); @@ -271,10 +291,21 @@ public function testGetBuild() $this->assertCount(1, $build->mods); $mod = $build->mods[0]; + $this->assertEquals(30, $mod->id); $this->assertEquals('armorbar', $mod->name); $this->assertEquals('v0.7.1', $mod->version); $this->assertEquals('f323a8d582302ea0abd615a223f8a68b', $mod->md5); $this->assertEquals('https://mirror.technicpack.net/Technic/mods/armorbar/armorbar-v0.7.1.zip', $mod->url); + $this->assertEquals(25000, $mod->filesize); + $this->assertEquals('Armor Bar', $mod->pretty_name); + $this->assertEquals('Test', $mod->author); + $this->assertEquals('Test description', $mod->description); + $this->assertEquals('https://example.com/', $mod->link); + + // Test if the query parameters are corrects + $lastRequest = end($historyContainer); + $expectedQuery = http_build_query(['include' => 'mods', 'k' => 'C3gy35Um2pBE97xn90z0sUNhH1KbzI99'], null, '&', PHP_QUERY_RFC3986); + $this->assertEquals($expectedQuery, $lastRequest['request']->getUri()->getQuery()); } public function testBadPack()