http_stub 0.15.5 → 0.16.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (137) hide show
  1. checksums.yaml +4 -4
  2. data/lib/http_stub.rb +37 -27
  3. data/lib/http_stub/configurer.rb +2 -2
  4. data/lib/http_stub/configurer/dsl/deprecated.rb +60 -0
  5. data/lib/http_stub/configurer/{server/dsl.rb → dsl/sanctioned.rb} +18 -18
  6. data/lib/http_stub/configurer/dsl/scenario_activator.rb +15 -0
  7. data/lib/http_stub/configurer/dsl/scenario_builder.rb +37 -0
  8. data/lib/http_stub/configurer/dsl/stub_activator_builder.rb +24 -0
  9. data/lib/http_stub/configurer/{request → dsl}/stub_builder.rb +4 -3
  10. data/lib/http_stub/configurer/dsl/stub_builder_producer.rb +21 -0
  11. data/lib/http_stub/configurer/request/http/factory.rb +8 -2
  12. data/lib/http_stub/configurer/request/scenario.rb +29 -0
  13. data/lib/http_stub/configurer/server/facade.rb +7 -7
  14. data/lib/http_stub/server/application.rb +21 -14
  15. data/lib/http_stub/server/registry.rb +7 -3
  16. data/lib/http_stub/server/response.rb +3 -3
  17. data/lib/http_stub/server/scenario.rb +12 -0
  18. data/lib/http_stub/server/scenario/activator.rb +25 -0
  19. data/lib/http_stub/server/scenario/controller.rb +36 -0
  20. data/lib/http_stub/server/scenario/instance.rb +36 -0
  21. data/lib/http_stub/server/scenario/request_parser.rb +19 -0
  22. data/lib/http_stub/server/stub.rb +3 -23
  23. data/lib/http_stub/server/stub/controller.rb +30 -0
  24. data/lib/http_stub/server/stub/exact_value_matcher.rb +15 -0
  25. data/lib/http_stub/server/stub/hash_with_string_value_matchers.rb +22 -0
  26. data/lib/http_stub/server/stub/headers.rb +19 -0
  27. data/lib/http_stub/server/stub/instance.rb +31 -0
  28. data/lib/http_stub/server/stub/method.rb +23 -0
  29. data/lib/http_stub/server/stub/omitted_value_matcher.rb +21 -0
  30. data/lib/http_stub/server/stub/payload_file_consolidator.rb +18 -0
  31. data/lib/http_stub/server/stub/regexp_value_matcher.rb +16 -0
  32. data/lib/http_stub/server/stub/registry.rb +31 -0
  33. data/lib/http_stub/server/stub/request_header_parser.rb +19 -0
  34. data/lib/http_stub/server/stub/request_headers.rb +28 -0
  35. data/lib/http_stub/server/stub/request_parameters.rb +23 -0
  36. data/lib/http_stub/server/stub/request_parser.rb +17 -0
  37. data/lib/http_stub/server/stub/response.rb +16 -0
  38. data/lib/http_stub/server/stub/response/base.rb +64 -0
  39. data/lib/http_stub/server/stub/response/file.rb +36 -0
  40. data/lib/http_stub/server/stub/response/text.rb +19 -0
  41. data/lib/http_stub/server/stub/string_value_matcher.rb +31 -0
  42. data/lib/http_stub/server/stub/triggers.rb +25 -0
  43. data/lib/http_stub/server/stub/uri.rb +23 -0
  44. data/lib/http_stub/server/views/_scenario.haml +16 -0
  45. data/lib/http_stub/server/views/application.sass +4 -0
  46. data/lib/http_stub/server/views/scenarios.haml +3 -0
  47. data/lib/http_stub/version.rb +1 -1
  48. data/spec/acceptance/activator_spec.rb +81 -0
  49. data/spec/acceptance/configurer_initialization_spec.rb +158 -0
  50. data/spec/acceptance/scenario_spec.rb +91 -0
  51. data/spec/acceptance/stub_control_values_spec.rb +160 -0
  52. data/spec/acceptance/stub_spec.rb +290 -0
  53. data/spec/acceptance/stub_trigger_spec.rb +144 -0
  54. data/spec/lib/http_stub/configurer/{deprecated_dsl_spec.rb → dsl/deprecated_spec.rb} +21 -13
  55. data/spec/lib/http_stub/configurer/{server/dsl_spec.rb → dsl/sanctioned_spec.rb} +74 -97
  56. data/spec/lib/http_stub/configurer/dsl/scenario_activator_spec.rb +62 -0
  57. data/spec/lib/http_stub/configurer/dsl/scenario_builder_spec.rb +146 -0
  58. data/spec/lib/http_stub/configurer/dsl/stub_activator_builder_spec.rb +120 -0
  59. data/spec/lib/http_stub/configurer/dsl/stub_builder_producer_spec.rb +105 -0
  60. data/spec/lib/http_stub/configurer/{request → dsl}/stub_builder_spec.rb +33 -5
  61. data/spec/lib/http_stub/configurer/request/controllable_value_spec.rb +1 -1
  62. data/spec/lib/http_stub/configurer/request/http/factory_spec.rb +46 -5
  63. data/spec/lib/http_stub/configurer/request/omittable_spec.rb +1 -1
  64. data/spec/lib/http_stub/configurer/request/scenario_spec.rb +69 -0
  65. data/spec/lib/http_stub/configurer/request/stub_spec.rb +2 -2
  66. data/spec/lib/http_stub/configurer/server/facade_spec.rb +17 -17
  67. data/spec/lib/http_stub/configurer_spec.rb +1 -1
  68. data/spec/lib/http_stub/server/application_integration_spec.rb +23 -13
  69. data/spec/lib/http_stub/server/application_spec.rb +40 -38
  70. data/spec/lib/http_stub/server/daemon_spec.rb +2 -2
  71. data/spec/lib/http_stub/server/registry_spec.rb +66 -17
  72. data/spec/lib/http_stub/server/{request_parser_spec.rb → request_file_consolidator_spec.rb} +20 -33
  73. data/spec/lib/http_stub/server/response_pipeline_spec.rb +1 -1
  74. data/spec/lib/http_stub/server/scenario/activator_spec.rb +106 -0
  75. data/spec/lib/http_stub/server/scenario/controller_spec.rb +102 -0
  76. data/spec/lib/http_stub/server/scenario/instance_spec.rb +126 -0
  77. data/spec/lib/http_stub/server/scenario/request_parser_spec.rb +59 -0
  78. data/spec/lib/http_stub/server/scenario_spec.rb +24 -0
  79. data/spec/lib/http_stub/server/{stub_controller_spec.rb → stub/controller_spec.rb} +13 -17
  80. data/spec/lib/http_stub/server/{exact_value_matcher_spec.rb → stub/exact_value_matcher_spec.rb} +3 -3
  81. data/spec/lib/http_stub/server/{hash_with_string_value_matchers_spec.rb → stub/hash_with_string_value_matchers_spec.rb} +7 -5
  82. data/spec/lib/http_stub/server/{headers_spec.rb → stub/headers_spec.rb} +2 -2
  83. data/spec/lib/http_stub/server/stub/instance_spec.rb +201 -0
  84. data/spec/lib/http_stub/server/stub/method_spec.rb +95 -0
  85. data/spec/lib/http_stub/server/{omitted_value_matcher_spec.rb → stub/omitted_value_matcher_spec.rb} +3 -3
  86. data/spec/lib/http_stub/server/{regexp_value_matcher_spec.rb → stub/regexp_value_matcher_spec.rb} +3 -3
  87. data/spec/lib/http_stub/server/stub/registry_integration_spec.rb +56 -0
  88. data/spec/lib/http_stub/server/{stub_registry_spec.rb → stub/registry_spec.rb} +27 -14
  89. data/spec/lib/http_stub/server/{request_header_parser_spec.rb → stub/request_header_parser_spec.rb} +6 -6
  90. data/spec/lib/http_stub/server/stub/request_headers_spec.rb +68 -0
  91. data/spec/lib/http_stub/server/{stub_parameters_spec.rb → stub/request_parameters_spec.rb} +19 -17
  92. data/spec/lib/http_stub/server/stub/request_parser_spec.rb +56 -0
  93. data/spec/lib/http_stub/server/{stub_response → stub/response}/base_spec.rb +5 -5
  94. data/spec/lib/http_stub/server/{stub_response → stub/response}/file_spec.rb +3 -3
  95. data/spec/lib/http_stub/server/{stub_response → stub/response}/text_spec.rb +3 -3
  96. data/spec/lib/http_stub/server/{stub_response_spec.rb → stub/response_spec.rb} +8 -8
  97. data/spec/lib/http_stub/server/{string_value_matcher_spec.rb → stub/string_value_matcher_spec.rb} +25 -13
  98. data/spec/lib/http_stub/server/{stub_triggers_spec.rb → stub/triggers_spec.rb} +7 -9
  99. data/spec/lib/http_stub/server/stub/uri_spec.rb +43 -0
  100. data/spec/lib/http_stub/server/stub_spec.rb +10 -185
  101. data/spec/spec_helper.rb +10 -5
  102. data/spec/support/configurer_integration.rb +24 -0
  103. data/spec/support/scenario_fixture.rb +33 -0
  104. data/spec/support/stub_fixture.rb +1 -1
  105. metadata +126 -84
  106. data/lib/http_stub/configurer/deprecated_dsl.rb +0 -57
  107. data/lib/http_stub/configurer/request/stub_activator.rb +0 -26
  108. data/lib/http_stub/configurer/request/stub_activator_builder.rb +0 -25
  109. data/lib/http_stub/server/exact_value_matcher.rb +0 -13
  110. data/lib/http_stub/server/hash_with_string_value_matchers.rb +0 -20
  111. data/lib/http_stub/server/headers.rb +0 -17
  112. data/lib/http_stub/server/omitted_value_matcher.rb +0 -19
  113. data/lib/http_stub/server/regexp_value_matcher.rb +0 -14
  114. data/lib/http_stub/server/request_header_parser.rb +0 -17
  115. data/lib/http_stub/server/request_parser.rb +0 -29
  116. data/lib/http_stub/server/string_value_matcher.rb +0 -29
  117. data/lib/http_stub/server/stub_activator.rb +0 -30
  118. data/lib/http_stub/server/stub_activator_controller.rb +0 -34
  119. data/lib/http_stub/server/stub_controller.rb +0 -28
  120. data/lib/http_stub/server/stub_headers.rb +0 -24
  121. data/lib/http_stub/server/stub_parameters.rb +0 -21
  122. data/lib/http_stub/server/stub_registry.rb +0 -29
  123. data/lib/http_stub/server/stub_response.rb +0 -14
  124. data/lib/http_stub/server/stub_response/base.rb +0 -62
  125. data/lib/http_stub/server/stub_response/file.rb +0 -34
  126. data/lib/http_stub/server/stub_response/text.rb +0 -17
  127. data/lib/http_stub/server/stub_triggers.rb +0 -23
  128. data/lib/http_stub/server/stub_uri.rb +0 -21
  129. data/lib/http_stub/server/views/stub_activators.haml +0 -6
  130. data/spec/lib/http_stub/configurer/request/stub_activator_builder_spec.rb +0 -119
  131. data/spec/lib/http_stub/configurer/request/stub_activator_spec.rb +0 -45
  132. data/spec/lib/http_stub/configurer_integration_spec.rb +0 -837
  133. data/spec/lib/http_stub/server/stub_activator_controller_spec.rb +0 -95
  134. data/spec/lib/http_stub/server/stub_activator_spec.rb +0 -83
  135. data/spec/lib/http_stub/server/stub_headers_spec.rb +0 -68
  136. data/spec/lib/http_stub/server/stub_registry_integration_spec.rb +0 -62
  137. data/spec/lib/http_stub/server/stub_uri_spec.rb +0 -42
