Skip to content

Commit 1ed73fe

Browse files
committed
[Notifier] Fix Smsmode HttpClient mandatory headers
As mentioned in the documentation https://dev.smsmode.com/sms/v1/home in section Content-Type & URL Encoding. HTTP request and response bodies are JSON objects, which is why Accept and Content-Type headers are mandatory and are implicitly added when the request is executed with the TEST button. If the Accept header is missing, a Not Acceptable (406) HTTP status code is returned. (Accept: application/json) The Content-Type header is only needed with POST and PATCH methods. If it is missing, an Unsupported Media Type (415)
1 parent 49d2527 commit 1ed73fe

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

src/Symfony/Component/Notifier/Bridge/Smsmode/SmsmodeTransport.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ protected function doSend(MessageInterface $message): SentMessage
6969
}
7070

7171
$response = $this->client->request('POST', $endpoint, [
72-
'headers' => ['X-Api-Key' => $this->apiKey],
72+
'headers' => [
73+
'X-Api-Key' => $this->apiKey,
74+
'Content-type' => 'application/json',
75+
'Accept' => 'application/json',
76+
],
7377
'json' => array_filter($options),
7478
]);
7579

src/Symfony/Component/Notifier/Bridge/Smsmode/Tests/SmsmodeTransportTest.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
namespace Symfony\Component\Notifier\Bridge\Smsmode\Tests;
1313

1414
use Symfony\Component\HttpClient\MockHttpClient;
15+
use Symfony\Component\HttpFoundation\Response;
1516
use Symfony\Component\Notifier\Bridge\Smsmode\SmsmodeOptions;
1617
use Symfony\Component\Notifier\Bridge\Smsmode\SmsmodeTransport;
1718
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
1819
use Symfony\Component\Notifier\Message\ChatMessage;
19-
use Symfony\Component\Notifier\Message\MessageInterface;
2020
use Symfony\Component\Notifier\Message\SmsMessage;
2121
use Symfony\Component\Notifier\Test\TransportTestCase;
2222
use Symfony\Component\Notifier\Tests\Transport\DummyMessage;
@@ -25,6 +25,8 @@
2525

2626
final class SmsmodeTransportTest extends TransportTestCase
2727
{
28+
private const DEFAULT_URL = "https://rest.smsmode.com/sms/v1/messages";
29+
2830
public static function createTransport(HttpClientInterface $client = null, string $from = 'test_from'): SmsmodeTransport
2931
{
3032
return new SmsmodeTransport('test_api_key', $from, $client ?? new MockHttpClient());
@@ -81,6 +83,44 @@ public function testNoInvalidArgumentExceptionIsThrownIfFromIsValid(string $from
8183
self::assertSame('foo', $sentMessage->getMessageId());
8284
}
8385

86+
public function testIfMandatoryHttpClientRequestHeaderContentTypeIsMissing()
87+
{
88+
$response = $this->createMock(ResponseInterface::class);
89+
$response->expects(self::exactly(2))->method('getStatusCode')->willReturn(Response::HTTP_UNSUPPORTED_MEDIA_TYPE);
90+
91+
$options = [
92+
'headers' => [
93+
'X-Api-Key' => 'test_api_key',
94+
'Accept' => 'application/json',
95+
],
96+
];
97+
98+
$client = new MockHttpClient($response);
99+
100+
$response = $client->request('POST', self::DEFAULT_URL, $options);
101+
102+
self::assertEquals(Response::HTTP_UNSUPPORTED_MEDIA_TYPE, $response->getStatusCode());
103+
}
104+
105+
public function testIfMandatoryHttpClientRequestHeaderAcceptIsMissing()
106+
{
107+
$response = $this->createMock(ResponseInterface::class);
108+
$response->expects(self::exactly(2))->method('getStatusCode')->willReturn(Response::HTTP_NOT_ACCEPTABLE);
109+
110+
$options = [
111+
'headers' => [
112+
'X-Api-Key' => 'test_api_key',
113+
'Content-type' => 'application/json',
114+
],
115+
];
116+
117+
$client = new MockHttpClient($response);
118+
119+
$response = $client->request('POST', self::DEFAULT_URL, $options);
120+
121+
self::assertEquals(Response::HTTP_NOT_ACCEPTABLE, $response->getStatusCode());
122+
}
123+
84124
public static function toStringProvider(): iterable
85125
{
86126
yield ['smsmode://rest.smsmode.com?from=test_from', self::createTransport()];

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy