From 75455ff5f0aad2fb7bf534115e10973cd733472a Mon Sep 17 00:00:00 2001 From: Adrien Barreau Date: Fri, 20 Oct 2023 15:06:15 +0000 Subject: [PATCH] feat: handle 204 / empty bodies Signed-off-by: Adrien Barreau --- LICENSE | 2 +- src/Api.php | 5 +++- src/Exceptions/ApiException.php | 2 +- src/Exceptions/InvalidParameterException.php | 2 +- src/Exceptions/NotLoggedException.php | 2 +- tests/ApiFunctionalTest.php | 2 +- tests/ApiTest.php | 27 +++++++++++++++++++- tests/bootstrap.php | 2 +- 8 files changed, 36 insertions(+), 8 deletions(-) diff --git a/LICENSE b/LICENSE index 1e2f2e1..4ecff73 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2013-2017, OVH SAS. +Copyright (c) 2013-2023, OVH SAS. All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/src/Api.php b/src/Api.php index 644dae4..4a237ae 100644 --- a/src/Api.php +++ b/src/Api.php @@ -1,5 +1,5 @@ getStatusCode() === 204 || $response->getBody()->getSize() === 0) { + return null; + } return json_decode($response->getBody(), true, 512, JSON_THROW_ON_ERROR); } diff --git a/src/Exceptions/ApiException.php b/src/Exceptions/ApiException.php index 4c8d2e2..7696466 100644 --- a/src/Exceptions/ApiException.php +++ b/src/Exceptions/ApiException.php @@ -1,5 +1,5 @@ assertSame($test['sig'], $req->getHeaderLine('X-Ovh-Signature')); } } + + public function testEmptyResponseBody() + { + $client = new MockClient( + // GET /auth/time + new Response(200, [], MOCK_TIME), + // POST /domain/zone/nonexisting.ovh/refresh + new Response(204, [], ''), + ); + + $api = new Api(MOCK_APPLICATION_KEY, MOCK_APPLICATION_SECRET, 'ovh-eu', MOCK_CONSUMER_KEY, $client); + $response = $api->post('/domain/zone/nonexisting.ovh/refresh'); + $this->assertSame(null, $response); + + $calls = $client->calls; + $this->assertCount(2, $calls); + + $req = $calls[0]['request']; + $this->assertSame('GET', $req->getMethod()); + $this->assertSame('https://eu.api.ovh.com/1.0/auth/time', $req->getUri()->__toString()); + + $req = $calls[1]['request']; + $this->assertSame('POST', $req->getMethod()); + $this->assertSame('https://eu.api.ovh.com/1.0/domain/zone/nonexisting.ovh/refresh', $req->getUri()->__toString()); + } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index dbcd754..84fd0c3 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,5 +1,5 @@