http_stub 0.19.1 → 0.20.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (114) hide show
  1. checksums.yaml +13 -5
  2. data/lib/http_stub.rb +26 -21
  3. data/lib/http_stub/configurer/server/command_processor.rb +1 -0
  4. data/lib/http_stub/server/application.rb +24 -46
  5. data/lib/http_stub/server/formatted_hash.rb +18 -0
  6. data/lib/http_stub/server/header_parser.rb +17 -0
  7. data/lib/http_stub/server/registry.rb +9 -9
  8. data/lib/http_stub/server/request.rb +19 -0
  9. data/lib/http_stub/server/request_pipeline.rb +27 -0
  10. data/lib/http_stub/server/response.rb +7 -3
  11. data/lib/http_stub/server/scenario.rb +1 -1
  12. data/lib/http_stub/server/scenario/activator.rb +4 -4
  13. data/lib/http_stub/server/scenario/controller.rb +8 -8
  14. data/lib/http_stub/server/scenario/{request_parser.rb → parser.rb} +2 -2
  15. data/lib/http_stub/server/scenario/{instance.rb → scenario.rb} +2 -2
  16. data/lib/http_stub/server/stub.rb +1 -1
  17. data/lib/http_stub/server/stub/controller.rb +8 -8
  18. data/lib/http_stub/server/stub/empty.rb +34 -0
  19. data/lib/http_stub/server/stub/match/exact_value_matcher.rb +17 -0
  20. data/lib/http_stub/server/stub/match/hash_with_string_value_matchers.rb +26 -0
  21. data/lib/http_stub/server/stub/match/match.rb +20 -0
  22. data/lib/http_stub/server/stub/match/omitted_value_matcher.rb +23 -0
  23. data/lib/http_stub/server/stub/match/regexp_value_matcher.rb +18 -0
  24. data/lib/http_stub/server/stub/match/rule/body.rb +49 -0
  25. data/lib/http_stub/server/stub/match/rule/headers.rb +31 -0
  26. data/lib/http_stub/server/stub/match/rule/json_body.rb +39 -0
  27. data/lib/http_stub/server/stub/match/rule/method.rb +27 -0
  28. data/lib/http_stub/server/stub/match/rule/parameters.rb +27 -0
  29. data/lib/http_stub/server/stub/match/rule/simple_body.rb +27 -0
  30. data/lib/http_stub/server/stub/match/rule/uri.rb +27 -0
  31. data/lib/http_stub/server/stub/match/string_value_matcher.rb +33 -0
  32. data/lib/http_stub/server/stub/match/truthy_matcher.rb +25 -0
  33. data/lib/http_stub/server/stub/{request_parser.rb → parser.rb} +2 -2
  34. data/lib/http_stub/server/stub/payload_file_consolidator.rb +1 -1
  35. data/lib/http_stub/server/stub/registry.rb +16 -8
  36. data/lib/http_stub/server/stub/response/base.rb +2 -2
  37. data/lib/http_stub/server/stub/stub.rb +44 -0
  38. data/lib/http_stub/server/stub/triggers.rb +2 -2
  39. data/lib/http_stub/server/views/_match.haml +20 -0
  40. data/lib/http_stub/server/views/matches.haml +3 -0
  41. data/lib/http_stub/server/views/stub.haml +1 -0
  42. data/lib/http_stub/version.rb +1 -1
  43. data/spec/acceptance/stub_control_values_spec.rb +6 -3
  44. data/spec/acceptance/stub_match_spec.rb +142 -0
  45. data/spec/lib/http_stub/configurer/server/command_processor_integration_spec.rb +4 -0
  46. data/spec/lib/http_stub/server/application_integration_spec.rb +17 -18
  47. data/spec/lib/http_stub/server/application_spec.rb +81 -53
  48. data/spec/lib/http_stub/server/formatted_hash_spec.rb +54 -0
  49. data/spec/lib/http_stub/server/{stub/request_header_parser_spec.rb → header_parser_spec.rb} +9 -9
  50. data/spec/lib/http_stub/server/{request_file_consolidator_spec.rb → payload_file_consolidator_spec.rb} +4 -4
  51. data/spec/lib/http_stub/server/registry_spec.rb +13 -14
  52. data/spec/lib/http_stub/server/request_pipeline_spec.rb +80 -0
  53. data/spec/lib/http_stub/server/request_spec.rb +113 -0
  54. data/spec/lib/http_stub/server/response_spec.rb +46 -12
  55. data/spec/lib/http_stub/server/scenario/activator_spec.rb +18 -18
  56. data/spec/lib/http_stub/server/scenario/controller_spec.rb +18 -15
  57. data/spec/lib/http_stub/server/scenario/{request_parser_spec.rb → parser_spec.rb} +8 -8
  58. data/spec/lib/http_stub/server/scenario/{instance_spec.rb → scenario_spec.rb} +7 -5
  59. data/spec/lib/http_stub/server/scenario_spec.rb +3 -3
  60. data/spec/lib/http_stub/server/stub/controller_spec.rb +29 -14
  61. data/spec/lib/http_stub/server/stub/empty_spec.rb +68 -0
  62. data/spec/lib/http_stub/server/stub/{exact_value_matcher_spec.rb → match/exact_value_matcher_spec.rb} +2 -2
  63. data/spec/lib/http_stub/server/stub/{hash_with_string_value_matchers_spec.rb → match/hash_with_string_value_matchers_spec.rb} +27 -19
  64. data/spec/lib/http_stub/server/stub/match/match_spec.rb +40 -0
  65. data/spec/lib/http_stub/server/stub/{omitted_value_matcher_spec.rb → match/omitted_value_matcher_spec.rb} +2 -2
  66. data/spec/lib/http_stub/server/stub/{regexp_value_matcher_spec.rb → match/regexp_value_matcher_spec.rb} +2 -2
  67. data/spec/lib/http_stub/server/stub/{request_body_spec.rb → match/rule/body_spec.rb} +12 -12
  68. data/spec/lib/http_stub/server/stub/match/rule/headers_spec.rb +90 -0
  69. data/spec/lib/http_stub/server/stub/{json_request_body_spec.rb → match/rule/json_body_spec.rb} +9 -10
  70. data/spec/lib/http_stub/server/stub/{method_spec.rb → match/rule/method_spec.rb} +6 -5
  71. data/spec/lib/http_stub/server/stub/{request_parameters_spec.rb → match/rule/parameters_spec.rb} +16 -10
  72. data/spec/lib/http_stub/server/stub/match/rule/simple_body_spec.rb +46 -0
  73. data/spec/lib/http_stub/server/stub/match/rule/uri_spec.rb +46 -0
  74. data/spec/lib/http_stub/server/stub/{string_value_matcher_spec.rb → match/string_value_matcher_spec.rb} +30 -26
  75. data/spec/lib/http_stub/server/stub/match/truthy_matcher_spec.rb +24 -0
  76. data/spec/lib/http_stub/server/stub/{request_parser_spec.rb → parser_spec.rb} +7 -7
  77. data/spec/lib/http_stub/server/stub/registry_integration_spec.rb +5 -5
  78. data/spec/lib/http_stub/server/stub/registry_spec.rb +114 -43
  79. data/spec/lib/http_stub/server/stub/response/base_spec.rb +8 -2
  80. data/spec/lib/http_stub/server/stub/stub_spec.rb +294 -0
  81. data/spec/lib/http_stub/server/stub/triggers_spec.rb +4 -4
  82. data/spec/lib/http_stub/server/stub_spec.rb +3 -3
  83. data/spec/spec_helper.rb +7 -3
  84. data/spec/support/configurer_integration.rb +1 -0
  85. data/spec/support/http_stub/empty_configurer.rb +7 -0
  86. data/spec/support/{scenario_fixture.rb → http_stub/scenario_fixture.rb} +0 -0
  87. data/spec/support/http_stub/server/request_fixture.rb +25 -0
  88. data/spec/support/http_stub/server/scenario/scenario_fixture.rb +15 -0
  89. data/spec/support/http_stub/server/stub/match/match_fixture.rb +17 -0
  90. data/spec/support/{stub_fixture.rb → http_stub/stub_fixture.rb} +0 -0
  91. data/spec/support/server_integration.rb +1 -0
  92. metadata +315 -290
  93. data/lib/http_stub/server/stub/exact_value_matcher.rb +0 -15
  94. data/lib/http_stub/server/stub/hash_with_string_value_matchers.rb +0 -22
  95. data/lib/http_stub/server/stub/headers.rb +0 -19
  96. data/lib/http_stub/server/stub/instance.rb +0 -32
  97. data/lib/http_stub/server/stub/json_request_body.rb +0 -35
  98. data/lib/http_stub/server/stub/method.rb +0 -23
  99. data/lib/http_stub/server/stub/omitted_value_matcher.rb +0 -21
  100. data/lib/http_stub/server/stub/regexp_value_matcher.rb +0 -16
  101. data/lib/http_stub/server/stub/request_body.rb +0 -44
  102. data/lib/http_stub/server/stub/request_header_parser.rb +0 -19
  103. data/lib/http_stub/server/stub/request_headers.rb +0 -28
  104. data/lib/http_stub/server/stub/request_parameters.rb +0 -23
  105. data/lib/http_stub/server/stub/simple_request_body.rb +0 -23
  106. data/lib/http_stub/server/stub/string_value_matcher.rb +0 -31
  107. data/lib/http_stub/server/stub/truthy_request_matcher.rb +0 -23
  108. data/lib/http_stub/server/stub/uri.rb +0 -23
  109. data/spec/lib/http_stub/server/stub/headers_spec.rb +0 -51
  110. data/spec/lib/http_stub/server/stub/instance_spec.rb +0 -235
  111. data/spec/lib/http_stub/server/stub/request_headers_spec.rb +0 -68
  112. data/spec/lib/http_stub/server/stub/simple_request_body_spec.rb +0 -43
  113. data/spec/lib/http_stub/server/stub/truthy_request_matcher_spec.rb +0 -23
  114. data/spec/lib/http_stub/server/stub/uri_spec.rb +0 -43