@@ -6,7 +6,7 @@ describe HttpStub::Server::Daemon do
6
6
 
7
7
  before(:example) { allow(server_daemon.logger).to receive(:info) }
8
8
 
9
- describe ".log_dir" do
9
+ describe "::log_dir" do
10
10
 
11
11
  before(:example) { @original_log_dir = HttpServerManager.log_dir }
12
12
 
@@ -20,7 +20,7 @@ describe HttpStub::Server::Daemon do
20
20
 
21
21
  end
22
22
 
23
- describe ".pid_dir" do
23
+ describe "::pid_dir" do
24
24
 
25
25
  before(:example) { @original_pid_dir = HttpServerManager.pid_dir }
26
26
 
@@ -1,7 +1,7 @@
1
1
  describe HttpStub::Server::Registry do
2
2
 
3
3
  let(:logger) { double("Logger").as_null_object }
4
- let(:request) { double("HttpRequest", logger: logger, inspect: "Request inspect result") }
4
+ let(:request) { instance_double(Rack::Request, logger: logger) }
5
5
 
6
6
  let(:registry) { HttpStub::Server::Registry.new("a_model") }
7
7
 
@@ -9,35 +9,84 @@ describe HttpStub::Server::Registry do
9
9
 
10
10
  let(:models) { (1..3).map { |i| double("HttpStub::Server::Model#{i}", :satisfies? => false) } }
