Skip to content

Commit c59a62e

Browse files
authored
Add test related to retry key (#452)
1 parent 561efe4 commit c59a62e

File tree

1 file changed

+176
-3
lines changed

1 file changed

+176
-3
lines changed

spec/line/bot/v2/misc_spec.rb

Lines changed: 176 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@
202202
stub_request(:get, "https://api.line.me/v2/bot/message/progress/narrowcast?requestId=7d51557c-7ba0-4ed7-991f-36b2a340dc1a")
203203
.with(
204204
headers: {
205-
'Authorization'=>'Bearer YOUR_CHANNEL_ACCESS_TOKEN',
205+
'Authorization' => 'Bearer YOUR_CHANNEL_ACCESS_TOKEN',
206206
}
207207
)
208208
.to_return(status: response_code, body: response_body, headers: { 'x-line-request-id' => '3a785346-2cf3-482f-8469-c893117fcef8' })
@@ -313,7 +313,7 @@
313313
'Authorization' => "Bearer test-channel-access-token"
314314
}
315315
)
316-
.to_return(status: response_code, body: response_body, headers: { })
316+
.to_return(status: response_code, body: response_body, headers: {})
317317

318318
request = Line::Bot::V2::Liff::UpdateLiffAppRequest.new(view: { url: 'https://example.com' })
319319
body, status_code, headers = client.update_liff_app_with_http_info(liff_id: 'test-liff-id', update_liff_app_request: request)
@@ -323,9 +323,119 @@
323323
end
324324
end
325325

326+
describe 'POST /v2/bot/message/push' do
327+
let(:client) { Line::Bot::V2::MessagingApi::ApiClient.new(channel_access_token: 'test-channel-access-token') }
328+
let(:response_body) do
329+
{
330+
"sentMessages": [
331+
{
332+
"id": "461230966842064897",
333+
"quoteToken": "IStG5h1Tz7b..."
334+
}
335+
]
336+
}.to_json
337+
end
338+
let(:response_code) { 200 }
339+
340+
it 'response - success' do
341+
stub_request(:post, "https://api.line.me/v2/bot/message/push")
342+
.with(
343+
headers: {
344+
'Authorization' => "Bearer test-channel-access-token"
345+
}
346+
)
347+
.to_return(status: response_code, body: response_body, headers: { 'Content-Type' => 'application/json' })
348+
349+
body, status_code, headers = client.push_message_with_http_info(push_message_request: { type: 'text', text: 'Hello, world!' })
350+
351+
expect(status_code).to eq(200)
352+
expect(body).to be_a(Line::Bot::V2::MessagingApi::PushMessageResponse)
353+
# TODO: Add test after https://github.com/line/line-bot-sdk-ruby/issues/440 is resolved
354+
# We should access body.sent_messages[0].id and body.sent_messages[0].quote_token, but it's not possible now.
355+
expect(body.sent_messages).to eq([{ id: '461230966842064897', quote_token: 'IStG5h1Tz7b...' }])
356+
end
357+
358+
it 'request with x_line_retry_key: nil' do
359+
client = Line::Bot::V2::MessagingApi::ApiClient.new(channel_access_token: 'test-channel-access-token-retry-key-nil')
360+
retry_key = nil
361+
stub_request(:post, "https://api.line.me/v2/bot/message/push")
362+
.with(
363+
headers: {
364+
'Authorization' => "Bearer test-channel-access-token-retry-key-nil",
365+
}
366+
)
367+
.to_return(status: response_code, body: response_body, headers: { 'Content-Type' => 'application/json' })
368+
369+
client.push_message_with_http_info(push_message_request: { type: 'text', text: 'Hello, world!' }, x_line_retry_key: retry_key)
370+
371+
expect(WebMock).to(have_requested(:post, "https://api.line.me/v2/bot/message/push")
372+
.with { |req| !req.headers.key?("X-Line-Retry-Key") })
373+
end
374+
375+
it 'request with x-line-retry-key header - success' do
376+
retry_key = 'f03c3eb4-0267-4080-9e65-fffa184e1933'
377+
stub_request(:post, "https://api.line.me/v2/bot/message/push")
378+
.with(
379+
headers: {
380+
'Authorization' => "Bearer test-channel-access-token",
381+
'X-Line-Retry-Key' => retry_key
382+
}
383+
)
384+
.to_return(status: response_code, body: response_body, headers: { 'Content-Type' => 'application/json' })
385+
386+
body, status_code, headers = client.push_message_with_http_info(push_message_request: { type: 'text', text: 'Hello, world!' }, x_line_retry_key: retry_key)
387+
388+
expect(status_code).to eq(200)
389+
expect(body).to be_a(Line::Bot::V2::MessagingApi::PushMessageResponse)
390+
# TODO: Add test after https://github.com/line/line-bot-sdk-ruby/issues/440 is resolved
391+
# We should access body.sent_messages[0].id and body.sent_messages[0].quote_token, but it's not possible now.
392+
expect(body.sent_messages).to eq([{ id: '461230966842064897', quote_token: 'IStG5h1Tz7b...' }])
393+
end
394+
395+
it 'request with x-line-retry-key header - conflicted' do
396+
retry_key = '2a6e07b0-0fcf-439f-908b-828ed527e882'
397+
request_id = '3a785346-2cf3-482f-8469-c893117fcef8'
398+
accepted_request_id = '4a6e07b0-0fcf-439f-908b-828ed527e882'
399+
400+
error_response_body = {
401+
"message" => "The retry key is already accepted",
402+
"sentMessages" => [
403+
{
404+
"id" => "461230966842064897",
405+
"quoteToken" => "IStG5h1Tz7b..."
406+
}
407+
]
408+
}.to_json
409+
error_response_headers = {
410+
'Content-Type' => 'application/json',
411+
'x-line-request-id' => request_id,
412+
'x-line-accepted-request-id' => accepted_request_id
413+
}
414+
stub_request(:post, "https://api.line.me/v2/bot/message/push")
415+
.with(
416+
headers: {
417+
'Authorization' => "Bearer test-channel-access-token",
418+
'X-Line-Retry-Key' => retry_key
419+
}
420+
)
421+
.to_return(status: 409, body: error_response_body, headers: error_response_headers)
422+
423+
body, status_code, headers = client.push_message_with_http_info(push_message_request: { type: 'text', text: 'Hello, world!' }, x_line_retry_key: retry_key)
424+
425+
expect(status_code).to eq(409)
426+
expect(body).to be_a(Line::Bot::V2::MessagingApi::ErrorResponse)
427+
expect(body.message).to eq("The retry key is already accepted")
428+
# TODO: Add test after https://github.com/line/line-bot-sdk-ruby/issues/440 is resolved.
429+
# We should access body.sent_messages[0].id and body.sent_messages[0].quote_token, but it's not possible now.
430+
expect(body.sent_messages).to eq([{ id: '461230966842064897', quote_token: 'IStG5h1Tz7b...' }])
431+
expect(headers['x-line-request-id']).to eq(request_id)
432+
expect(headers['x-line-accepted-request-id']).to eq(accepted_request_id)
433+
end
434+
end
435+
326436
describe 'POST /v2/bot/message/broadcast' do
327437
let(:client) { Line::Bot::V2::MessagingApi::ApiClient.new(channel_access_token: 'test-channel-access-token') }
328-
let(:response_body) { { }.to_json } # empty json
438+
let(:response_body) { {}.to_json } # empty json
329439
let(:response_code) { 200 }
330440