@@ -1,19 +1,19 @@
1
- describe HttpStub::Server::Stub::RequestParser do
1
+ describe HttpStub::Server::Stub::Parser do
2
2
 
3
- let(:request_parser) { described_class }
3
+ let(:parser) { described_class }
4
4
 
5
5
  describe "::parse" do
6
6
 
7
- let(:params) { {} }
8
- let(:body_hash) { {} }
9
- let(:request) { instance_double(Rack::Request, params: params, body: StringIO.new(body_hash.to_json)) }
7
+ let(:parameters) { {} }
8
+ let(:body_hash) { {} }
9
+ let(:request) { instance_double(HttpStub::Server::Request, parameters: parameters, body: body_hash.to_json) }
10
10
 
11
- subject { request_parser.parse(request) }
11
+ subject { parser.parse(request) }
12
12
 
13
13
  context "when the request contains a payload parameter" do
14
14
 
15
15
  let(:payload) { HttpStub::StubFixture.new.server_payload }
16
- let(:params) { { "payload" => payload.to_json } }
16
+ let(:parameters) { { "payload" => payload.to_json } }
17
17
 
18
18
  it "consolidates any files into the payload" do
19
19
  expect(HttpStub::Server::Stub::PayloadFileConsolidator).to receive(:consolidate!).with(payload, request)
