Skip to content
Merged
Changes from 1 commit
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
177 changes: 161 additions & 16 deletions spec/line/bot/v2/misc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -340,16 +340,34 @@
end
let(:response_code) { 200 }

it 'response - success' do
it 'response - success - using Line::Bot::V2::MessagingApi::PushMessageRequest' do
stub_request(:post, "https://api.line.me/v2/bot/message/push")
.with(
headers: {
'Authorization' => "Bearer test-channel-access-token"
}
},
body: {
"to" => "USER_ID",
"messages" => [
{
"type" => "text",
"text" => " Hello, world! "
}
],
"notificationDisabled" => false,
}.to_json
)
.to_return(status: response_code, body: response_body, headers: { 'Content-Type' => 'application/json' })

body, status_code, headers = client.push_message_with_http_info(push_message_request: { type: 'text', text: 'Hello, world!' })
request = Line::Bot::V2::MessagingApi::PushMessageRequest.new(
to: 'USER_ID',
messages: [
Line::Bot::V2::MessagingApi::TextMessage.new(
text: ' Hello, world! '
)
]
)
body, status_code, headers = client.push_message_with_http_info(push_message_request: request)

expect(status_code).to eq(200)
expect(body).to be_a(Line::Bot::V2::MessagingApi::PushMessageResponse)
Expand All @@ -358,18 +376,67 @@
expect(body.sent_messages).to eq([{ id: '461230966842064897', quote_token: 'IStG5h1Tz7b...' }])
end

it 'response - success - using hash' do
stub_request(:post, "https://api.line.me/v2/bot/message/push")
.with(
headers: {
'Authorization' => "Bearer test-channel-access-token"
},
body: {
"to" => "USER_ID",
"messages" => [
{
"type" => "text",
"text" => " Hello, world! "
}
],
}.to_json
)
.to_return(status: response_code, body: response_body, headers: { 'Content-Type' => 'application/json' })

request = {
"to" => "USER_ID",
"messages" => [{ type: 'text', text: ' Hello, world! ' }]
}
Comment on lines +397 to +400
Copy link
Contributor Author

@Yang-33 Yang-33 Apr 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this (without using Line::Bot::V2::MessagingApi::PushMessageRequest) is acceptable as of now.

Q: I'm not sure if it's intentional. Is this intentional? @mokuzon

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not my intention to do so, but I do get a warning from RBS.
Since we do not strictly implement type checking, I think this is an unavoidable behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the reply. Since it wasn't intentional, let's remove the current behavior from the spec (i.e., don't explicitly mention it in the documentation, but leave the code as is). On the other hand, I thought that v1 users might appreciate this behavior when migrating to v2, as it makes the migration process much simpler. There's no need to actively rewrite the request object.

If, in the future, this behavior becomes problematic for any SDK users, we can discontinue the current behavior.

body, status_code, headers = client.push_message_with_http_info(push_message_request: request)

expect(status_code).to eq(200)
expect(body).to be_a(Line::Bot::V2::MessagingApi::PushMessageResponse)
end

it 'request with x_line_retry_key: nil' do
client = Line::Bot::V2::MessagingApi::ApiClient.new(channel_access_token: 'test-channel-access-token-retry-key-nil')
retry_key = nil
stub_request(:post, "https://api.line.me/v2/bot/message/push")
.with(
headers: {
'Authorization' => "Bearer test-channel-access-token-retry-key-nil",
}
},
body: {
"to" => "USER_ID",
"messages" => [
{
"type" => "text",
"text" => "Hello, world!"
}
],
"notificationDisabled" => false,
}.to_json
)
.to_return(status: response_code, body: response_body, headers: { 'Content-Type' => 'application/json' })

client.push_message_with_http_info(push_message_request: { type: 'text', text: 'Hello, world!' }, x_line_retry_key: retry_key)
request = Line::Bot::V2::MessagingApi::PushMessageRequest.new(
to: 'USER_ID',
messages: [
Line::Bot::V2::MessagingApi::TextMessage.new(
text: 'Hello, world!'
)
]
)
client.push_message_with_http_info(
push_message_request: request,
x_line_retry_key: retry_key
)

expect(WebMock).to(have_requested(:post, "https://api.line.me/v2/bot/message/push")
.with { |req| !req.headers.key?("X-Line-Retry-Key") })
Expand All @@ -382,12 +449,32 @@
headers: {
'Authorization' => "Bearer test-channel-access-token",
'X-Line-Retry-Key' => retry_key
}
},
body: {
"to" => "USER_ID",
"messages" => [
{
"type" => "text",
"text" => "Hello, world!"
}
],
"notificationDisabled" => false,
}.to_json
)
.to_return(status: response_code, body: response_body, headers: { 'Content-Type' => 'application/json' })

