Skip to content

Commit

Permalink
Merge pull request #213 from line/test-webhook-endpoint
Browse files Browse the repository at this point in the history
support test webhook endpoint
  • Loading branch information
ryota-sakamoto authored Jan 12, 2021
2 parents ec0d151 + b61dc54 commit ee9c0e2
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/line/bot/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,23 @@ def set_webhook_endpoint(webhook_endpoint)
put(endpoint, endpoint_path, body.to_json, credentials)
end

# Checks if the configured webhook endpoint can receive a test webhook event.
#
# @param webhook_endpoint [String] options
#
# @return [Net::HTTPResponse]
def test_webhook_endpoint(webhook_endpoint = nil)
channel_token_required

endpoint_path = '/bot/channel/webhook/test'
body = if webhook_endpoint.nil?
{}
else
{endpoint: webhook_endpoint}
end
post(endpoint, endpoint_path, body.to_json, credentials)
end

def get_liff_apps
channel_token_required

Expand Down
53 changes: 53 additions & 0 deletions spec/line/bot/webhook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,23 @@
}
EOS

TEST_WEBHOOK_ENDPOINT = 'https://example.com/test'
TEST_WEBHOOK_ENDPOINT_CONTENT = <<"EOS"
{
"endpoint": "#{TEST_WEBHOOK_ENDPOINT}"
}
EOS

TEST_WEBHOOK_ENDPOINT_RESPONSE = <<"EOS"
{
"success": true,
"timestamp": "2020-09-30T05:38:20.031Z",
"statusCode": 200,
"reason": "OK",
"detail": "200"
}
EOS

describe Line::Bot::Client do
let(:client) do
dummy_config = {
Expand Down Expand Up @@ -47,4 +64,40 @@
expect(WebMock).to have_requested(:put, Line::Bot::API::DEFAULT_ENDPOINT + '/bot/channel/webhook/endpoint')
.with(body: JSON.parse(UPDATE_WEBHOOK_ENDPOINT_CONTENT).to_json)
end

it 'test webhook endpoint' do
uri_template = Addressable::Template.new Line::Bot::API::DEFAULT_ENDPOINT + '/bot/channel/webhook/test'
stub_request(:post, uri_template).to_return(body: TEST_WEBHOOK_ENDPOINT_RESPONSE, status: 200)

response = client.test_webhook_endpoint(TEST_WEBHOOK_ENDPOINT)
expect(WebMock).to have_requested(:post, Line::Bot::API::DEFAULT_ENDPOINT + '/bot/channel/webhook/test')
.with(body: JSON.parse(TEST_WEBHOOK_ENDPOINT_CONTENT).to_json)

json = JSON.parse(response.body, symbolize_names: true)
expect(json).to eq(
success: true,
timestamp: "2020-09-30T05:38:20.031Z",
statusCode: 200,
reason: "OK",
detail: "200"
)
end

it 'test webhook endpoint empty' do
uri_template = Addressable::Template.new Line::Bot::API::DEFAULT_ENDPOINT + '/bot/channel/webhook/test'
stub_request(:post, uri_template).to_return(body: TEST_WEBHOOK_ENDPOINT_RESPONSE, status: 200)

response = client.test_webhook_endpoint
expect(WebMock).to have_requested(:post, Line::Bot::API::DEFAULT_ENDPOINT + '/bot/channel/webhook/test')
.with(body: JSON.parse('{}').to_json)

json = JSON.parse(response.body, symbolize_names: true)
expect(json).to eq(
success: true,
timestamp: "2020-09-30T05:38:20.031Z",
statusCode: 200,
reason: "OK",
detail: "200"
)
end
end

0 comments on commit ee9c0e2

Please sign in to comment.