@@ -1,9 +1,9 @@
1
1
  describe HttpStub::Server::Stub::Registry, "integrating with real stubs" do
2
2
 
3
- let(:logger) { double("Logger").as_null_object }
4
- let(:request) { instance_double(Rack::Request, logger: logger) }
3
+ let(:request_match_registry) { HttpStub::Server::Registry.new("request match") }
4
+ let(:logger) { instance_double(Logger, info: nil) }
5
5
 
6
- let(:stub_registry) { HttpStub::Server::Stub::Registry.new }
6
+ let(:stub_registry) { HttpStub::Server::Stub::Registry.new(request_match_registry) }
7
7
 
8
8
  describe "#recall" do
9
9
 
@@ -13,7 +13,7 @@ describe HttpStub::Server::Stub::Registry, "integrating with real stubs" do
13
13
 
14
14
  let(:stubs) { (1..3).map { |i| create_stub(i) } }
15
15
 
16
- before(:example) { stubs.each { |stub| stub_registry.add(stub, request) } }
16
+ before(:example) { stubs.each { |stub| stub_registry.add(stub, logger) } }
17
17
 
18
18
  context "and remembered" do
19
19
 
@@ -23,7 +23,7 @@ describe HttpStub::Server::Stub::Registry, "integrating with real stubs" do
23
23
 
