Skip to content

Commit

Permalink
feat: handle 204 / empty bodies
Browse files Browse the repository at this point in the history
Signed-off-by: Adrien Barreau <[email protected]>
  • Loading branch information
deathiop committed Oct 20, 2023
1 parent d2c2161 commit 75455ff
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 8 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 4 additions & 1 deletion src/Api.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
# 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
Expand Down Expand Up @@ -329,6 +329,9 @@ protected function rawCall($method, $path, $content = null, $is_authenticated =
*/
private function decodeResponse(Response $response)
{
if ($response->getStatusCode() === 204 || $response->getBody()->getSize() === 0) {
return null;
}
return json_decode($response->getBody(), true, 512, JSON_THROW_ON_ERROR);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/ApiException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
# 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
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/InvalidParameterException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
# 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
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/NotLoggedException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
# 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
Expand Down
2 changes: 1 addition & 1 deletion tests/ApiFunctionalTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
# 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
Expand Down
27 changes: 26 additions & 1 deletion tests/ApiTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
# 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
Expand Down Expand Up @@ -462,4 +462,29 @@ public function testVersionInUrl()
$this->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());
}
}
2 changes: 1 addition & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
# 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
Expand Down

0 comments on commit 75455ff

Please sign in to comment.