Skip to content

Commit efe9eec

Browse files
authored
Add test for cases value=null or key is undefined (#443)
There are cases where the JSON key itself does not exist, and cases where the key exists but the value is null. This change adds tests for these scenarios.
1 parent 4e56058 commit efe9eec

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

spec/line/bot/v2/misc_spec.rb

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,95 @@
131131
end
132132
end
133133

134+
describe 'GET /v2/bot/insight/followers' do
135+
let(:client) { Line::Bot::V2::Insight::ApiClient.new(channel_access_token: channel_access_token) }
136+
let(:response_code) { 200 }
137+
138+
it 'returns the number of followers successfully (ready status)' do
139+
response_body = {
140+
"status" => "ready",
141+
"followers" => 1000,
142+
"targetedReaches" => 900,
143+
"blocks" => 100
144+
}.to_json
145+
146+
stub_request(:get, "https://api.line.me/v2/bot/insight/followers")
147+
.with(
148+
headers: {
149+
'Authorization' => "Bearer #{channel_access_token}"
150+
}
151+
)
152+
.to_return(status: response_code, body: response_body, headers: { 'Content-Type' => 'application/json' })
153+
154+
body, status_code, = client.get_number_of_followers_with_http_info
155+
156+
expect(status_code).to eq(200)
157+
expect(body.status).to eq('ready')
158+
expect(body.followers).to eq(1000)
159+
expect(body.targeted_reaches).to eq(900)
160+
expect(body.blocks).to eq(100)
161+
end
162+
163+
it 'handles the null fields properly (unready status)' do
164+
response_body = {
165+
"status" => "unready",
166+
"followers" => nil,
167+
"targetedReaches" => nil,
168+
"blocks" => nil
169+
}.to_json
170+
171+
stub_request(:get, "https://api.line.me/v2/bot/insight/followers")
172+
.with(
173+
headers: {
174+
'Authorization' => "Bearer #{channel_access_token}"
175+
}
176+
)
177+
.to_return(status: response_code, body: response_body, headers: { 'Content-Type' => 'application/json' })
178+
179+
body, status_code, = client.get_number_of_followers_with_http_info
180+
181+
expect(status_code).to eq(200)
182+
expect(body.status).to eq('unready')
183+
expect(body.followers).to be_nil
184+
expect(body.targeted_reaches).to be_nil
185+
expect(body.blocks).to be_nil
186+
end
187+
end
188+
189+
describe 'GET /v2/bot/message/progress/narrowcast' do
190+
let(:client) { Line::Bot::V2::MessagingApi::ApiClient.new(channel_access_token: channel_access_token) }
191+
let(:response_body) do
192+
# missing errorCode, completedTime, targetCount
193+
{
194+
"phase" => "waiting",
195+
"acceptedTime" => "2020-12-03T10:15:30.121Z"
196+
}
197+
.to_json
198+
end
199+
let(:response_code) { 200 }
200+
201+
it 'no problem even when response does not contain some JSON keys' do
202+
stub_request(:get, "https://api.line.me/v2/bot/message/progress/narrowcast?requestId=7d51557c-7ba0-4ed7-991f-36b2a340dc1a")
203+
.with(
204+
headers: {
205+
'Authorization'=>'Bearer YOUR_CHANNEL_ACCESS_TOKEN',
206+
}
207+
)
208+
.to_return(status: response_code, body: response_body, headers: { 'x-line-request-id' => '3a785346-2cf3-482f-8469-c893117fcef8' })
209+
210+
body, status_code, headers = client.get_narrowcast_progress_with_http_info(
211+
request_id: '7d51557c-7ba0-4ed7-991f-36b2a340dc1a'
212+
)
213+
214+
expect(status_code).to eq(200)
215+
expect(body.phase).to eq('waiting')
216+
expect(body.accepted_time).to eq('2020-12-03T10:15:30.121Z')
217+
expect(body.error_code).to be_nil
218+
expect(body.completed_time).to be_nil
219+
expect(body.target_count).to be_nil
220+
end
221+
end
222+
134223
describe 'POST /v2/oauth/accessToken' do
135224
let(:client) { Line::Bot::V2::ChannelAccessToken::ApiClient.new }
136225
let(:grant_type) { 'client_credentials' }

0 commit comments

Comments
 (0)