Skip to content

Commit ee9c0e2

Browse files
Merge pull request #213 from line/test-webhook-endpoint
support test webhook endpoint
2 parents ec0d151 + b61dc54 commit ee9c0e2

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

lib/line/bot/client.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,23 @@ def set_webhook_endpoint(webhook_endpoint)
668668
put(endpoint, endpoint_path, body.to_json, credentials)
669669
end
670670

671+
# Checks if the configured webhook endpoint can receive a test webhook event.
672+
#
673+
# @param webhook_endpoint [String] options
674+
#
675+
# @return [Net::HTTPResponse]
676+
def test_webhook_endpoint(webhook_endpoint = nil)
677+
channel_token_required
678+
679+
endpoint_path = '/bot/channel/webhook/test'
680+
body = if webhook_endpoint.nil?
681+
{}
682+
else
683+
{endpoint: webhook_endpoint}
684+
end
685+
post(endpoint, endpoint_path, body.to_json, credentials)
686+
end
687+
671688
def get_liff_apps
672689
channel_token_required
673690

spec/line/bot/webhook_spec.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,23 @@
1616
}
1717
EOS
1818

19+
TEST_WEBHOOK_ENDPOINT = 'https://example.com/test'
20+
TEST_WEBHOOK_ENDPOINT_CONTENT = <<"EOS"
21+
{
22+
"endpoint": "#{TEST_WEBHOOK_ENDPOINT}"
23+
}
24+
EOS
25+
26+
TEST_WEBHOOK_ENDPOINT_RESPONSE = <<"EOS"
27+
{
28+
"success": true,
29+
"timestamp": "2020-09-30T05:38:20.031Z",
30+
"statusCode": 200,
31+
"reason": "OK",
32+
"detail": "200"
33+
}
34+
EOS
35+
1936
describe Line::Bot::Client do
2037
let(:client) do
2138
dummy_config = {
@@ -47,4 +64,40 @@
4764
expect(WebMock).to have_requested(:put, Line::Bot::API::DEFAULT_ENDPOINT + '/bot/channel/webhook/endpoint')
4865
.with(body: JSON.parse(UPDATE_WEBHOOK_ENDPOINT_CONTENT).to_json)
4966
end
67+
68+
it 'test webhook endpoint' do
69+
uri_template = Addressable::Template.new Line::Bot::API::DEFAULT_ENDPOINT + '/bot/channel/webhook/test'
70+
stub_request(:post, uri_template).to_return(body: TEST_WEBHOOK_ENDPOINT_RESPONSE, status: 200)
71+
72+
response = client.test_webhook_endpoint(TEST_WEBHOOK_ENDPOINT)
73+
expect(WebMock).to have_requested(:post, Line::Bot::API::DEFAULT_ENDPOINT + '/bot/channel/webhook/test')
74+
.with(body: JSON.parse(TEST_WEBHOOK_ENDPOINT_CONTENT).to_json)
75+
76+
json = JSON.parse(response.body, symbolize_names: true)
77+
expect(json).to eq(
78+
success: true,
79+
timestamp: "2020-09-30T05:38:20.031Z",
80+
statusCode: 200,
81+
reason: "OK",
82+
detail: "200"
83+
)
84+
end
85+
86+
it 'test webhook endpoint empty' do
87+
uri_template = Addressable::Template.new Line::Bot::API::DEFAULT_ENDPOINT + '/bot/channel/webhook/test'
88+
stub_request(:post, uri_template).to_return(body: TEST_WEBHOOK_ENDPOINT_RESPONSE, status: 200)
89+
90+
response = client.test_webhook_endpoint
91+
expect(WebMock).to have_requested(:post, Line::Bot::API::DEFAULT_ENDPOINT + '/bot/channel/webhook/test')
92+
.with(body: JSON.parse('{}').to_json)
93+
94+
json = JSON.parse(response.body, symbolize_names: true)
95+
expect(json).to eq(
96+
success: true,
97+
timestamp: "2020-09-30T05:38:20.031Z",
98+
statusCode: 200,
99+
reason: "OK",
100+
detail: "200"
101+
)
102+
end
50103
end

0 commit comments

Comments
 (0)