-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathresponse_spec.rb
90 lines (64 loc) · 2.34 KB
/
response_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
require 'spec_helper'
describe MediawikiApi::Response do
let(:response) { MediawikiApi::Response.new(faraday_response, envelope) }
let(:faraday_response) { double('Faraday::Response', body: body) }
let(:body) { '{}' }
let(:response_object) { JSON.parse(body) }
let(:envelope) { [] }
describe '#data' do
subject { response.data }
context 'with a JSON object response body' do
let(:body) { '{ "query": { "result": "success" } }' }
context 'and no expected envelope' do
let(:envelope) { [] }
it { is_expected.to eq(response_object) }
end
context 'and a single-level envelope' do
let(:envelope) { ['query'] }
let(:nested_object) { response_object['query'] }
it { is_expected.to eq(nested_object) }
end
context 'and a multi-level envelope' do
let(:envelope) { %w[query result] }
let(:nested_object) { response_object['query']['result'] }
it { is_expected.to eq(nested_object) }
end
context "and a multi-level envelope that doesn't completely match" do
let(:envelope) { %w[query something] }
let(:partially_nested_object) { response_object['query'] }
it { is_expected.to eq(partially_nested_object) }
end
end
context 'with a JSON array response body' do
let(:body) { '[ "something" ]' }
context 'with any expected envelope' do
let(:envelope) { %w[what ever] }
it { is_expected.to eq(response_object) }
end
end
end
describe '#warnings' do
subject { response.warnings }
context 'where the response contains no warnings' do
let(:body) { '{ "query": { "result": "success" } }' }
it { is_expected.to be_empty }
end
context 'where the response contains warnings' do
let(:body) { '{ "warnings": { "main": { "*": "sorta bad message" } } }' }
it { is_expected.to_not be_empty }
it { is_expected.to include('sorta bad message') }
end
end
describe '#warnings?' do
subject { response.warnings? }
before { allow(response).to receive(:warnings) { warnings } }
context 'where there are warnings' do
let(:warnings) { ['warning'] }
it { is_expected.to be(true) }
end
context 'where there are no warnings' do
let(:warnings) { [] }
it { is_expected.to be(false) }
end
end
end