http_stub 0.16.0.pre1 → 0.17.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +13 -5
  2. data/lib/http_stub.rb +10 -5
  3. data/lib/http_stub/configurer.rb +1 -21
  4. data/lib/http_stub/configurer/dsl/{sanctioned.rb → server.rb} +7 -1
  5. data/lib/http_stub/configurer/dsl/stub_builder.rb +4 -0
  6. data/lib/http_stub/configurer/request/stub.rb +1 -0
  7. data/lib/http_stub/configurer/server/command_processor.rb +3 -3
  8. data/lib/http_stub/rake/server_tasks.rb +1 -1
  9. data/lib/http_stub/server/daemon.rb +3 -1
  10. data/lib/http_stub/server/stub/instance.rb +3 -2
  11. data/lib/http_stub/server/stub/json_request_body.rb +35 -0
  12. data/lib/http_stub/server/stub/request_body.rb +44 -0
  13. data/lib/http_stub/server/stub/simple_request_body.rb +23 -0
  14. data/lib/http_stub/server/stub/truthy_request_matcher.rb +23 -0
  15. data/lib/http_stub/server/views/_stub.haml +3 -0
  16. data/lib/http_stub/version.rb +1 -1
  17. data/spec/acceptance/configurer_initialization_spec.rb +1 -1
  18. data/spec/acceptance/stub_body_schema_validation_spec.rb +67 -0
  19. data/spec/acceptance/stub_control_values_spec.rb +2 -2
  20. data/spec/acceptance/stub_spec.rb +1 -1
  21. data/spec/lib/http_stub/configurer/dsl/{sanctioned_spec.rb → server_spec.rb} +34 -15
  22. data/spec/lib/http_stub/configurer/dsl/stub_builder_spec.rb +18 -0
  23. data/spec/lib/http_stub/configurer/request/stub_spec.rb +52 -20
  24. data/spec/lib/http_stub/configurer/server/command_processor_integration_spec.rb +7 -4
  25. data/spec/lib/http_stub/rake/server_tasks_smoke_spec.rb +54 -13
  26. data/spec/lib/http_stub/server/application_integration_spec.rb +22 -2
  27. data/spec/lib/http_stub/server/daemon_integration_spec.rb +1 -1
  28. data/spec/lib/http_stub/server/daemon_spec.rb +50 -2
  29. data/spec/lib/http_stub/server/stub/instance_spec.rb +44 -10
  30. data/spec/lib/http_stub/server/stub/json_request_body_spec.rb +102 -0
  31. data/spec/lib/http_stub/server/stub/request_body_spec.rb +120 -0
  32. data/spec/lib/http_stub/server/stub/simple_request_body_spec.rb +43 -0
  33. data/spec/lib/http_stub/server/stub/string_value_matcher_spec.rb +2 -2
  34. data/spec/lib/http_stub/server/stub/truthy_request_matcher_spec.rb +23 -0
  35. data/spec/lib/http_stub/server/stub/uri_spec.rb +1 -1
  36. data/spec/spec_helper.rb +3 -2
  37. data/spec/support/configurer_integration.rb +2 -2
  38. data/spec/support/stub_fixture.rb +2 -1
  39. metadata +307 -281
  40. data/spec/lib/http_stub/configurer_spec.rb +0 -22
@@ -1,7 +1,7 @@
1
1
  describe "Stub basics acceptance" do
2
2
  include_context "configurer integration"
3
3
 
4
- let(:configurer) { HttpStub::Examples::ConfigurerWithStub.new }
4
+ let(:configurer) { HttpStub::Examples::ConfigurerWithTrivialStub.new }
5
5
 
6
6
  before(:example) { configurer.class.initialize! }
7
7
 
@@ -1,11 +1,30 @@
1
- describe HttpStub::Configurer::DSL::Sanctioned do
1
+ describe HttpStub::Configurer::DSL::Server do
2
2
 
3
3
  let(:server_facade) { instance_double(HttpStub::Configurer::Server::Facade) }