body, status_code, headers = client.push_message_with_http_info(push_message_request: { type: 'text', text: 'Hello, world!' }, x_line_retry_key: retry_key)

request = Line::Bot::V2::MessagingApi::PushMessageRequest.new(
to: 'USER_ID',
messages: [
Line::Bot::V2::MessagingApi::TextMessage.new(
text: 'Hello, world!'
)
]
)
body, status_code, headers = client.push_message_with_http_info(
push_message_request: request,
x_line_retry_key: retry_key
)
expect(status_code).to eq(200)
expect(body).to be_a(Line::Bot::V2::MessagingApi::PushMessageResponse)
# TODO: Add test after https://github.com/line/line-bot-sdk-ruby/issues/440 is resolved
Expand Down Expand Up @@ -419,12 +506,32 @@
headers: {
'Authorization' => "Bearer test-channel-access-token",
'X-Line-Retry-Key' => retry_key
}
},
body: {
"to" => "USER_ID",
"messages" => [
{
"type" => "text",
"text" => "Hello, world!"
}
],
"notificationDisabled" => false,
}.to_json
)
.to_return(status: 409, body: error_response_body, headers: error_response_headers)

body, status_code, headers = client.push_message_with_http_info(push_message_request: { type: 'text', text: 'Hello, world!' }, x_line_retry_key: retry_key)

request = Line::Bot::V2::MessagingApi::PushMessageRequest.new(
to: 'USER_ID',
messages: [
Line::Bot::V2::MessagingApi::TextMessage.new(
text: 'Hello, world!'
)
]
)
body, status_code, headers = client.push_message_with_http_info(
push_message_request: request,
x_line_retry_key: retry_key
)
expect(status_code).to eq(409)
expect(body).to be_a(Line::Bot::V2::MessagingApi::ErrorResponse)
expect(body.message).to eq("The retry key is already accepted")
Expand All @@ -446,11 +553,28 @@
.with(
headers: {
'Authorization' => "Bearer test-channel-access-token"
}
},
body: {
"messages" => [
{
"type" => "text",
"text" => "Hello, world!"
}
],
"notificationDisabled" => false,
}.to_json
)
.to_return(status: response_code, body: response_body, headers: { 'Content-Type' => 'application/json' })

body, status_code, headers = client.broadcast_with_http_info(broadcast_request: { type: 'text', text: 'Hello, world!' })
request = Line::Bot::V2::MessagingApi::BroadcastRequest.new(
messages: [
Line::Bot::V2::MessagingApi::TextMessage.new(
text: 'Hello, world!'
)
]
)

body, status_code, headers = client.broadcast_with_http_info(broadcast_request: request)

expect(status_code).to eq(200)
expect(body).to eq("{}")
Expand All @@ -467,7 +591,14 @@
)
.to_return(status: response_code, body: response_body, headers: { 'Content-Type' => 'application/json' })

client.broadcast_with_http_info(broadcast_request: { type: 'text', text: 'Hello, world!' }, x_line_retry_key: retry_key)
request = Line::Bot::V2::MessagingApi::BroadcastRequest.new(
messages: [
Line::Bot::V2::MessagingApi::TextMessage.new(
text: 'Hello, world!'
)
]
)
client.broadcast_with_http_info(broadcast_request: request, x_line_retry_key: retry_key)

expect(WebMock).to(have_requested(:post, "https://api.line.me/v2/bot/message/broadcast")
.with { |req| !req.headers.key?("X-Line-Retry-Key") })
Expand All @@ -484,7 +615,14 @@
)
.to_return(status: response_code, body: response_body, headers: { 'Content-Type' => 'application/json' })

body, status_code, headers = client.broadcast_with_http_info(broadcast_request: { type: 'text', text: 'Hello, world!' }, x_line_retry_key: retry_key)
request = Line::Bot::V2::MessagingApi::BroadcastRequest.new(
messages: [
Line::Bot::V2::MessagingApi::TextMessage.new(
text: 'Hello, world!'
)
]
)
body, status_code, headers = client.broadcast_with_http_info(broadcast_request: request, x_line_retry_key: retry_key)

expect(status_code).to eq(200)
expect(body).to eq("{}")
Expand All @@ -510,7 +648,14 @@
)
.to_return(status: 409, body: error_response_body, headers: error_response_headers)

body, status_code, headers = client.broadcast_with_http_info(broadcast_request: { type: 'text', text: 'Hello, world!' }, x_line_retry_key: retry_key)
request = Line::Bot::V2::MessagingApi::BroadcastRequest.new(
messages: [
Line::Bot::V2::MessagingApi::TextMessage.new(
text: 'Hello, world!'
)
]
)
body, status_code, headers = client.broadcast_with_http_info(broadcast_request: request, x_line_retry_key: retry_key)

expect(status_code).to eq(409)
expect(body).to be_a(Line::Bot::V2::MessagingApi::ErrorResponse)
Expand Down