24
24
  let(:stub_to_add) { create_stub(4) }
25
25
 
26
- before(:example) { stub_registry.add(stub_to_add, request) }
26
+ before(:example) { stub_registry.add(stub_to_add, logger) }
27
27
 
28
28
  it "should restore all known stubs to the remembered state" do
29
29
  subject
@@ -1,80 +1,140 @@
1
1
  describe HttpStub::Server::Stub::Registry do
2
2
 
3
- let(:registry) { instance_double(HttpStub::Server::Registry) }
3
+ let(:match_registry) { instance_double(HttpStub::Server::Registry) }
4
+ let(:underlying_stub_registry) { instance_double(HttpStub::Server::Registry) }
4
5
 
5
- let(:stub_registry) { HttpStub::Server::Stub::Registry.new }
6
+ let(:logger) { instance_double(Logger) }
6
7
 
7
- before(:example) { allow(HttpStub::Server::Registry).to receive(:new).and_return(registry) }
8
+ let(:stub_registry) { HttpStub::Server::Stub::Registry.new(match_registry) }
9
+
10
+ before(:example) { allow(HttpStub::Server::Registry).to receive(:new).and_return(underlying_stub_registry) }
8
11
 
9
12
  describe "#add" do
10
13
 
11
- let(:stub) { instance_double(HttpStub::Server::Stub::Instance) }
12
- let(:request) { instance_double(Rack::Request) }
14
+ let(:stub) { instance_double(HttpStub::Server::Stub::Stub) }
15
+
16
+ subject { stub_registry.add(stub, logger) }
13
17
 
14
18
  it "delegates to an underlying simple registry" do
15
- expect(registry).to receive(:add).with(stub, request)
19
+ expect(underlying_stub_registry).to receive(:add).with(stub, logger)
16
20
 
17
- stub_registry.add(stub, request)
21
+ subject
18
22
  end
19
23
 
20
24
  end
21
25
 
22
26
  describe "#concat" do
23
27
 
24
- let(:stubs) { (1..3).map { instance_double(HttpStub::Server::Stub::Instance) } }
25
- let(:request) { instance_double(Rack::Request) }
28
+ let(:stubs) { (1..3).map { instance_double(HttpStub::Server::Stub::Stub) } }
29
+
30
+ subject { stub_registry.concat(stubs, logger) }
26
31
 
27
32
  it "delegates to an underlying simple registry" do
28
- expect(registry).to receive(:concat).with(stubs, request)
33
+ expect(underlying_stub_registry).to receive(:concat).with(stubs, logger)
29
34
 
30
- stub_registry.concat(stubs, request)
35
+ subject
31
36
  end
32
37
 
33
38
  end
34
39
 
35
- describe "#find_for" do
40
+ describe "#find" do
36
41
 
37
- let(:request) { instance_double(Rack::Request) }
42
+ let(:triggers) { instance_double(HttpStub::Server::Stub::Triggers, add_to: nil) }
43
+ let(:stub) { instance_double(HttpStub::Server::Stub::Stub, triggers: triggers) }
38
44
 
39
- subject { stub_registry.find_for(request) }
45
+ subject { stub_registry.find(criteria, logger) }
40
46
 
41
- it "delegates to an underlying simple registry to find based on the request" do
42
- expect(registry).to receive(:find).with(criteria: request, request: request)
47
+ shared_examples_for "an approach to finding a stub in the registry" do
43
48
 
44
- subject
45
- end
49
+ it "delegates to an underlying simple registry to find based on the criteria" do
50
+ expect(underlying_stub_registry).to receive(:find).with(criteria, logger)
46
51
 
47
- context "when a stub is found" do
52
+ subject
53
+ end
48
54
 
49
- let(:triggers) { instance_double(HttpStub::Server::Stub::Triggers) }
50
- let(:stub) { instance_double(HttpStub::Server::Stub::Instance, triggers: triggers) }
55
+ context "when a stub is found" do
51
56
 
52
- before(:example) { allow(registry).to receive(:find).and_return(stub) }
57
+ before(:example) { allow(underlying_stub_registry).to receive(:find).and_return(stub) }
53
58
 