4
4
 
5
- let(:dsl) { HttpStub::Configurer::DSL::Sanctioned.new(server_facade) }
5
+ let(:server) { HttpStub::Configurer::DSL::Server.new(server_facade) }
6
6
 
7
7
  it "produces stub builders" do
8
- expect(dsl).to be_a(HttpStub::Configurer::DSL::StubBuilderProducer)
8
+ expect(server).to be_a(HttpStub::Configurer::DSL::StubBuilderProducer)
9
+ end
10
+
11
+ describe "#base_uri" do
12
+
13
+ subject { server.base_uri }
14
+
15
+ before(:example) do
16
+ server.host = "some_host"
17
+ server.port = 8888
18
+ end
19
+
20
+ it "returns a uri that combines any established host and port" do
21
+ expect(subject).to include("some_host:8888")
22
+ end
23
+
24
+ it "returns a uri accessed via http" do
25
+ expect(subject).to match(/^http:\/\//)
26
+ end
27
+
9
28
  end
10
29
 
11
30
  describe "#has_started!" do
@@ -13,7 +32,7 @@ describe HttpStub::Configurer::DSL::Sanctioned do
13
32
  it "informs the facade that the server has started" do
14
33
  expect(server_facade).to receive(:server_has_started)
15
34
 
16
- dsl.has_started!
35
+ server.has_started!
17
36
  end
18
37
 
19
38
  end
@@ -43,7 +62,7 @@ describe HttpStub::Configurer::DSL::Sanctioned do
43
62
 
44
63
  context "when a stub builder is provided" do
45
64
 
46
- subject { dsl.add_stub!(stub_builder) }
65
+ subject { server.add_stub!(stub_builder) }
47
66
 
48
67
  it_behaves_like "adding a stub request"
49
68
 
@@ -55,7 +74,7 @@ describe HttpStub::Configurer::DSL::Sanctioned do
55
74
 
56
75
  before(:example) { allow(HttpStub::Configurer::DSL::StubBuilder).to receive(:new).and_return(stub_builder) }
57
76
 
58
- subject { dsl.add_stub!(&block) }
77
+ subject { server.add_stub!(&block) }
59
78
 
60
79
  it "creates a stub builder" do
61
80
  expect(HttpStub::Configurer::DSL::StubBuilder).to receive(:new)
@@ -89,13 +108,13 @@ describe HttpStub::Configurer::DSL::Sanctioned do
89
108
  allow(HttpStub::Configurer::DSL::ScenarioBuilder).to receive(:new).and_return(scenario_builder)
90
109
  end
91
110
 
92
- subject { dsl.add_scenario!(scenario_name, &block) }
111
+ subject { server.add_scenario!(scenario_name, &block) }
93
112
 
94
113
  context "when response defaults have been established" do
95
114
 
96
115
  let(:response_defaults) { { key: "value" } }
97
116
 
98
- before(:example) { dsl.response_defaults = { key: "value" } }
117
+ before(:example) { server.response_defaults = { key: "value" } }
99
118
 
100
119
  it "creates a scenario builder containing the response defaults" do
101
120
  expect(HttpStub::Configurer::DSL::ScenarioBuilder).to receive(:new).with(response_defaults, anything)
@@ -156,13 +175,13 @@ describe HttpStub::Configurer::DSL::Sanctioned do
156
175
  allow(HttpStub::Configurer::DSL::StubActivatorBuilder).to receive(:new).and_return(stub_activator_builder)
157
176
  end
158
177
 
159
- subject { dsl.add_activator!(&block) }
178
+ subject { server.add_activator!(&block) }
160
179
 
161
180
  context "when response defaults have been established" do
162
181
 
163
182
  let(:response_defaults) { { key: "value" } }
164
183
 
165
- before(:example) { dsl.response_defaults = { key: "value" } }
184
+ before(:example) { server.response_defaults = { key: "value" } }
166
185
 
167
186
  it "creates a stub activator builder containing the response defaults" do
168
187
  expect(HttpStub::Configurer::DSL::StubActivatorBuilder).to receive(:new).with(response_defaults)
@@ -209,7 +228,7 @@ describe HttpStub::Configurer::DSL::Sanctioned do
209
228
  it "delegates to the server facade" do
210
229
  expect(server_facade).to receive(:activate).with(scenario_name)
211
230
 
212
- dsl.activate!(scenario_name)
231
+ server.activate!(scenario_name)
213
232
  end
214
233
 
215
234
  end
@@ -219,7 +238,7 @@ describe HttpStub::Configurer::DSL::Sanctioned do
219
238
  it "delegates to the server facade" do
220
239
  expect(server_facade).to receive(:remember_stubs)
221
240
 
222
- dsl.remember_stubs
241
+ server.remember_stubs
223
242
  end
224
243
 
225
244
  end
@@ -229,7 +248,7 @@ describe HttpStub::Configurer::DSL::Sanctioned do
229
248
  it "delegates to the server facade" do
230
249
  expect(server_facade).to receive(:recall_stubs)
231
250
 
232
- dsl.recall_stubs!
251
+ server.recall_stubs!
233
252
  end
234
253
 
235
254
  end
@@ -239,7 +258,7 @@ describe HttpStub::Configurer::DSL::Sanctioned do
239
258
  it "delegates to the server facade" do
240
259
  expect(server_facade).to receive(:clear_stubs)
241
260
 
242
- dsl.clear_stubs!
261
+ server.clear_stubs!
243
262
  end
244
263
 
245
264
  end
@@ -249,7 +268,7 @@ describe HttpStub::Configurer::DSL::Sanctioned do
249
268
  it "delegates to the server facade" do
250
269
  expect(server_facade).to receive(:clear_scenarios)
251
270
 
252
- dsl.clear_scenarios!
271
+ server.clear_scenarios!
253
272
  end
254
273
 
255
274
  end
@@ -38,6 +38,24 @@ describe HttpStub::Configurer::DSL::StubBuilder do
38
38
 
39
39
  end
40
40
 
41
+ describe "#schema" do
42
+
43
+ let(:type) { :some_type }
44
+
45
+ subject { builder.schema(type, schema_definition) }
46
+
47
+ context "when a definition is provided in a ruby hash" do
48
+
49
+ let(:schema_definition) { { schema: "definition" } }
50
+
51
+ it "returns a hash with a :schema entry containing both the type and schema definition" do
52
+ expect(subject).to eql(schema: { type: type, definition: schema_definition })
53
+ end
54
+
55
+ end
56
+
57
+ end
58
+
41
59
  describe "#respond_with" do
42
60
 
43
61
  subject { builder.respond_with(status: 201) }
@@ -47,48 +47,80 @@ describe HttpStub::Configurer::Request::Stub do
47
47
 
48
48
  before(:example) { allow(HttpStub::Configurer::Request::ControllableValue).to receive(:format) }
49
49
 
50
- context "when request headers are provided" do
50
+ context "when request headers" do
51
51
 
52
- it "formats the headers into control values" do
53
- expect(HttpStub::Configurer::Request::ControllableValue).to receive(:format).with(fixture.request.headers)
52
+ context "are provided" do
53
+
54
+ it "formats the headers into control values" do
55
+ expect(HttpStub::Configurer::Request::ControllableValue).to receive(:format).with(fixture.request.headers)
56
+
57
+ subject
58
+ end
59
+
60
+ end
61
+
62
+ context "are not provided" do
63
+
64
+ before(:example) { fixture.request.headers = nil }
65
+
66
+ it "formats an empty header hash" do
67
+ expect(HttpStub::Configurer::Request::ControllableValue).to receive(:format).with({})
68
+
69
+ subject
70
+ end
54
71
 
55
- subject
56
72
  end
57
73
 
58
74
  end
59
75
 
60
- context "when no request header is provided" do
76
+ context "when request parameters" do
77
+
78
+ context "are provided" do
79
+
80
+ it "formats the request parameters into control values" do
81
+ expect(HttpStub::Configurer::Request::ControllableValue).to receive(:format).with(fixture.request.parameters)
82
+
83
+ subject
84
+ end
61
85
 
62
- before(:example) do
63
- fixture.request.headers = nil
64
86
  end
65
87
 
66
- it "formats an empty header hash" do
67
- expect(HttpStub::Configurer::Request::ControllableValue).to receive(:format).with({})
88
+ context "are not provided" do
89
+
90
+ before(:example) { fixture.request.parameters = nil }
91
+
92
+ it "formats an empty parameter hash" do
93
+ expect(HttpStub::Configurer::Request::ControllableValue).to receive(:format).with({})
94
+
95
+ subject
96
+ end
68
97
 
69
- subject
70
98
  end
71
99
 
72
100
  end
73
101
 
74
- context "when a request parameter is provided" do
102
+ context "when a request body" do
75
103
 
76
- it "formats the request parameters into control values" do
77
- expect(HttpStub::Configurer::Request::ControllableValue).to receive(:format).with(fixture.request.parameters)
104
+ context "is provided" do
105
+
106
+ it "formats the request body into a control value" do
107
+ expect(HttpStub::Configurer::Request::ControllableValue).to receive(:format).with(fixture.request.body)
108
+
109
+ subject
110
+ end
78
111
 
79
- subject
80
112
  end
81
113
 
82
- end
114
+ context "is not provided" do
83
115
 
84
- context "when no request parameter is provided" do
116
+ before(:example) { fixture.request.body = nil }
85
117
 
86
- before(:example) { fixture.request.parameters = nil }
118
+ it "formats an empty parameter hash" do
119
+ expect(HttpStub::Configurer::Request::ControllableValue).to receive(:format).with({})
87
120
 
88
- it "formats an empty parameter hash" do
89
- expect(HttpStub::Configurer::Request::ControllableValue).to receive(:format).with({})
121
+ subject
122
+ end
90
123
 
91
- subject
92
124
  end
93
125
 
94
126
  end
@@ -1,12 +1,15 @@
1
1
  describe HttpStub::Configurer::Server::CommandProcessor do
2
2
 
3
3
  let(:command) { HttpStub::Configurer::Server::Command.new(request: request, description: "performing an operation") }
4
- let(:server_base_uri) { "http://localhost:8001" }
5
-
6
- let(:configurer) do
7
- double(HttpStub::Configurer, get_base_uri: server_base_uri, get_host: "localhost", get_port: 8001)
4
+ let(:server_host) { "localhost" }
5
+ let(:server_port) { 8001 }
6
+ let(:server_base_uri) { "http://#{server_host}:#{server_port}" }
7
+ let(:stub_server) do
8
+ instance_double(HttpStub::Configurer::DSL::Server, base_uri: server_base_uri, host: server_host, port: server_port)
8
9
  end
9
10
 
11
+ let(:configurer) { double(HttpStub::Configurer, stub_server: stub_server) }
12
+
10
13
  let(:command_processor) { HttpStub::Configurer::Server::CommandProcessor.new(configurer) }
11
14
 
12
15
  describe "#process" do
@@ -1,30 +1,71 @@
1
1
  describe HttpStub::Rake::ServerTasks do
2
2
  include Rake::DSL
3
3
 
4
- before(:context) { HttpStub::Rake::ServerTasks.new(name: :test_server, port: 8003) }
4
+ shared_context "verification of generated tasks" do
5
5
 
6
- describe "start task" do
6
+ before(:example) { HttpStub::Rake::ServerTasks.new(task_args) }
7
7
 
8
- context "when invoked" do
8
+ describe "start:foreground task" do
9
9
 
10
- before(:context) do
11
- @server_thread = Thread.new { Rake::Task["test_server:start:foreground"].invoke("--trace") }
12
- ::Wait.until!("http stub server started") { Net::HTTP.get_response("localhost", "/", 8003) }
13
- end
10
+ let(:task) { Rake::Task["#{task_args[:name]}:start:foreground"] }
11
+
12
+ context "when invoked" do
13
+
14
+ before(:example) do
15
+ Thread.new { task.invoke("--trace") }
16
+ wait_until_server_has_started
17
+ end
18
+
19
+ after(:example) { wait_until_server_has_stopped }
20
+
21
+ it "starts a stub server that responds to stub requests" do
22
+ request = Net::HTTP::Post.new("/stubs")
23
+ request.body = { "uri" => "/", "response" => { "status" => 302, "body" => "Some Body" } }.to_json
14
24
 
15
- after(:context) { @server_thread.kill }
25
+ response = Net::HTTP.new("localhost", port).start { |http| http.request(request) }
16
26
 
17
- it "starts a stub server that responds to stub requests" do
18
- request = Net::HTTP::Post.new("/stubs")
19
- request.body = { "response" => { "status" => 302, "body" => "Some Body" } }.to_json
27
+ expect(response.code).to eql("200")
28
+ end
20
29
 
21
- response = Net::HTTP.new("localhost", 8003).start { |http| http.request(request) }
30
+ def wait_until_server_has_started
31
+ ::Wait.until!("http stub server #{task_args[:name]} started") do
32
+ Net::HTTP.get_response("localhost", "/", port)
33
+ end
34
+ end
35
+
36
+ def wait_until_server_has_stopped
37
+ HttpStub::Server::Application.stop!
38
+ end
22
39
 
23
- expect(response.code).to eql("200")
24
40
  end
25
41
 
26
42
  end
27
43
 
28
44
  end
29
45
 
46
+ context "when a configurer is provided" do
47
+
48
+ let(:port) { 8004 }
49
+ let(:configurer) do
50
+ configurer = Class.new.tap do |configurer|
51
+ configurer.send(:include, HttpStub::Configurer)
52
+ configurer.stub_server.host = "localhost"
53
+ configurer.stub_server.port = port
54
+ end
55
+ end
56
+ let(:task_args) { { name: :test_server_with_configurer, configurer: configurer } }
57
+
58
+ include_context "verification of generated tasks"
59
+
60
+ end
61
+
62
+ context "when a port is provided" do
63
+
64
+ let(:port) { 8003 }
65
+ let(:task_args) { { name: :test_server_with_port, port: port } }
66
+
67
+ include_context "verification of generated tasks"
68
+
69
+ end
70
+
30
71
  end
@@ -36,8 +36,8 @@ describe HttpStub::Server::Application, "when the server is running" do
36
36
 
37
37
  before(:context) do
38
38
  configurer = HttpStub::Examples::ConfigurerWithExhaustiveScenarios
39
- configurer.host(server_host)
40
- configurer.port(server_port)
39
+ configurer.stub_server.host = server_host
40
+ configurer.stub_server.port = server_port
41
41
  configurer.initialize!
42
42
  end
43
43
 
@@ -93,6 +93,26 @@ describe HttpStub::Server::Application, "when the server is running" do
93
93
  end
94
94
  end
95
95
 
96
+ it "returns a response whose body contains the bodies of each stub" do
97
+ (1..3).each do |stub_number|
98
+ expect(response.body).to(
99
+ match(/#{escape_html("\"property_#{stub_number}\":{\"type\":\"property_#{stub_number}_type\"")}/)
100
+ )
101
+ end
102
+ end
103
+
104
+ it "returns a response whose body contains the bodies of each stub trigger" do
105
+ (1..3).each do |stub_number|
106
+ (1..3).each do |trigger_number|
107
+ expected_property_name = "property_#{stub_number}_trigger_#{trigger_number}"
108
+ expected_property_type = "property_#{stub_number}_trigger_#{trigger_number}_type"
109
+ expect(response.body).to(
110
+ match(/#{escape_html("\"#{expected_property_name}\":{\"type\":\"#{expected_property_type}\"")}/)
111
+ )
112
+ end
113
+ end
114
+ end
115
+
96
116
  it "returns a response whose body contains the response status of each stub" do
97
117
  (1..3).each { |stub_number| expect(response.body).to match(/20#{stub_number}/) }
98
118
  end
@@ -2,6 +2,6 @@ describe HttpStub::Server::Daemon do
2
2
 
3
3
  let(:server) { HttpStub::Server::Daemon.new(name: :example_server_daemon, port: 8002) }
4
4
 
5
- it_should_behave_like "a managed http server"
5
+ it_behaves_like "a managed http server"
6
6
 
7
7
  end
@@ -1,8 +1,8 @@
1
1
  describe HttpStub::Server::Daemon do
2
2
 
3
- let(:configurer) { nil }
3
+ let(:server_port) { 8888 }
4
4
 
5
- let(:server_daemon) { HttpStub::Server::Daemon.new(name: :sample_server_daemon, port: 8888, configurer: configurer) }
5
+ let(:server_daemon) { create_daemon_without_configurer }
6
6
 
7
7
  before(:example) { allow(server_daemon.logger).to receive(:info) }
8
8
 
@@ -34,6 +34,42 @@ describe HttpStub::Server::Daemon do
34
34
 
35
35
  end
36
36
 
37
+ describe "constructor" do
38
+
39
+ context "when a configurer is provided" do
40
+
41
+ let(:server_host) { "some_host" }
42
+ let(:stub_server) { instance_double(HttpStub::Configurer::DSL::Server, host: server_host, port: server_port) }
43
+ let(:configurer) { double(HttpStub::Configurer, stub_server: stub_server) }
44
+
45
+ let(:server_daemon) { create_daemon_with_configurer }
46
+
47
+ it "establishes the daemons host as the configurers server host" do
48
+ expect(server_daemon.host).to eql(server_host)
49
+ end
50
+
51
+ it "establishes the daemons port as the configurers server port" do
52
+ expect(server_daemon.port).to eql(server_port)
53
+ end
54
+
55
+ end
56
+
57
+ context "when a configurer is not provided" do
58
+
59
+ let(:server_daemon) { create_daemon_without_configurer }
60
+
61
+ it "defaults the daemons host to 'localhost'" do
62
+ expect(server_daemon.host).to eql("localhost")
63
+ end
64
+
65
+ it "establishes the daemons port as the provided value" do
66
+ expect(server_daemon.port).to eql(server_port)
67
+ end
68
+
69
+ end
70
+
71
+ end
72
+
37
73
  describe "#start!" do
38
74
 
39
75
  before(:example) { allow(server_daemon).to receive(:running?).and_return(true) }
@@ -42,6 +78,8 @@ describe HttpStub::Server::Daemon do
42
78
 
43
79
  let(:configurer) { double(HttpStub::Configurer).as_null_object }
44
80
 
81
+ let(:server_daemon) { create_daemon_with_configurer }
82
+
45
83
  it "initializes the configurer" do
46
84
  expect(configurer).to receive(:initialize!)
47
85
 
@@ -58,6 +96,8 @@ describe HttpStub::Server::Daemon do
58
96
 
59
97
  context "when no configurer is provided" do
60
98
 
99
+ let(:server_daemon) { create_daemon_without_configurer }
100
+
61
101
  it "does not log that the server has been initialized" do
62
102
  expect(server_daemon.logger).not_to receive(:info).with("sample_server_daemon initialized")
63
103
 
@@ -68,4 +108,12 @@ describe HttpStub::Server::Daemon do
68
108
 
69
109
  end
70
110
 
111
+ def create_daemon_with_configurer
112
+ HttpStub::Server::Daemon.new(name: :sample_server_daemon, configurer: configurer)
113
+ end
114
+
115
+ def create_daemon_without_configurer
116
+ HttpStub::Server::Daemon.new(name: :sample_server_daemon, port: server_port)
117
+ end
118
+
71
119
  end