11
11
 
12
- before(:example) { models.each { |model| registry.add(model, request) } }
12
+ before(:example) { registry.concat(models, request) }
13
13
 
14
14
  end
15
15
 
16
16
  describe "#add" do
17
17
 
18
+ let(:model_string_representation) { "some model string" }
19
+ let(:model) { double("HttpStub::Server::Model", to_s: model_string_representation) }
20
+
21
+ subject { registry.add(model, request) }
22
+
23
+ before(:example) { allow(logger).to receive(:info) }
24
+
18
25
  it "logs that the model has been registered" do
19
- model = double("HttpStub::Server::Model", to_s: "Model as String")
20
- expect(logger).to receive(:info).with(/Model as String/)
26
+ expect(logger).to receive(:info).with(/#{model_string_representation}/)
27
+
28
+ subject
29
+ end
30
+
31
+ it "adds the model" do
32
+ subject
21
33
 
22
- registry.add(model, request)
34
+ expect(registry.last).to eql(model)
23
35
  end
24
36
 
25
37
  end
26
38
 
27
- describe "#find_for" do
39
+ describe "#concat" do
40
+
41
+ let(:model_string_representations) { (1..3).map { |i| "model string ##{i}" } }
42
+ let(:models) do
43
+ model_string_representations.map do |string_representation|
44
+ double("HttpStub::Server::Model", to_s: string_representation)
45
+ end
46
+ end
47
+
48
+ subject { registry.concat(models, request) }
49
+
50
+ it "logs that the models have been registered" do
51
+ model_string_representations.each do |string_representation|
52
+ expect(logger).to receive(:info).with(/#{string_representation}/)
53
+ end
54
+
55
+ subject
56
+ end
57
+
58
+ it "adds the models" do
59
+ subject
60
+
61
+ expect(registry.all).to eql(models.reverse)
62
+ end
63
+
64
+ end
65
+
66
+ describe "#find" do
67
+
68
+ let(:criteria) { double("Criteria", inspect: "Criteria inspect result") }
69
+
70
+ subject { registry.find(criteria: criteria, request: request) }
28
71
 
29
72
  describe "when multiple models have been registered" do
30
73
 
31
74
  include_context "register multiple models"
32
75
 
33
- describe "and one registered model satisfies the request" do
76
+ it "determines if the models satisfy the provided criteria" do
77
+ models.each { |model| expect(model).to receive(:satisfies?).with(criteria).and_return(false) }
78
+
79
+ subject
80
+ end
81
+
82
+ describe "and one registered model satisfies the criteria" do
34
83
 
35
84
  let(:matching_model) { models[1] }
36
85
 
37
86
  before(:example) { allow(matching_model).to receive(:satisfies?).and_return(true) }
38
87
 
39
88
  it "returns the model" do
40
- expect(registry.find_for(request)).to eql(matching_model)
89
+ expect(subject).to eql(matching_model)
41
90
  end
42
91
 
43
92
  describe "and the registry is subsequently cleared" do
@@ -45,27 +94,27 @@ describe HttpStub::Server::Registry do
45
94
  before(:example) { registry.clear(request) }
46
95
 
47
96
  it "returns nil" do
48
- expect(registry.find_for(request)).to be_nil
97
+ expect(subject).to be_nil
49
98
  end
50
99
 
51
100
  end
52
101
 
53
102
  end
54
103
 
55
- describe "and multiple registered models satisfy the request" do
104
+ describe "and multiple registered models satisfy the criteria" do
56
105
 
57
106
  before(:example) { [0, 2].each { |i| allow(models[i]).to receive(:satisfies?).and_return(true) } }
58
107
 
59
108
  it "supports model overrides by returning the last model registered" do
60
- expect(registry.find_for(request)).to eql(models[2])
109
+ expect(subject).to eql(models[2])
61
110
  end
62
111
 
63
112
  end
64
113
 
65
- describe "and no registered models match the request" do
114
+ describe "and no registered models match the criteria" do
66
115
 
67
116
  it "returns nil" do
68
- expect(registry.find_for(request)).to be_nil
117
+ expect(subject).to be_nil
69
118
  end
70
119
 
71
120
  end
@@ -75,15 +124,15 @@ describe HttpStub::Server::Registry do
75
124
  describe "when no model has been registered" do
76
125
 
77
126
  it "returns nil" do
78
- expect(registry.find_for(request)).to be_nil
127
+ expect(subject).to be_nil
79
128
  end
80
129
 
81
130
  end
82
131
 
83
- it "it should log model discovery diagnostics that includes the complete details of the request" do
84
- expect(logger).to receive(:info).with(/Request inspect result/)
132
+ it "it should log model discovery diagnostics that includes the complete details of the criteria" do
133
+ expect(logger).to receive(:info).with(/Criteria inspect result/)
85
134
 
86
- registry.find_for(request)
135
+ subject
87
136
  end
88
137
 
89
138
  end
@@ -1,42 +1,19 @@
1
- describe HttpStub::Server::RequestParser do
1
+ describe HttpStub::Server::Stub::PayloadFileConsolidator do
2
2
 
3
- let(:request_parser) { HttpStub::Server::RequestParser }
3
+ let(:consolidator) { described_class }
4
4
 
5
- describe "::parse" do
5
+ describe "::consolidate!" 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(:params) { {} }
8
+ let(:request) { instance_double(Rack::Request, params: params) }
10
9
 
11
- subject { request_parser.parse(request) }
12
-
13
- context "when the request contains a payload parameter" do
14
-
15
- let(:payload) { HttpStub::StubFixture.new.server_payload }
16
- let(:params) { { "payload" => payload.to_json } }
17
-
18
- it "returns the payload" do
19
- expect(subject).to eql(payload)
20
- end
21
-
22
- end
23
-
24
- context "when the request body contains the payload (for API backwards compatibility)" do
25
-
26
- let(:body_hash) { HttpStub::StubFixture.new.server_payload }
27
-
28
- it "returns the request body" do
29
- expect(subject).to eql(body_hash)
30
- end
31
-
32
- end
10
+ subject { consolidator.consolidate!(payload, request) }
33
11
 
34
12
  context "when the payload contains a response file and has a corresponding file parameter" do
35
13
 
36
14
  let(:payload_fixture) { HttpStub::StubFixture.new.with_file_response! }
37
15
  let(:payload) { payload_fixture.server_payload }
38
- let(:file_params) { { "response_file_#{payload_fixture.id}" => payload_fixture.file_parameter } }
39
- let(:params) { { "payload" => payload.to_json }.merge(file_params) }
16
+ let(:params) { { "response_file_#{payload_fixture.id}" => payload_fixture.file_parameter } }
40
17
 
41
18
  it "returns the payload with a response body that contains the file parameter value" do
42
19
  expected_payload = payload.clone.tap { |hash| hash["response"]["body"] = payload_fixture.file_parameter }
@@ -54,7 +31,6 @@ describe HttpStub::Server::RequestParser do
54
31
  context "and the trigger payloads contain response text" do
55
32
 
56
33
  let(:trigger_fixtures) { (1..3).map { HttpStub::StubFixture.new.with_text_response! } }
57
- let(:params) { { "payload" => payload.to_json } }
58
34
 
59
35
  it "returns the payload unchanged" do
60
36
  expect(subject).to eql(payload)
@@ -65,12 +41,11 @@ describe HttpStub::Server::RequestParser do
65
41
  context "and the trigger payloads contain a response file with corresponding file parameters" do
66
42
 
67
43
  let(:trigger_fixtures) { (1..3).map { HttpStub::StubFixture.new.with_file_response! } }
68
- let(:file_params) do
44
+ let(:params) do
69
45
  trigger_fixtures.reduce({}) do |result, fixture|
70
46
  result.merge("response_file_#{fixture.id}" => fixture.file_parameter)
71
47
  end
72
48
  end
73
- let(:params) { { "payload" => payload.to_json }.merge(file_params) }
74
49
 
75
50
  it "returns the payload with the trigger response bodies replaced by the files" do
76
51
  expected_payload = payload_fixture.server_payload
@@ -85,6 +60,18 @@ describe HttpStub::Server::RequestParser do
85
60
 
86
61
  end
87
62
 
63
+ context "when the payload does not contain a response file and has no triggers" do
64
+
65
+ let(:payload) { HttpStub::StubFixture.new.server_payload }
66
+
67
+ it "returns an unchanged payload" do
68
+ original_payload = payload.clone
69
+
70
+ expect(subject).to eql(original_payload)
71
+ end
72
+
73
+ end
74
+
88
75
  end
89
76
 
90
77
  end
@@ -6,7 +6,7 @@ describe HttpStub::Server::ResponsePipeline do
6
6
 
7
7
  describe "#process" do
8
8
 
9
- let(:response) { double(HttpStub::Server::StubResponse::Base, delay_in_seconds: 5, serve_on: nil) }
9
+ let(:response) { double(HttpStub::Server::Stub::Response::Base, delay_in_seconds: 5, serve_on: nil) }
10
10
 
11
11
  before(:example) { allow(response_pipeline).to receive(:sleep) }
12
12
 
@@ -0,0 +1,106 @@
1
+ describe HttpStub::Server::Scenario::Activator do
2
+
3
+ let(:request) { instance_double(Rack::Request) }
4
+ let(:stubs) { (1..3).map { instance_double(HttpStub::Server::Stub::Instance) } }
5
+ let(:triggered_scenario_names) { [] }
6
+ let(:scenario) do
7
+ instance_double(
8
+ HttpStub::Server::Scenario::Instance, stubs: stubs, triggered_scenario_names: triggered_scenario_names
9
+ )
10
+ end
11
+ let(:scenario_registry) { instance_double(HttpStub::Server::Registry).as_null_object }
12
+ let(:stub_registry) { instance_double(HttpStub::Server::Stub::Registry).as_null_object }
13
+ let(:activator_class) { HttpStub::Server::Scenario::Activator }
14
+
15
+ let(:activator) { activator_class.new(scenario_registry, stub_registry) }
16
+
17
+ describe "#activate" do
18
+
19
+ subject { activator.activate(scenario, request) }
20
+
21
+ it "adds the scenario's stubs to the stub registry with the provided request" do
22
+ expect(stub_registry).to receive(:concat).with(stubs, request)
23
+
24
+ subject
25
+ end
26
+
27
+ context "when the scenario contains triggered scenarios" do
28
+
29
+ let(:triggered_scenario_names) { (1..3).map { |i| "triggered_scenario_name/#{i}" } }
30
+ let(:triggered_scenarios) do
31
+ triggered_scenario_names.map do
32
+ instance_double(HttpStub::Server::Scenario::Instance, stubs: [], triggered_scenario_names: [])
33
+ end
34
+ end
35
+
36
+ before(:each) do
37
+ triggered_scenario_names.zip(triggered_scenarios).each do |triggered_scenario_name, triggered_scenario|
38
+ allow(scenario_registry).to(
39
+ receive(:find).with(hash_including(criteria: triggered_scenario_name)).and_return(triggered_scenario)
40
+ )
41
+ end
42
+ end
43
+
44
+ it "finds each triggered scenario in the scenario registry" do
45
+ triggered_scenario_names.zip(triggered_scenarios).each do |triggered_scenario_name, triggered_scenario|
46
+ expect(scenario_registry).to(
47
+ receive(:find).with(criteria: triggered_scenario_name, request: request).and_return(triggered_scenario)
48
+ )
49
+ end
50
+
51
+ subject
52
+ end
53
+
54
+ context "when the triggered scenario's are found" do
55
+
56
+ class HttpStub::Server::Scenario::ActivatorRetainingActivateArgs < HttpStub::Server::Scenario::Activator
57
+
58
+ attr_reader :activate_args
59
+
60
+ def initialize(scenario_registry, stub_registry)
61
+ super
62
+ @activate_args = []
63
+ end
64
+
65
+ def activate(scenario, request)
66
+ @activate_args << [ scenario, request ]
67
+ super
68
+ end
69
+
70
+ end
71
+
72
+ let(:activator_class) { HttpStub::Server::Scenario::ActivatorRetainingActivateArgs }
73
+
74
+ it "activates the scenario's" do
75
+ expected_activate_args = triggered_scenarios.reduce([ [ scenario, request ] ]) do |result, triggered_scenario|
76
+ result << [ triggered_scenario, request ]
77
+ end
78
+
79
+ subject
80
+
81
+ expect(activator.activate_args).to eql(expected_activate_args)
82
+ end
83
+
84
+ end
85
+
86
+ context "when a triggered scenario is not found" do
87
+
88
+ let(:scenario_name_not_found) { triggered_scenario_names[1] }
89
+
90
+ before(:example) do
91
+ allow(scenario_registry).to(
92
+ receive(:find).with(hash_including(criteria: scenario_name_not_found)).and_return(nil)
93
+ )
94
+ end
95
+
96
+ it "raises an error indicating the triggered scenario is not found" do
97
+ expect { subject }.to raise_error("Scenario not found with name '#{scenario_name_not_found}'")
98
+ end
99
+
100
+ end
101
+
102
+ end
103
+
104
+ end
105
+
106
+ end
@@ -0,0 +1,102 @@
1
+ describe HttpStub::Server::Scenario::Controller do
2
+
3
+ let(:request_path_info) { "/some/request/path" }
4
+ let(:request) { instance_double(Rack::Request, path_info: request_path_info) }
5
+ let(:payload) { HttpStub::ScenarioFixture.new.server_payload }
6
+ let(:stubs) { (1..3).map { instance_double(HttpStub::Server::Stub::Instance) } }
7
+ let(:scenario) { instance_double(HttpStub::Server::Scenario::Instance, stubs: stubs) }
8
+ let(:scenario_registry) { instance_double(HttpStub::Server::Registry).as_null_object }
9
+ let(:stub_registry) { instance_double(HttpStub::Server::Stub::Registry).as_null_object }
10
+ let(:activator) { instance_double(HttpStub::Server::Scenario::Activator).as_null_object }
11
+
12
+ let(:controller) { HttpStub::Server::Scenario::Controller.new(scenario_registry, stub_registry) }
13
+
14
+ before(:example) do
15
+ allow(HttpStub::Server::Scenario::Activator).to receive(:new).and_return(activator)
16
+ allow(HttpStub::Server::Scenario::RequestParser).to receive(:parse).and_return(payload)
17
+ allow(HttpStub::Server::Scenario).to receive(:create).and_return(scenario)
18
+ end
19
+
20
+ describe "#register" do
21
+
22
+ subject { controller.register(request) }
23
+
24
+ it "parses the payload from the request" do
25
+ expect(HttpStub::Server::Scenario::RequestParser).to receive(:parse).with(request).and_return(payload)
26
+
27
+ subject
28
+ end
29
+
30
+ it "creates a scenario with the parsed payload" do
31
+ expect(HttpStub::Server::Scenario).to receive(:create).with(payload).and_return(scenario)
32
+
33
+ subject
34
+ end
35
+
36
+ it "adds the created scenario to the scenario registry with the provided request" do
37
+ expect(scenario_registry).to receive(:add).with(scenario, request)
38
+
39
+ subject
40
+ end
41
+
42
+ it "returns a success response" do
43
+ expect(subject).to eql(HttpStub::Server::Response::SUCCESS)
44
+ end
45
+
46
+ end
47
+
48
+ describe "#activate" do
49
+
50
+ subject { controller.activate(request) }
51
+
52
+ it "finds a scenario whose name matches the request path omitting the '/' prefix" do
53
+ expect(scenario_registry).to receive(:find).with(criteria: "some/request/path", request: request)
54
+
55
+ subject
56
+ end
57
+
58
+ describe "when a scenario is found that satisfies the request" do
59
+
60
+ before(:example) { allow(scenario_registry).to receive(:find).and_return(scenario) }
61
+
62
+ it "activates the scenario via the activator" do
63
+ expect(activator).to receive(:activate).with(scenario, request)
64
+
65
+ subject
66
+ end
67
+
68
+ it "returns a success response" do
69
+ expect(subject).to eql(HttpStub::Server::Response::SUCCESS)
70
+ end
71
+
72
+ end
73
+
74
+ describe "when no stub activator is activated by the request" do
75
+
76
+ before(:example) { allow(scenario_registry).to receive(:find).and_return(nil) }
77
+
78
+ it "does not add a stub to the registry" do
79
+ expect(stub_registry).not_to receive(:concat)
80
+
81
+ subject
82
+ end
83
+
84
+ it "returns an empty response" do
85
+ expect(subject).to eql(HttpStub::Server::Response::EMPTY)
86
+ end
87
+
88
+ end
89
+
90
+ end
91
+
92
+ describe "#clear" do
93
+
94
+ it "clears the scenario registry" do
95
+ expect(scenario_registry).to receive(:clear).with(request)
96
+
97
+ controller.clear(request)
98
+ end
99
+
100
+ end
101
+
102
+ end