Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ protected function doSend(MessageInterface $message): SentMessage

if (null !== $this->user && null !== $this->password) {
$headers['Authorization'] = 'Basic '.rtrim(base64_encode($this->user.':'.$this->password), '=');
} elseif (null !== $this->password) {
$headers['Authorization'] = 'Bearer '.$this->password;
}

$response = $this->client->request('POST', ($this->secureHttp ? 'https' : 'http').'://'.$this->getEndpoint(), [
Expand Down Expand Up @@ -115,8 +117,8 @@ protected function doSend(MessageInterface $message): SentMessage

public function supports(MessageInterface $message): bool
{
return $message instanceof PushMessage &&
(null === $message->getOptions() || $message->getOptions() instanceof NtfyOptions);
return $message instanceof PushMessage
&& (null === $message->getOptions() || $message->getOptions() instanceof NtfyOptions);
}

public function __toString(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ public function create(Dsn $dsn): TransportInterface
$transport->setPort($port);
}

if (!empty($user = $dsn->getUser()) && !empty($password = $dsn->getPassword())) {
if (!empty($user = $dsn->getUser())) {
$transport->setUser($user);
}

if (!empty($password = $dsn->getPassword())) {
$transport->setPassword($password);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public static function createProvider(): iterable
'ntfy://ntfy.sh/test',
'ntfy://user:password@default/test',
];
yield [
'ntfy://ntfy.sh/test',
'ntfy://:password@default/test',
];
yield [
'ntfy://ntfy.sh:8888/test',
'ntfy://user:password@default:8888/test?secureHttp=off',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,32 @@ public function testSend()
$this->assertSame('2BYIwRmvBKcv', $sentMessage->getMessageId());
}

public function testSendWithPassword()
{
$response = $this->createMock(ResponseInterface::class);
$response->expects($this->exactly(2))
->method('getStatusCode')
->willReturn(200);
$response->expects($this->once())
->method('getContent')
->willReturn(json_encode(['id' => '2BYIwRmvBKcv', 'event' => 'message']));

$client = new MockHttpClient(function (string $method, string $url, array $options = []) use ($response): ResponseInterface {
$expectedBody = json_encode(['topic' => 'test', 'title' => 'Hello', 'message' => 'World']);
$expectedAuthorization = 'Authorization: Bearer testtokentesttoken';
$this->assertJsonStringEqualsJsonString($expectedBody, $options['body']);
$this->assertTrue(\in_array($expectedAuthorization, $options['headers'], true));

return $response;
});

$transport = $this->createTransport($client)->setPassword('testtokentesttoken');

$sentMessage = $transport->send(new PushMessage('Hello', 'World'));

$this->assertSame('2BYIwRmvBKcv', $sentMessage->getMessageId());
}

public function testSendWithUserAndPassword()
{
$response = $this->createMock(ResponseInterface::class);
Expand Down