-
-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathshared_examples_for_responses.rb
137 lines (115 loc) · 3.91 KB
/
shared_examples_for_responses.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
shared_examples_for "a JSON 404 response" do
it "returns a 404 Not Found" do
subject
expect(last_response.status).to eq 404
end
end
shared_examples_for "a paginated response" do
let(:response_body_hash) { JSON.parse(subject.body, symbolize_names: true) }
it "includes the pagination relations" do
expect(response_body_hash[:_links]).to have_key(:next)
end
it "includes the page details" do
expect(response_body_hash).to include(
page: {
number: instance_of(Integer),
size: instance_of(Integer),
totalElements: instance_of(Integer),
totalPages: instance_of(Integer),
}
)
end
end
shared_examples_for "a page" do
it "includes the page details" do
expect(response_body_hash).to include(
page: {
number: instance_of(Integer),
size: instance_of(Integer),
totalElements: instance_of(Integer),
totalPages: instance_of(Integer),
}
)
end
end
shared_examples_for "an invalid pagination params response" do
let(:response_body_hash) { JSON.parse(subject.body, symbolize_names: true) }
it "returns a a 400 status code" do
expect(subject.status).to eq 400
end
it "includes the parameter validation errors" do
expect(response_body_hash[:errors].has_key?(:page)).to be_truthy
expect(response_body_hash[:errors].has_key?(:size)).to be_truthy
end
end
require "rspec/expectations"
RSpec::Matchers.define :be_a_hal_json_success_response do
match do | actual |
expect(actual.status).to be 200
expect(actual.headers["Content-Type"]).to eq "application/hal+json;charset=utf-8"
end
failure_message do
"Expected successful json response, got #{actual.status} #{actual.headers['Content-Type']} with body #{actual.body}"
end
end
RSpec::Matchers.define :be_a_hal_json_created_response do
match do | actual |
expect(actual.status).to be 201
expect(actual.headers["Content-Type"]).to eq "application/hal+json;charset=utf-8"
end
failure_message do
"Expected creation successful json response, got #{actual.status} #{actual.headers['Content-Type']} with body #{actual.body}"
end
end
RSpec::Matchers.define :be_a_json_response do
match do | actual |
expect(actual.headers["Content-Type"]).to eq "application/hal+json;charset=utf-8"
end
end
RSpec::Matchers.define :be_a_json_error_response do | message |
match do | actual |
expect(actual.status).to be 400
expect(actual.headers["Content-Type"]).to eq "application/hal+json;charset=utf-8"
expect(actual.body).to include message
end
end
RSpec::Matchers.define :be_a_404_response do
match do | actual |
expect(actual.status).to be 404
end
end
RSpec::Matchers.define :include_hashes_matching do |expected_array_of_hashes|
match do |array_of_hashes|
expected_array_of_hashes.each do | expected |
expect(array_of_hashes).to include_hash_matching(expected)
end
expect(array_of_hashes.size).to eq expected_array_of_hashes.size
end
def slice actual, keys
keys.each_with_object({}) { |k, hash| hash[k] = actual[k] if actual.has_key?(k) }
end
end
RSpec::Matchers.define :include_hash_matching do |expected|
match do |array_of_hashes|
@array_of_hashes = array_of_hashes
array_of_hashes.any? { |actual| slice(actual, expected.keys) == expected }
end
failure_message do
"expected #{@array_of_hashes.inspect} to include #{expected.inspect}"
end
def slice actual, keys
keys.each_with_object({}) do |k, hash|
if (actual.respond_to?(:has_key?) && actual.has_key?(k))
hash[k] = actual[k]
elsif actual.respond_to?(k)
hash[k] = actual.send(k)
end
end
end
end
RSpec::Matchers.define :be_a_pact_never_verified_for_consumer do | expected_consumer_name |
match do | actual_reason |
expect(actual_reason).to be_a(PactBroker::Matrix::PactNotEverVerifiedByProvider)
expect(actual_reason.consumer_selector.pacticipant_name).to eq expected_consumer_name
end
end