54
- it "should add the stubs triggers to the registry" do
55
- expect(triggers).to receive(:add_to).with(stub_registry, request)
59
+ it "returns the stub found in the underlying stub registry" do
60
+ expect(subject).to eql(stub)
61
+ end
56
62
 
57
- subject
58
63
  end
59
64
 
60
- it "returns the stub found in the underlying registry" do
61
- allow(triggers).to receive(:add_to)
65
+ context "when a stub is not found" do
66
+
67
+ before(:example) { allow(underlying_stub_registry).to receive(:find).and_return(nil) }
68
+
69
+ it "returns the result from the underlying registry" do
70
+ expect(subject).to eql(nil)
71
+ end
62
72
 
63
- expect(subject).to eql(stub)
64
73
  end
65
74
 
66
75
  end
67
76
 
68
- context "when a stub is not found" do
77
+ context "when a request is provided" do
78
+
79
+ let(:request) { HttpStub::Server::RequestFixture.create }
80
+ let(:criteria) { request }
81
+
82
+ before(:example) { allow(match_registry).to receive(:add) }
83
+
84
+ it_behaves_like "an approach to finding a stub in the registry"
85
+
86
+ context "when a stub is found" do
87
+
88
+ let(:stub_match) { instance_double(HttpStub::Server::Stub::Match::Match) }
89
+
90
+ before(:example) do
91
+ allow(underlying_stub_registry).to receive(:find).and_return(stub)
92
+ allow(HttpStub::Server::Stub::Match::Match).to receive(:new).and_return(stub_match)
93
+ end
94
+
95
+ it "creates a match containing the stub and request" do
96
+ expect(HttpStub::Server::Stub::Match::Match).to receive(:new).with(stub, request)
97
+
98
+ subject
99
+ end
100
+
101
+ it "adds the match to the match registry" do
102
+ expect(match_registry).to receive(:add).with(stub_match, logger)
69
103
 
70
- before(:example) { allow(registry).to receive(:find).and_return(nil) }
104
+ subject
105
+ end
106
+
107
+ it "adds the stubs triggers to the underlying stub registry" do
108
+ expect(triggers).to receive(:add_to).with(stub_registry, logger)
109
+
110
+ subject
111
+ end
112
+
113
+ end
114
+
115
+ context "when a stub is not found" do
116
+
117
+ before(:example) { allow(underlying_stub_registry).to receive(:find).and_return(nil) }
118
+
119
+ it "does not add a match to the match registry" do
120
+ expect(match_registry).to_not receive(:add)
121
+
122
+ subject
123
+ end
71
124
 
72
- it "returns the result from the underlying registry" do
73
- expect(subject).to eql(nil)
74
125
  end
75
126
 
76
127
  end
77
128
 
129
+ context "when an id is provided" do
130
+
131
+ let(:id) { SecureRandom.uuid }
132
+ let(:criteria) { id }
133
+
134
+ it_behaves_like "an approach to finding a stub in the registry"
135
+
136
+ end
137
+
78
138
  end
79
139
 
80
140
  describe "#recall" do
@@ -83,15 +143,15 @@ describe HttpStub::Server::Stub::Registry do
83
143
 
84
144
  context "when the state of the registry has been remembered" do
85
145
 
86
- let(:last_stub_remembered) { instance_double(HttpStub::Server::Stub::Instance) }
146
+ let(:last_stub_remembered) { instance_double(HttpStub::Server::Stub::Stub) }
87
147
 
88
148
  before(:example) do
89
- allow(registry).to receive(:last).and_return(last_stub_remembered)
149
+ allow(underlying_stub_registry).to receive(:last).and_return(last_stub_remembered)
90
150
  stub_registry.remember
91
151
  end
92
152
 
93
153
  it "causes the underlying registry to rollback to the last stub added before the state was remembered" do
94
- expect(registry).to receive(:rollback_to).with(last_stub_remembered)
154
+ expect(underlying_stub_registry).to receive(:rollback_to).with(last_stub_remembered)
95
155
 
96
156
  subject
97
157
  end
@@ -101,7 +161,7 @@ describe HttpStub::Server::Stub::Registry do
101
161
  context "when the state of the registry has not been remembered" do
