http_stub 0.22.4 → 0.23.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/http_stub.rb +15 -5
- data/lib/http_stub/configurer/dsl/request_attribute_referencer.rb +19 -0
- data/lib/http_stub/configurer/dsl/request_referencer.rb +24 -0
- data/lib/http_stub/configurer/dsl/scenario_activator.rb +2 -2
- data/lib/http_stub/configurer/dsl/scenario_builder.rb +2 -2
- data/lib/http_stub/configurer/dsl/server.rb +4 -4
- data/lib/http_stub/configurer/dsl/stub_builder.rb +5 -3
- data/lib/http_stub/configurer/request/controllable_value.rb +1 -3
- data/lib/http_stub/configurer/request/http/factory.rb +2 -2
- data/lib/http_stub/configurer/request/omittable.rb +2 -4
- data/lib/http_stub/configurer/request/regexpable.rb +3 -8
- data/lib/http_stub/configurer/server/command_processor.rb +5 -7
- data/lib/http_stub/configurer/server/facade.rb +2 -4
- data/lib/http_stub/extensions/core/hash.rb +4 -11
- data/lib/http_stub/extensions/core/hash/formatted.rb +23 -0
- data/lib/http_stub/extensions/core/hash/indifferent_and_insensitive_access.rb +24 -0
- data/lib/http_stub/extensions/core/hash/with_indifferent_and_insensitive_access.rb +18 -0
- data/lib/http_stub/extensions/rack/handler.rb +1 -1
- data/lib/http_stub/rake/task_generators.rb +1 -1
- data/lib/http_stub/server/application.rb +10 -11
- data/lib/http_stub/server/application_helpers.rb +3 -5
- data/lib/http_stub/server/daemon.rb +5 -6
- data/lib/http_stub/server/request.rb +3 -10
- data/lib/http_stub/server/request/headers.rb +25 -0
- data/lib/http_stub/server/request/parameters.rb +21 -0
- data/lib/http_stub/server/request/request.rb +21 -0
- data/lib/http_stub/server/scenario/links.rb +1 -1
- data/lib/http_stub/server/stub/controller.rb +1 -1
- data/lib/http_stub/server/stub/empty.rb +4 -0
- data/lib/http_stub/server/stub/match/hash_matcher.rb +19 -0
- data/lib/http_stub/server/stub/match/omitted_value_matcher.rb +1 -3
- data/lib/http_stub/server/stub/match/rule/body.rb +2 -4
- data/lib/http_stub/server/stub/match/rule/headers.rb +5 -11
- data/lib/http_stub/server/stub/match/rule/json_body.rb +3 -5
- data/lib/http_stub/server/stub/match/rule/method.rb +1 -1
- data/lib/http_stub/server/stub/match/rule/parameters.rb +4 -7
- data/lib/http_stub/server/stub/match/rule/simple_body.rb +3 -3
- data/lib/http_stub/server/stub/match/rule/truthy.rb +27 -0
- data/lib/http_stub/server/stub/match/rule/uri.rb +3 -3
- data/lib/http_stub/server/stub/match/string_value_matcher.rb +4 -13
- data/lib/http_stub/server/stub/response.rb +5 -2
- data/lib/http_stub/server/stub/response/attribute/body.rb +32 -0
- data/lib/http_stub/server/stub/response/attribute/headers.rb +26 -0
- data/lib/http_stub/server/stub/response/attribute/interpolator.rb +26 -0
- data/lib/http_stub/server/stub/response/attribute/interpolator/headers.rb +27 -0
- data/lib/http_stub/server/stub/response/attribute/interpolator/parameters.rb +27 -0
- data/lib/http_stub/server/stub/response/base.rb +5 -16
- data/lib/http_stub/server/stub/response/file.rb +6 -2
- data/lib/http_stub/server/stub/response/text.rb +12 -0
- data/lib/http_stub/server/stub/stub.rb +4 -0
- data/lib/http_stub/version.rb +1 -1
- data/spec/acceptance/request_reference_spec.rb +44 -0
- data/spec/lib/http_stub/configurer/dsl/request_attribute_referencer_spec.rb +37 -0
- data/spec/lib/http_stub/configurer/dsl/request_referencer_spec.rb +37 -0
- data/spec/lib/http_stub/configurer/dsl/scenario_activator_spec.rb +14 -14
- data/spec/lib/http_stub/configurer/dsl/server_spec.rb +2 -6
- data/spec/lib/http_stub/configurer/dsl/stub_builder_spec.rb +34 -8
- data/spec/lib/http_stub/{server/formatted_hash_spec.rb → extensions/core/hash/formatted_spec.rb} +4 -6
- data/spec/lib/http_stub/{hash_with_indifferent_and_insensitive_access_spec.rb → extensions/core/hash/indifferent_and_insensitive_access_spec.rb} +8 -4
- data/spec/lib/http_stub/extensions/core/hash/with_indifferent_and_insensitive_access_spec.rb +41 -0
- data/spec/lib/http_stub/extensions/core/hash_spec.rb +11 -129
- data/spec/lib/http_stub/server/application_spec.rb +1 -1
- data/spec/lib/http_stub/server/payload_file_consolidator_spec.rb +1 -1
- data/spec/lib/http_stub/server/request/headers_spec.rb +89 -0
- data/spec/lib/http_stub/server/request/parameters_spec.rb +60 -0
- data/spec/lib/http_stub/server/request/request_spec.rb +84 -0
- data/spec/lib/http_stub/server/request_spec.rb +9 -98
- data/spec/lib/http_stub/server/response_spec.rb +2 -6
- data/spec/lib/http_stub/server/scenario/controller_spec.rb +1 -1
- data/spec/lib/http_stub/server/scenario/parser_spec.rb +3 -1
- data/spec/lib/http_stub/server/stub/controller_spec.rb +11 -4
- data/spec/lib/http_stub/server/stub/empty_spec.rb +10 -0
- data/spec/lib/http_stub/server/stub/match/hash_matcher_spec.rb +122 -0
- data/spec/lib/http_stub/server/stub/match/rule/body_spec.rb +4 -4
- data/spec/lib/http_stub/server/stub/match/rule/headers_spec.rb +54 -45
- data/spec/lib/http_stub/server/stub/match/rule/json_body_spec.rb +5 -5
- data/spec/lib/http_stub/server/stub/match/rule/method_spec.rb +19 -17
- data/spec/lib/http_stub/server/stub/match/rule/parameters_spec.rb +26 -33
- data/spec/lib/http_stub/server/stub/match/rule/simple_body_spec.rb +16 -25
- data/spec/lib/http_stub/server/stub/match/{truthy_matcher_spec.rb → rule/truthy_spec.rb} +3 -5
- data/spec/lib/http_stub/server/stub/match/rule/uri_spec.rb +16 -25
- data/spec/lib/http_stub/server/stub/match/string_value_matcher_spec.rb +2 -12
- data/spec/lib/http_stub/server/stub/parser_spec.rb +5 -3
- data/spec/lib/http_stub/server/stub/response/attribute/body_spec.rb +98 -0
- data/spec/lib/http_stub/server/stub/response/attribute/headers_spec.rb +57 -0
- data/spec/lib/http_stub/server/stub/response/attribute/interpolator/headers_spec.rb +53 -0
- data/spec/lib/http_stub/server/stub/response/attribute/interpolator/parameters_spec.rb +53 -0
- data/spec/lib/http_stub/server/stub/response/attribute/interpolator_spec.rb +41 -0
- data/spec/lib/http_stub/server/stub/response/base_spec.rb +6 -53
- data/spec/lib/http_stub/server/stub/response/file_spec.rb +48 -7
- data/spec/lib/http_stub/server/stub/response/text_spec.rb +66 -1
- data/spec/lib/http_stub/server/stub/stub_spec.rb +25 -4
- data/spec/spec_helper.rb +1 -0
- data/spec/support/http_stub/server/request_fixture.rb +1 -1
- metadata +86 -26
- data/lib/http_stub/hash_with_indifferent_and_insensitive_access.rb +0 -18
- data/lib/http_stub/server/formatted_hash.rb +0 -18
- data/lib/http_stub/server/header_parser.rb +0 -17
- data/lib/http_stub/server/stub/match/hash_with_string_value_matchers.rb +0 -26
- data/lib/http_stub/server/stub/match/truthy_matcher.rb +0 -25
- data/spec/lib/http_stub/server/header_parser_spec.rb +0 -51
- data/spec/lib/http_stub/server/stub/match/hash_with_string_value_matchers_spec.rb +0 -166
@@ -1,111 +1,22 @@
|
|
1
1
|
describe HttpStub::Server::Request do
|
2
2
|
|
3
|
-
let(:
|
4
|
-
let(:rack_request_method) { "some method" }
|
5
|
-
let(:rack_parameters) { { "parameter_key" => "parameter value" } }
|
6
|
-
let(:rack_body) { "some request body" }
|
7
|
-
let(:rack_request) do
|
8
|
-
instance_double(Rack::Request, path_info: rack_path_info,
|
9
|
-
request_method: rack_request_method,
|
10
|
-
params: rack_parameters,
|
11
|
-
body: StringIO.new(rack_body))
|
12
|
-
end
|
13
|
-
|
14
|
-
let(:server_request) { described_class.new(rack_request) }
|
15
|
-
|
16
|
-
before(:example) do
|
17
|
-
allow(HttpStub::Server::HeaderParser).to receive(:parse).and_return({})
|
18
|
-
allow(HttpStub::Server::FormattedHash).to receive(:new)
|
19
|
-
end
|
20
|
-
|
21
|
-
describe "#uri" do
|
22
|
-
|
23
|
-
it "is the rack request path information" do
|
24
|
-
expect(server_request.uri).to eql(rack_path_info)
|
25
|
-
end
|
26
|
-
|
27
|
-
end
|
28
|
-
|
29
|
-
describe "#method" do
|
30
|
-
|
31
|
-
subject { server_request.method }
|
32
|
-
|
33
|
-
it "is the rack request method" do
|
34
|
-
expect(subject).to eql(rack_request_method)
|
35
|
-
end
|
36
|
-
|
37
|
-
end
|
38
|
-
|
39
|
-
describe "#headers" do
|
40
|
-
|
41
|
-
let(:parsed_headers) { { "parsed_header_key" => "parsed header value" } }
|
42
|
-
let(:formatted_hash) { instance_double(HttpStub::Server::FormattedHash) }
|
43
|
-
|
44
|
-
subject { server_request.headers }
|
45
|
-
|
46
|
-
before(:example) do
|
47
|
-
allow(HttpStub::Server::HeaderParser).to receive(:parse).and_return(parsed_headers)
|
48
|
-
allow(HttpStub::Server::FormattedHash).to receive(:new).with(parsed_headers, anything).and_return(formatted_hash)
|
49
|
-
end
|
50
|
-
|
51
|
-
it "parses the headers from the rack request via the header parser" do
|
52
|
-
expect(HttpStub::Server::HeaderParser).to receive(:parse).with(rack_request)
|
53
|
-
|
54
|
-
subject
|
55
|
-
end
|
3
|
+
let(:rack_request) { instance_double(Rack::Request) }
|
56
4
|
|
57
|
-
|
58
|
-
expect(HttpStub::Server::FormattedHash).to receive(:new).with(parsed_headers, anything)
|
5
|
+
describe "::create" do
|
59
6
|
|
60
|
-
|
61
|
-
end
|
62
|
-
|
63
|
-
it "creates a formatted hash formatted by a ':' request value delimiter" do
|
64
|
-
expect(HttpStub::Server::FormattedHash).to receive(:new).with(anything, ":")
|
65
|
-
|
66
|
-
subject
|
67
|
-
end
|
68
|
-
|
69
|
-
it "is the formattted hash" do
|
70
|
-
expect(subject).to eql(formatted_hash)
|
71
|
-
end
|
72
|
-
|
73
|
-
end
|
74
|
-
|
75
|
-
describe "#parameters" do
|
76
|
-
|
77
|
-
let(:formatted_hash) { instance_double(HttpStub::Server::FormattedHash) }
|
7
|
+
subject { described_class.create(rack_request) }
|
78
8
|
|
79
|
-
|
80
|
-
|
81
|
-
before(:example) do
|
82
|
-
allow(HttpStub::Server::FormattedHash).to receive(:new).with(rack_parameters, anything).and_return(formatted_hash)
|
83
|
-
end
|
84
|
-
|
85
|
-
it "creates a formatted hash containing the rack request parameters" do
|
86
|
-
expect(HttpStub::Server::FormattedHash).to receive(:new).with(rack_parameters, anything)
|
87
|
-
|
88
|
-
subject
|
89
|
-
end
|
90
|
-
|
91
|
-
it "creates a formatted hash with a '=' request value delimiter" do
|
92
|
-
expect(HttpStub::Server::FormattedHash).to receive(:new).with(anything, "=")
|
9
|
+
it "creates a request for the rack request" do
|
10
|
+
expect(HttpStub::Server::Request::Request).to receive(:new).with(rack_request)
|
93
11
|
|
94
12
|
subject
|
95
13
|
end
|
96
14
|
|
97
|
-
it "
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
end
|
102
|
-
|
103
|
-
describe "#body" do
|
104
|
-
|
105
|
-
subject { server_request.body }
|
15
|
+
it "returns the created request" do
|
16
|
+
http_stub_request = instance_double(HttpStub::Server::Request::Request)
|
17
|
+
allow(HttpStub::Server::Request::Request).to receive(:new).and_return(http_stub_request)
|
106
18
|
|
107
|
-
|
108
|
-
expect(subject).to eql(rack_body)
|
19
|
+
expect(subject).to eql(http_stub_request)
|
109
20
|
end
|
110
21
|
|
111
22
|
end
|
@@ -14,7 +14,7 @@ describe HttpStub::Server::Response do
|
|
14
14
|
|
15
15
|
describe "::success" do
|
16
16
|
|
17
|
-
subject {
|
17
|
+
subject { described_class.success }
|
18
18
|
|
19
19
|
it_behaves_like "a success response"
|
20
20
|
|
@@ -63,11 +63,7 @@ describe HttpStub::Server::Response do
|
|
63
63
|
end
|
64
64
|
|
65
65
|
it "has no body" do
|
66
|
-
expect(subject.body).to be(
|
67
|
-
end
|
68
|
-
|
69
|
-
it "is empty" do
|
70
|
-
expect(subject).to be_empty
|
66
|
+
expect(subject.body.provided?).to be(false)
|
71
67
|
end
|
72
68
|
|
73
69
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
describe HttpStub::Server::Scenario::Controller do
|
2
2
|
|
3
3
|
let(:request_uri) { "/some/request/path" }
|
4
|
-
let(:request) { instance_double(HttpStub::Server::Request, uri: request_uri) }
|
4
|
+
let(:request) { instance_double(HttpStub::Server::Request::Request, uri: request_uri) }
|
5
5
|
let(:logger) { instance_double(Logger) }
|
6
6
|
let(:payload) { HttpStub::ScenarioFixture.new.server_payload }
|
7
7
|
let(:stubs) { (1..3).map { instance_double(HttpStub::Server::Stub::Stub) } }
|
@@ -6,7 +6,9 @@ describe HttpStub::Server::Scenario::Parser do
|
|
6
6
|
|
7
7
|
let(:parameters) { {} }
|
8
8
|
let(:body_hash) { {} }
|
9
|
-
let(:request)
|
9
|
+
let(:request) do
|
10
|
+
instance_double(HttpStub::Server::Request::Request, parameters: parameters, body: body_hash.to_json)
|
11
|
+
end
|
10
12
|
|
11
13
|
subject { parser.parse(request) }
|
12
14
|
|
@@ -1,6 +1,6 @@
|
|
1
1
|
describe HttpStub::Server::Stub::Controller do
|
2
2
|
|
3
|
-
let(:request) { instance_double(HttpStub::Server::Request) }
|
3
|
+
let(:request) { instance_double(HttpStub::Server::Request::Request) }
|
4
4
|
let(:logger) { instance_double(Logger) }
|
5
5
|
let(:payload) { HttpStub::StubFixture.new.server_payload }
|
6
6
|
let(:response) { instance_double(HttpStub::Server::Stub::Response::Base) }
|
@@ -62,10 +62,17 @@ describe HttpStub::Server::Stub::Controller do
|
|
62
62
|
|
63
63
|
let(:matched_stub) { the_stub }
|
64
64
|
|
65
|
-
it "
|
66
|
-
expect(the_stub).to receive(:
|
65
|
+
it "calculates the stubs response based on the request" do
|
66
|
+
expect(the_stub).to receive(:response_for).with(request)
|
67
67
|
|
68
|
-
|
68
|
+
subject
|
69
|
+
end
|
70
|
+
|
71
|
+
it "returns the calculated response" do
|
72
|
+
calculated_response = instance_double(HttpStub::Server::Stub::Response::Base)
|
73
|
+
allow(the_stub).to receive(:response_for).and_return(calculated_response)
|
74
|
+
|
75
|
+
expect(subject).to eql(calculated_response)
|
69
76
|
end
|
70
77
|
|
71
78
|
end
|
@@ -53,6 +53,16 @@ describe HttpStub::Server::Stub::Empty do
|
|
53
53
|
|
54
54
|
end
|
55
55
|
|
56
|
+
describe "#response_for" do
|
57
|
+
|
58
|
+
let(:request) { instance_double(HttpStub::Server::Request) }
|
59
|
+
|
60
|
+
it "returns unchanged empty stub" do
|
61
|
+
expect(empty_stub.response_for(request)).to eql(empty_stub)
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
|
56
66
|
describe "#to_s" do
|
57
67
|
|
58
68
|
it "returns an empty string" do
|
@@ -0,0 +1,122 @@
|
|
1
|
+
describe HttpStub::Server::Stub::Match::HashMatcher do
|
2
|
+
|
3
|
+
let(:stub_hash) do
|
4
|
+
(1..3).each_with_object({}) { |i, result| result["key#{i}"] = "stub value #{i}" }
|
5
|
+
end
|
6
|
+
|
7
|
+
let(:string_value_matcher) { HttpStub::Server::Stub::Match::StringValueMatcher }
|
8
|
+
|
9
|
+
describe "::match?" do
|
10
|
+
|
11
|
+
let(:actual_hash) { {} }
|
12
|
+
|
13
|
+
subject { described_class.match?(stub_hash, actual_hash) }
|
14
|
+
|
15
|
+
context "when the stub hash contains multiple entries" do
|
16
|
+
|
17
|
+
it "returns the result of evaluating a match for each stub value" do
|
18
|
+
stub_hash.values.each do |stub_value|
|
19
|
+
allow(string_value_matcher).to receive(:match?).with(stub_value, anything).and_return(true)
|
20
|
+
end
|
21
|
+
|
22
|
+
expect(subject).to be(true)
|
23
|
+
end
|
24
|
+
|
25
|
+
context "and the actual hash has the same number of entries" do
|
26
|
+
|
27
|
+
context "and the keys match" do
|
28
|
+
|
29
|
+
let(:actual_hash) do
|
30
|
+
(1..3).each_with_object({}) { |i, result| result["key#{i}"] = "actual value #{i}" }
|
31
|
+
end
|
32
|
+
|
33
|
+
context "and the values match" do
|
34
|
+
|
35
|
+
before(:example) { allow(string_value_matcher).to receive(:match?).and_return(true) }
|
36
|
+
|
37
|
+
it "returns true" do
|
38
|
+
expect(subject).to be(true)
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
context "and a value does not match" do
|
44
|
+
|
45
|
+
before(:example) do
|
46
|
+
allow(string_value_matcher).to receive(:match?).and_return(true)
|
47
|
+
allow(string_value_matcher).to receive(:match?).with("stub value 2", "actual value 2").and_return(false)
|
48
|
+
end
|
49
|
+
|
50
|
+
it "returns false" do
|
51
|
+
expect(subject).to be(false)
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
context "and a key does not match" do
|
59
|
+
|
60
|
+
let(:actual_hash) do
|
61
|
+
{ "key1" => "actual value 1", "differentkey2" => "different value 2", "key3" => "actual value 3" }
|
62
|
+
end
|
63
|
+
|
64
|
+
before(:example) { allow(string_value_matcher).to receive(:match?).and_return(true) }
|
65
|
+
|
66
|
+
it "determines if the corresponding value matches nil" do
|
67
|
+
expect(string_value_matcher).to receive(:match?).with("stub value 2", nil)
|
68
|
+
|
69
|
+
subject
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
context "and the actual hash contains more entries" do
|
77
|
+
|
78
|
+
let(:actual_hash) do
|
79
|
+
(1..5).each_with_object({}) { |i, result| result["key#{i}"] = "actual value #{i}" }
|
80
|
+
end
|
81
|
+
|
82
|
+
it "only evaluates a match for each stub value" do
|
83
|
+
stub_hash.values.each do |stub_value|
|
84
|
+
allow(string_value_matcher).to receive(:match?).with(stub_value, anything).and_return(true)
|
85
|
+
end
|
86
|
+
|
87
|
+
expect(subject).to be(true)
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
context "when the stubbed hash is empty" do
|
95
|
+
|
96
|
+
let(:stub_hash) { {} }
|
97
|
+
|
98
|
+
context "and the actual hash is not empty" do
|
99
|
+
|
100
|
+
let(:actual_hash) { { "key" => "value" } }
|
101
|
+
|
102
|
+
it "returns true" do
|
103
|
+
expect(subject).to be(true)
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
107
|
+
|
108
|
+
context "and the provided hash is empty" do
|
109
|
+
|
110
|
+
let(:actual_hash) { {} }
|
111
|
+
|
112
|
+
it "returns true" do
|
113
|
+
expect(subject).to be(true)
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
117
|
+
|
118
|
+
end
|
119
|
+
|
120
|
+
end
|
121
|
+
|
122
|
+
end
|
@@ -99,8 +99,8 @@ describe HttpStub::Server::Stub::Match::Rule::Body do
|
|
99
99
|
|
100
100
|
let(:stubbed_body) { "" }
|
101
101
|
|
102
|
-
it "returns a truthy
|
103
|
-
expect(subject).to eql(HttpStub::Server::Stub::Match::
|
102
|
+
it "returns a truthy match rule" do
|
103
|
+
expect(subject).to eql(HttpStub::Server::Stub::Match::Rule::Truthy)
|
104
104
|
end
|
105
105
|
|
106
106
|
end
|
@@ -109,8 +109,8 @@ describe HttpStub::Server::Stub::Match::Rule::Body do
|
|
109
109
|
|
110
110
|
let(:stubbed_body) { nil }
|
111
111
|
|
112
|
-
it "returns a truthy
|
113
|
-
expect(subject).to eql(HttpStub::Server::Stub::Match::
|
112
|
+
it "returns a truthy match rule" do
|
113
|
+
expect(subject).to eql(HttpStub::Server::Stub::Match::Rule::Truthy)
|
114
114
|
end
|
115
115
|
|
116
116
|
end
|
@@ -1,64 +1,53 @@
|
|
1
1
|
describe HttpStub::Server::Stub::Match::Rule::Headers do
|
2
2
|
|
3
|
-
let(:
|
3
|
+
let(:stub_headers) { { "stub_key" => "value" } }
|
4
4
|
|
5
|
-
let(:
|
5
|
+
let(:headers) { described_class.new(stub_headers) }
|
6
6
|
|
7
|
-
|
7
|
+
it "is a hash with indifferent and insensitive access" do
|
8
|
+
expect(headers).to be_a(HttpStub::Extensions::Core::Hash::IndifferentAndInsensitiveAccess)
|
9
|
+
end
|
8
10
|
|
9
|
-
|
11
|
+
context "when the stub header names contain hypens" do
|
10
12
|
|
11
|
-
|
12
|
-
downcased_and_underscored_hash = { "another_stub_key" => "value" }
|
13
|
-
expect(stubbed_headers).to receive(:downcase_and_underscore_keys).and_return(downcased_and_underscored_hash)
|
14
|
-
expect(HttpStub::Server::Stub::Match::HashWithStringValueMatchers).to(
|
15
|
-
receive(:new).with(downcased_and_underscored_hash)
|
16
|
-
)
|
13
|
+
let(:stub_headers) { (1..3).each_with_object({}) { |i, result| result["stub-key-#{i}"] = "value-#{i}" } }
|
17
14
|
|
18
|
-
|
15
|
+
it "underscores the names to accommodate for Rack Header manipulation" do
|
16
|
+
expect(headers.keys).to eql(stub_headers.keys.map(&:underscore))
|
19
17
|
end
|
20
18
|
|
21
|
-
|
19
|
+
it "leaves the values unchanged" do
|
20
|
+
expect(headers.values).to eql(stub_headers.values)
|
21
|
+
end
|
22
22
|
|
23
|
-
|
23
|
+
end
|
24
24
|
|
25
|
-
|
25
|
+
context "when no headers are provided" do
|
26
26
|
|
27
|
-
|
28
|
-
expect(HttpStub::Server::Stub::Match::HashWithStringValueMatchers).to receive(:new).with({})
|
27
|
+
let(:stub_headers) { nil }
|
29
28
|
|
30
|
-
|
29
|
+
it "is empty" do
|
30
|
+
expect(headers.empty?).to be(true)
|
31
31
|
end
|
32
32
|
|
33
33
|
end
|
34
34
|
|
35
35
|
describe "#matches?" do
|
36
36
|
|
37
|
-
let(:
|
38
|
-
let(:
|
39
|
-
|
40
|
-
end
|
41
|
-
let(:logger) { double(Logger) }
|
37
|
+
let(:request_headers) { instance_double(HttpStub::Server::Request::Headers) }
|
38
|
+
let(:request) { instance_double(HttpStub::Server::Request::Request, headers: request_headers) }
|
39
|
+
let(:logger) { double(Logger) }
|
42
40
|
|
43
|
-
subject {
|
41
|
+
subject { headers.matches?(request, logger) }
|
44
42
|
|
45
|
-
|
46
|
-
|
47
|
-
allow(HttpStub::Server::Stub::Match::HashWithStringValueMatchers).to(
|
48
|
-
receive(:new).and_return(regexpable_stubbed_headers)
|
49
|
-
)
|
50
|
-
end
|
51
|
-
|
52
|
-
it "downcases and underscore the keys in the request header hash" do
|
53
|
-
expect(request_header_hash).to receive(:downcase_and_underscore_keys)
|
43
|
+
it "determines if the stub header and request header hashes match" do
|
44
|
+
expect(HttpStub::Server::Stub::Match::HashMatcher).to receive(:match?).with(headers, request_headers)
|
54
45
|
|
55
46
|
subject
|
56
47
|
end
|
57
48
|
|
58
|
-
it "
|
59
|
-
|
60
|
-
allow(request_header_hash).to receive(:downcase_and_underscore_keys).and_return(downcased_and_underscored_hash)
|
61
|
-
expect(regexpable_stubbed_headers).to receive(:matches?).with(downcased_and_underscored_hash).and_return(true)
|
49
|
+
it "returns the result of the match" do
|
50
|
+
allow(HttpStub::Server::Stub::Match::HashMatcher).to receive(:match?).and_return(true)
|
62
51
|
|
63
52
|
expect(subject).to eql(true)
|
64
53
|
end
|
@@ -67,22 +56,42 @@ describe HttpStub::Server::Stub::Match::Rule::Headers do
|
|
67
56
|
|
68
57
|
describe "#to_s" do
|
69
58
|
|
70
|
-
|
59
|
+
subject { headers.to_s }
|
60
|
+
|
61
|
+
describe "when multiple headers are provided" do
|
62
|
+
|
63
|
+
let(:stub_headers) { (1..3).each_with_object({}) { |i, result| result["key#{i}"] = "value#{i}" } }
|
71
64
|
|
72
|
-
|
65
|
+
it "returns a string with each header name and value separated by ':'" do
|
66
|
+
result = subject
|
67
|
+
|
68
|
+
stub_headers.each { |name, value| expect(result).to match(/#{name}:#{value}/) }
|
69
|
+
end
|
70
|
+
|
71
|
+
it "separates each header with comma for readability" do
|
72
|
+
expect(subject).to match(/key\d.value\d, key\d.value\d, key\d.value\d/)
|
73
|
+
end
|
73
74
|
|
74
|
-
before(:example) do
|
75
|
-
allow(HttpStub::Server::FormattedHash).to receive(:new).with(stubbed_headers, anything).and_return(readable_hash)
|
76
75
|
end
|
77
76
|
|
78
|
-
|
79
|
-
|
77
|
+
describe "when empty headers are provided" do
|
78
|
+
|
79
|
+
let(:stub_headers) { {} }
|
80
|
+
|
81
|
+
it "returns an empty string" do
|
82
|
+
expect(subject).to eql("")
|
83
|
+
end
|
84
|
+
|
80
85
|
end
|
81
86
|
|
82
|
-
|
83
|
-
|
87
|
+
describe "when nil headers are provided" do
|
88
|
+
|
89
|
+
let(:stub_headers) { nil }
|
90
|
+
|
91
|
+
it "returns an empty string" do
|
92
|
+
expect(subject).to eql("")
|
93
|
+
end
|
84
94
|
|
85
|
-
subject
|
86
95
|
end
|
87
96
|
|
88
97
|
end
|