Skip to content

Commit 8b8edb7

Browse files
Guillaumeatymic
authored andcommitted
Add query and verify option (#16)
* Add a short description to the doc comment of WebhookMessage properties * Update Guzzle options: verify and query Allow setting the verify option to a boolean or a string Add the query option to modify the GET parameters of the request * Fix StyleCI
1 parent 3859e4f commit 8b8edb7

File tree

5 files changed

+114
-4
lines changed

5 files changed

+114
-4
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,10 @@ public function routeNotificationForWebhook()
7676
### Available methods
7777

7878
- `data('')`: Accepts a JSON-encodable value for the Webhook body.
79+
- `query('')`: Accepts an associative array of query string values to add to the request.
7980
- `userAgent('')`: Accepts a string value for the Webhook user agent.
8081
- `header($name, $value)`: Sets additional headers to send with the POST Webhook.
82+
- `verify()`: Enable the SSL certificate verification or provide the path to a CA bundle
8183

8284

8385
## Changelog

src/WebhookChannel.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ public function send($notifiable, Notification $notification)
3737
$webhookData = $notification->toWebhook($notifiable)->toArray();
3838

3939
$response = $this->client->post($url, [
40+
'query' => Arr::get($webhookData, 'query'),
4041
'body' => json_encode(Arr::get($webhookData, 'data')),
41-
'verify' => false,
42+
'verify' => Arr::get($webhookData, 'verify'),
4243
'headers' => Arr::get($webhookData, 'headers'),
4344
]);
4445

src/WebhookMessage.php

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,41 @@
44

55
class WebhookMessage
66
{
7-
/** @var mixed */
7+
/**
8+
* The GET parameters of the request.
9+
*
10+
* @var array|string|null
11+
*/
12+
protected $query;
13+
14+
/**
15+
* The POST data of the Webhook request.
16+
*
17+
* @var mixed
18+
*/
819
protected $data;
920

10-
/** @var array|null */
21+
/**
22+
* The headers to send with the request.
23+
*
24+
* @var array|null
25+
*/
1126
protected $headers;
1227

13-
/** @var string|null */
28+
/**
29+
* The user agent header.
30+
*
31+
* @var string|null
32+
*/
1433
protected $userAgent;
1534

35+
/**
36+
* The Guzzle verify option.
37+
*
38+
* @var bool|string
39+
*/
40+
protected $verify = false;
41+
1642
/**
1743
* @param mixed $data
1844
*
@@ -31,6 +57,20 @@ public function __construct($data = '')
3157
$this->data = $data;
3258
}
3359

60+
/**
61+
* Set the Webhook parameters to be URL encoded.
62+
*
63+
* @param mixed $query
64+
*
65+
* @return $this
66+
*/
67+
public function query($query)
68+
{
69+
$this->query = $query;
70+
71+
return $this;
72+
}
73+
3474
/**
3575
* Set the Webhook data to be JSON encoded.
3676
*
@@ -74,14 +114,28 @@ public function userAgent($userAgent)
74114
return $this;
75115
}
76116

117+
/**
118+
* Indicate that the request should be verified.
119+
*
120+
* @return $this
121+
*/
122+
public function verify($value = true)
123+
{
124+
$this->verify = $value;
125+
126+
return $this;
127+
}
128+
77129
/**
78130
* @return array
79131
*/
80132
public function toArray()
81133
{
82134
return [
135+
'query' => $this->query,
83136
'data' => $this->data,
84137
'headers' => $this->headers,
138+
'verify' => $this->verify,
85139
];
86140
}
87141
}

tests/ChannelTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public function it_can_send_a_notification()
2121
->once()
2222
->with('https://notifiable-webhook-url.com',
2323
[
24+
'query' => null,
2425
'body' => '{"payload":{"webhook":"data"}}',
2526
'verify' => false,
2627
'headers' => [
@@ -42,6 +43,7 @@ public function it_can_send_a_notification_with_2xx_status()
4243
->once()
4344
->with('https://notifiable-webhook-url.com',
4445
[
46+
'query' => null,
4547
'body' => '{"payload":{"webhook":"data"}}',
4648
'verify' => false,
4749
'headers' => [
@@ -54,6 +56,31 @@ public function it_can_send_a_notification_with_2xx_status()
5456
$channel->send(new TestNotifiable(), new TestNotification());
5557
}
5658

59+
/** @test */
60+
public function it_can_send_a_notification_with_query_string()
61+
{
62+
$response = new Response();
63+
$client = Mockery::mock(Client::class);
64+
$client->shouldReceive('post')
65+
->once()
66+
->with('https://notifiable-webhook-url.com',
67+
[
68+
'query' => [
69+
'webhook' => 'data',
70+
],
71+
'body' => '""',
72+
'verify' => false,
73+
'headers' => [
74+
'User-Agent' => 'WebhookAgent',
75+
'X-Custom' => 'CustomHeader',
76+
],
77+
])
78+
->andReturn($response);
79+
80+
$channel = new WebhookChannel($client);
81+
$channel->send(new TestNotifiable(), new QueryTestNotification());
82+
}
83+
5784
/**
5885
* @expectedException NotificationChannels\Webhook\Exceptions\CouldNotSendNotification
5986
* @test
@@ -98,3 +125,15 @@ public function toWebhook($notifiable)
98125
->header('X-Custom', 'CustomHeader');
99126
}
100127
}
128+
129+
class QueryTestNotification extends Notification
130+
{
131+
public function toWebhook($notifiable)
132+
{
133+
return
134+
(new WebhookMessage())
135+
->query(['webhook' => 'data'])
136+
->userAgent('WebhookAgent')
137+
->header('X-Custom', 'CustomHeader');
138+
}
139+
}

tests/MessageTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ public function it_provides_a_create_method()
3333
$this->assertEquals(['foo' => 'bar'], Arr::get($message->toArray(), 'data'));
3434
}
3535

36+
/** @test */
37+
public function it_can_set_the_webhook_query()
38+
{
39+
$this->message->query(['foo' => 'bar']);
40+
$this->assertEquals(['foo' => 'bar'], Arr::get($this->message->toArray(), 'query'));
41+
}
42+
3643
/** @test */
3744
public function it_can_set_the_webhook_data()
3845
{
@@ -53,4 +60,11 @@ public function it_can_set_a_custom_header()
5360
$this->message->header('X-Custom', 'Value');
5461
$this->assertEquals(['X-Custom' => 'Value'], Arr::get($this->message->toArray(), 'headers'));
5562
}
63+
64+
/** @test */
65+
public function it_can_verify_the_request()
66+
{
67+
$this->message->verify();
68+
$this->assertEquals(true, Arr::get($this->message->toArray(), 'verify'));
69+
}
5670
}

0 commit comments

Comments
 (0)