102
162
 
103
163
  it "does not rollback the underlying registry" do
104
- expect(registry).to_not receive(:rollback_to)
164
+ expect(underlying_stub_registry).to_not receive(:rollback_to)
105
165
 
106
166
  subject
107
167
  end
@@ -112,18 +172,18 @@ describe HttpStub::Server::Stub::Registry do
112
172
 
113
173
  describe "#all" do
114
174
 
115
- let(:stubs) { (1..3).map { instance_double(HttpStub::Server::Stub::Instance) } }
175
+ let(:stubs) { (1..3).map { instance_double(HttpStub::Server::Stub::Stub) } }
116
176
 
117
177
  subject { stub_registry.all }
118
178
 
119
179
  it "delegates to an underlying simple registry" do
120
- expect(registry).to receive(:all)
180
+ expect(underlying_stub_registry).to receive(:all)
121
181
 
122
182
  subject
123
183
  end
124
184
 
125
185
  it "returns the result from the underlying registry" do
126
- allow(registry).to receive(:all).and_return(stubs)
186
+ allow(underlying_stub_registry).to receive(:all).and_return(stubs)
127
187
 
128
188
  expect(subject).to eql(stubs)
129
189
  end
@@ -132,12 +192,23 @@ describe HttpStub::Server::Stub::Registry do
132
192
 
133
193
  describe "#clear" do
134
194
 
135
- let(:request) { instance_double(Rack::Request) }
195
+ subject { stub_registry.clear(logger) }
136
196
 
137
- it "delegates to an underlying simple registry" do
138
- expect(registry).to receive(:clear).with(request)
197
+ before(:example) do
198
+ allow(underlying_stub_registry).to receive(:clear)
199
+ allow(match_registry).to receive(:clear)
200
+ end
201
+
202
+ it "clears the underlying simple registry" do
203
+ expect(underlying_stub_registry).to receive(:clear).with(logger)
139
204
 
140
- stub_registry.clear(request)
205
+ subject
206
+ end
207
+
208
+ it "clears the match registry" do
209
+ expect(match_registry).to receive(:clear).with(logger)
210
+
211
+ subject
141
212
  end
142
213
 
143
214
  end
@@ -96,8 +96,14 @@ describe HttpStub::Server::Stub::Response::Base do
96
96
 
97
97
  let(:response_header_hash) { response.headers.to_hash }
98
98
 
99
- it "is Headers" do
100
- expect(response.headers).to be_a(HttpStub::Server::Stub::Headers)
99
+ it "is a readable hash" do
100
+ expect(response.headers).to be_a(HttpStub::Server::FormattedHash)
101
+ end
102
+
103
+ it "formats the hash with a ':' key value delimiter" do
104
+ expect(HttpStub::Server::FormattedHash).to receive(:new).with(anything, ":")
105
+
106
+ response.headers
101
107
  end
102
108
 
103
109
  context "when default headers have been added" do
