Skip to content

Commit 3552515

Browse files
authored
Add test about base_url provided in client (#456)
`base_url` option provided by all client is useful for using a mock server, changing base url due to some reasons, or testing locally(though we have webmock... in this project). This change adds tests for it.
1 parent 88957ae commit 3552515

1 file changed

Lines changed: 83 additions & 0 deletions

File tree

spec/line/bot/v2/misc_spec.rb

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'spec_helper'
2+
require 'base64'
23

34
describe 'misc' do
45
let(:channel_access_token) { 'YOUR_CHANNEL_ACCESS_TOKEN' }
@@ -672,6 +673,88 @@
672673
end
673674
end
674675

676+
describe 'Client.new - base_url' do
677+
describe 'normal client' do
678+
let(:client) { Line::Bot::V2::MessagingApi::ApiClient.new(channel_access_token: 'test-channel-access-token', base_url: 'https://example.com') }
679+
let(:user_id) { 'u1234567890' }
680+
let(:response_body) { { displayName: "LINE taro", userId: user_id, language: "en", pictureUrl: "https://profile.line-scdn.net/ch/v2/p/uf9da5ee2b...", statusMessage: "Hello, LINE!" }.to_json }
681+
let(:response_code) { 200 }
682+
683+
it 'allow to change base_url in normal client' do
684+
path = "https://example.com/v2/bot/profile/#{user_id}"
685+
stub_request(:get, path)
686+
.with(
687+
headers: {
688+
'Authorization' => "Bearer test-channel-access-token"
689+
}
690+
)
691+
.to_return(status: response_code, body: response_body, headers: { 'Content-Type' => 'application/json' })
692+
693+
body, status_code, headers = client.get_profile_with_http_info(user_id: user_id)
694+
695+
expect(status_code).to eq(response_code)
696+
expect(body.user_id).to eq(user_id)
697+
end
698+
end
699+
700+
describe 'blob client' do
701+
let(:client) { Line::Bot::V2::MessagingApi::ApiBlobClient.new(channel_access_token: 'test-channel-access-token', base_url: 'https://example.com') }
702+
let(:response_body) { { status: "succeeded" }.to_json }
703+
let(:response_code) { 200 }
704+
705+
it 'allow to change base_url in blob client' do
706+
path = "https://example.com/v2/bot/message/test-message-id/content/transcoding"
707+
stub_request(:get, path)
708+
.with(
709+
headers: {
710+
'Authorization' => "Bearer test-channel-access-token"
711+
}
712+
)
713+
.to_return(status: response_code, body: response_body, headers: { 'Content-Type' => 'application/json' })
714+
715+
body, status_code, headers = client.get_message_content_transcoding_by_message_id_with_http_info(message_id: 'test-message-id')
716+
expect(status_code).to eq(200)
717+
expect(body.status).to eq('succeeded')
718+
end
719+
end
720+
721+
describe 'module attach client' do
722+
let(:client) { Line::Bot::V2::ModuleAttach::ApiClient.new(base_url: 'https://example.com', channel_id: '100000', channel_secret: 'test-channel-secret') }
723+
let(:response_body) { { bot_id: "U111...", scopes: ["message:send", "message:receive"] }.to_json }
724+
let(:response_code) { 200 }
725+
726+
it 'allow to change base_url in module attach client' do
727+
path = "https://example.com/module/auth/v1/token"
728+
expected_authorization_header_value = Base64.strict_encode64("100000:test-channel-secret")
729+
730+
stub_request(:post, path)
731+
.with(
732+
headers: {
733+
'Authorization' => "Basic #{expected_authorization_header_value}",
734+
'Content-Type' => 'application/x-www-form-urlencoded'
735+
},
736+
body: {
737+
'grant_type' => 'authorization_code',
738+
'code' => 'test-code',
739+
'redirect_uri' => 'https://example2.com/callback?key=value',
740+
'scope' => 'message:send message:receive'
741+
}
742+
)
743+
.to_return(status: response_code, body: response_body, headers: { 'Content-Type' => 'application/json' })
744+
745+
body, status_code, headers = client.attach_module_with_http_info(
746+
grant_type: 'authorization_code',
747+
code: 'test-code',
748+
redirect_uri: 'https://example2.com/callback?key=value',
749+
scope: 'message:send message:receive'
750+
)
751+
expect(status_code).to eq(200)
752+
expect(body.bot_id).to eq('U111...')
753+
expect(body.scopes).to eq(["message:send", "message:receive"])
754+
end
755+
end
756+
end
757+
675758
describe 'Line::Bot::V2::MessagingApi::TemplateMessage#initialize' do
676759
it "contains fixed type attribute" do
677760
template_message = Line::Bot::V2::MessagingApi::TemplateMessage.new(

0 commit comments

Comments
 (0)