331441
it 'empty json as response body should not throw error' do
@@ -342,6 +452,69 @@
342452
expect(status_code).to eq(200)
343453
expect(body).to eq("{}")
344454
end
455+
456+
it 'request with x_line_retry_key: nil' do
457+
client = Line::Bot::V2::MessagingApi::ApiClient.new(channel_access_token: 'test-channel-access-token-retry-key-nil')
458+
retry_key = nil
459+
stub_request(:post, "https://api.line.me/v2/bot/message/broadcast")
460+
.with(
461+
headers: {
462+
'Authorization' => "Bearer test-channel-access-token-retry-key-nil",
463+
}
464+
)
465+
.to_return(status: response_code, body: response_body, headers: { 'Content-Type' => 'application/json' })
466+
467+
client.broadcast_with_http_info(broadcast_request: { type: 'text', text: 'Hello, world!' }, x_line_retry_key: retry_key)
468+
469+
expect(WebMock).to(have_requested(:post, "https://api.line.me/v2/bot/message/broadcast")
470+
.with { |req| !req.headers.key?("X-Line-Retry-Key") })
471+
end
472+
473+
it 'request with x-line-retry-key header - success' do
474+
retry_key = 'f03c3eb4-0267-4080-9e65-fffa184e1933'
475+
stub_request(:post, "https://api.line.me/v2/bot/message/broadcast")
476+
.with(
477+
headers: {
478+
'Authorization' => "Bearer test-channel-access-token",
479+
'X-Line-Retry-Key' => retry_key
480+
}
481+
)
482+
.to_return(status: response_code, body: response_body, headers: { 'Content-Type' => 'application/json' })
483+
484+
body, status_code, headers = client.broadcast_with_http_info(broadcast_request: { type: 'text', text: 'Hello, world!' }, x_line_retry_key: retry_key)
485+
486+
expect(status_code).to eq(200)
487+
expect(body).to eq("{}")
488+
end
489+
490+
it 'request with x-line-retry-key header - conflicted' do
491+
retry_key = '2a6e07b0-0fcf-439f-908b-828ed527e882'
492+
request_id = '3a785346-2cf3-482f-8469-c893117fcef8'
493+
accepted_request_id = '4a6e07b0-0fcf-439f-908b-828ed527e882'
494+
495+
error_response_body = { "message" => "The retry key is already accepted" }.to_json
496+
error_response_headers = {
497+
'Content-Type' => 'application/json',
498+
'x-line-request-id' => request_id,
499+
'x-line-accepted-request-id' => accepted_request_id
500+
}
501+
stub_request(:post, "https://api.line.me/v2/bot/message/broadcast")
502+
.with(
503+
headers: {
504+
'Authorization' => "Bearer test-channel-access-token",
505+
'X-Line-Retry-Key' => retry_key
506+
}
507+
)
508+
.to_return(status: 409, body: error_response_body, headers: error_response_headers)
509+
510+
body, status_code, headers = client.broadcast_with_http_info(broadcast_request: { type: 'text', text: 'Hello, world!' }, x_line_retry_key: retry_key)
511+
512+
expect(status_code).to eq(409)
513+
expect(body).to be_a(Line::Bot::V2::MessagingApi::ErrorResponse)
514+
expect(body.message).to eq("The retry key is already accepted")
515+
expect(headers['x-line-request-id']).to eq(request_id)
516+
expect(headers['x-line-accepted-request-id']).to eq(accepted_request_id)
517+
end
345518
end
346519

347520
describe 'GET /v2/bot/message/aggregation/list' do

0 commit comments

Comments
 (0)