@@ -0,0 +1,294 @@
1
+ describe HttpStub::Server::Stub::Stub do
2
+
3
+ let(:request_header_payload) do
4
+ {
5
+ "header1" => "header_value1",
6
+ "header2" => "header_value2",
7
+ "header3" => "header_value3"
8
+ }
9
+ end
10
+ let(:request_parameter_payload) do
11
+ {
12
+ "param1" => "param_value1",
13
+ "param2" => "param_value2",
14
+ "param3" => "param_value3"
15
+ }
16
+ end
17
+ let(:request_method_payload) { "get" }
18
+ let(:trigger_payload) do
19
+ {
20
+ "uri" => "/a_triggered_path",
21
+ "method" => "post",
22
+ "headers" => { "triggered_header" => "triggered_header_value" },
23
+ "parameters" => { "triggered_parameter" => "triggered_parameter_value" },
24
+ "body" => { "schema" => { "json" => "trigger schema definition" } },
25
+ "response" => {
26
+ "status" => 203,
27
+ "body" => "Triggered body"
28
+ }
29
+ }
30
+ end
31
+ let(:stub_id) { SecureRandom.uuid }
32
+ let(:stub_payload) do
33
+ {
34
+ "id" => stub_id,
35
+ "uri" => "/a_path",
36
+ "method" => stub_method,
37
+ "headers" => request_header_payload,
38
+ "parameters" => request_parameter_payload,
39
+ "body" => { "schema" => { "json" => "stub schema definition" } },
40
+ "response" => {
41
+ "status" => 201,
42
+ "body" => "Some body"
43
+ },
44
+ "triggers" => [ trigger_payload ]
45
+ }
46
+ end
47
+ let(:stub_method) { instance_double(HttpStub::Server::Stub::Match::Rule::Method, matches?: true) }
48
+ let(:uri) { instance_double(HttpStub::Server::Stub::Match::Rule::Uri, matches?: true) }
49
+ let(:request_headers) { instance_double(HttpStub::Server::Stub::Match::Rule::Headers, matches?: true) }
50
+ let(:request_parameters) { instance_double(HttpStub::Server::Stub::Match::Rule::Parameters, matches?: true) }
51
+ let(:request_body) { double("HttpStub::Server::Stub::SomeRequestBody", matches?: true) }
52
+ let(:response) { instance_double(HttpStub::Server::Stub::Response::Base) }
53
+ let(:triggers) { instance_double(HttpStub::Server::Stub::Triggers) }
54
+
55
+ let(:the_stub) { HttpStub::Server::Stub::Stub.new(stub_payload) }
56
+
57
+ before(:example) do
58
+ allow(HttpStub::Server::Stub::Match::Rule::Method).to receive(:new).and_return(stub_method)
59
+ allow(HttpStub::Server::Stub::Match::Rule::Uri).to receive(:new).and_return(uri)
60
+ allow(HttpStub::Server::Stub::Match::Rule::Headers).to receive(:new).and_return(request_headers)
61
+ allow(HttpStub::Server::Stub::Match::Rule::Parameters).to receive(:new).and_return(request_parameters)
62
+ allow(HttpStub::Server::Stub::Match::Rule::Body).to receive(:create).and_return(request_body)
63
+ allow(HttpStub::Server::Stub::Response).to receive(:create).and_return(response)
64
+ allow(HttpStub::Server::Stub::Triggers).to receive(:new).and_return(triggers)
65
+ end
66
+
67
+ describe "#matches?" do
68
+
69
+ let(:logger) { instance_double(Logger) }
70
+
71
+ context "when a request is provided" do
72
+
73
+ let(:request_method) { request_method_payload }
74
+ let(:request_uri) { "/a_request_uri" }
75
+ let(:request) { instance_double(HttpStub::Server::Request, method: request_method_payload) }
76
+
77
+ subject { the_stub.matches?(request, logger) }
78
+
79
+ describe "when the request uri matches" do
80
+
81
+ before(:example) { allow(uri).to receive(:matches?).with(request, logger).and_return(true) }
82
+
83
+ describe "and the request method matches" do
84
+
85
+ describe "and a header match is configured" do
86
+
87
+ describe "that matches" do
88
+
89
+ before(:example) { allow(request_headers).to receive(:matches?).with(request, logger).and_return(true) }
90
+
91
+ describe "and a parameter match is configured" do
92
+
93
+ describe "that matches" do
94
+
95
+ before(:example) do
96
+ allow(request_parameters).to receive(:matches?).with(request, logger).and_return(true)
97
+ end
98
+
99
+ describe "and a body match is configured" do
100
+
101
+ describe "that matches" do
102
+
103
+ before(:example) do
104
+ allow(request_body).to receive(:matches?).with(request, logger).and_return(true)
105
+ end
106
+
107
+ it "returns true" do
108
+ expect(subject).to be(true)
109
+ end
110
+
111
+ end
112
+
113
+ end
114
+
115
+ end
116
+
117
+ end
118
+
119
+ end
120
+
121
+ end
122
+
123
+ end
124
+
125
+ end
126
+
127
+
128
+ describe "when the request uri does not match" do
129
+
130
+ before(:example) { allow(uri).to receive(:matches?).with(request, logger).and_return(false) }
131
+
132
+ it "returns false" do
133
+ expect(subject).to be(false)
134
+ end
135
+
136
+ end
137
+
138
+ describe "when the request method does not match" do
139
+
140
+ before(:example) { allow(stub_method).to receive(:matches?).with(request, logger).and_return(false) }
141
+
142
+ it "returns false" do
143
+ expect(subject).to be(false)
144
+ end
145
+
146
+ end
147
+
148
+ describe "when the headers do not match" do
149
+
150
+ before(:example) { allow(request_headers).to receive(:matches?).with(request, logger).and_return(false) }
151
+
152
+ it "returns false" do
153
+ expect(subject).to be(false)
154
+ end
155
+
156
+ end
157
+
158
+ describe "when the parameters do not match" do
159
+
160
+ before(:example) { allow(request_parameters).to receive(:matches?).with(request, logger).and_return(false) }
161
+
162
+ it "returns false" do
163
+ expect(subject).to be(false)
164
+ end
165
+
166
+ end
167
+
168
+ describe "when the bodies do not match" do
169
+
170
+ before(:example) { allow(request_body).to receive(:matches?).with(request, logger).and_return(false) }
171
+
172
+ it "returns false" do
173
+ expect(subject).to be(false)
174
+ end
175
+
176
+ end
177
+
178
+ end
179
+
180
+ context "when an id is provided" do
181
+
182
+ subject { the_stub.matches?(id, logger) }
183
+
184
+ context "and the id matches the stubs id" do
185
+
186
+ let(:id) { stub_id }
187
+
188
+ it "returns true" do
189
+ expect(subject).to be(true)
190
+ end
191
+
192
+ end
193
+
194
+ context "and the id does not match the stubs id" do
195
+
196
+ let(:id) { "does-not-match" }
197
+
198
+ it "returns false" do
199
+ expect(subject).to be(false)
200
+ end
201
+
202
+ end
203
+
204
+ end
205
+
206
+ end
207
+
208
+ describe "#uri" do
209
+
210
+ it "returns the uri model encapsulating the uri provided in the request body" do
211
+ expect(the_stub.uri).to eql(uri)
212
+ end
213
+
214
+ end
215
+
216
+ describe "#method" do
217
+
218
+ it "returns the method model encapsulating the method provided in the request body" do
219
+ expect(the_stub.method).to eql(stub_method)
220
+ end
221
+
222
+ end
223
+
224
+ describe "#headers" do
225
+
226
+ it "returns the headers model encapsulating the headers provided in the request body" do
227
+ expect(the_stub.headers).to eql(request_headers)
228
+ end
229
+
230
+ end
231
+
232
+ describe "#parameters" do
233
+
234
+ it "returns the parameters model encapsulating the parameters provided in the request body" do
235
+ expect(the_stub.parameters).to eql(request_parameters)
236
+ end
237
+
238
+ end
239
+
240
+ describe "#body" do
241
+
242
+ it "returns the body model encapsulating the body provided in the request body" do
243
+ expect(the_stub.body).to eql(request_body)
244
+ end
245
+
246
+ end
247
+
248
+ describe "#response" do
249
+
250
+ it "exposes the response model encapsulating the response provided in the request body" do
251
+ expect(the_stub.response).to eql(response)
252
+ end
253
+
254
+ end
255
+
256
+ describe "#triggers" do
257
+
258
+ it "returns the triggers model encapsulating the triggers provided in the request body" do
259
+ expect(the_stub.triggers).to eql(triggers)
260
+ end
261
+
262
+ end
263
+
264
+ describe "#stub_uri" do
265
+
266
+ context "when an id is provided in the payload" do
267
+
268
+ it "returns a relative uri to the stub that includes the id" do
269
+ expect(the_stub.stub_uri).to eql("/stubs/#{stub_id}")
270
+ end
271
+
272
+ end
273
+
274
+ context "when an id is not provided in the payload" do
275
+
276
+ it "returns a relative uri to the stub that includes a generated id" do
277
+ expect(the_stub.stub_uri).to match(/\/stubs\/[a-zA-Z0-9-]+$/)
278
+ end
279
+
280
+ end
281
+
282
+ end
283
+
284
+ describe "#to_s" do
285
+
286
+ it "returns a string representation of the stub arguments" do
287
+ expect(stub_payload).to receive(:to_s).and_return("stub arguments string")
288
+
289
+ expect(the_stub.to_s).to eql("stub arguments string")
290
+ end
291
+
292
+ end
293
+
294
+ end