http_stub 0.25.1 → 0.26.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (187) hide show
  1. checksums.yaml +4 -4
  2. data/lib/http_stub.rb +32 -9
  3. data/lib/http_stub/configurer.rb +2 -10
  4. data/lib/http_stub/configurer/dsl/scenario_builder.rb +13 -11
  5. data/lib/http_stub/configurer/dsl/server.rb +32 -43
  6. data/lib/http_stub/configurer/dsl/server_endpoint_template.rb +23 -0
  7. data/lib/http_stub/configurer/dsl/session.rb +55 -0
  8. data/lib/http_stub/configurer/dsl/session_endpoint_template.rb +22 -0
  9. data/lib/http_stub/configurer/dsl/session_factory.rb +34 -0
  10. data/lib/http_stub/configurer/dsl/stub_builder.rb +18 -8
  11. data/lib/http_stub/configurer/dsl/stub_builder_template.rb +26 -0
  12. data/lib/http_stub/configurer/request/http/basic.rb +9 -3
  13. data/lib/http_stub/configurer/request/http/factory.rb +7 -16
  14. data/lib/http_stub/configurer/request/http/multipart.rb +5 -7
  15. data/lib/http_stub/configurer/request/stub.rb +3 -3
  16. data/lib/http_stub/configurer/request/triggers.rb +25 -0
  17. data/lib/http_stub/configurer/server/command_processor.rb +8 -10
  18. data/lib/http_stub/configurer/server/configuration.rb +33 -0
  19. data/lib/http_stub/configurer/server/facade.rb +21 -43
  20. data/lib/http_stub/configurer/server/request_processor.rb +2 -2
  21. data/lib/http_stub/configurer/server/session_facade.rb +64 -0
  22. data/lib/http_stub/extensions/core/uri.rb +28 -0
  23. data/lib/http_stub/extensions/rack/handler.rb +1 -0
  24. data/lib/http_stub/extensions/sinatra/namespace.rb +9 -0
  25. data/lib/http_stub/rake/server_tasks.rb +1 -4
  26. data/lib/http_stub/server/application/application.rb +17 -98
  27. data/lib/http_stub/server/application/configuration.rb +38 -0
  28. data/lib/http_stub/server/application/cross_origin_support.rb +7 -5
  29. data/lib/http_stub/server/application/request_support.rb +38 -0
  30. data/lib/http_stub/server/application/response_support.rb +19 -0
  31. data/lib/http_stub/server/application/routes/memory.rb +37 -0
  32. data/lib/http_stub/server/application/routes/resource.rb +23 -0
  33. data/lib/http_stub/server/application/routes/scenario.rb +52 -0
  34. data/lib/http_stub/server/application/routes/session.rb +65 -0
  35. data/lib/http_stub/server/application/routes/stub.rb +75 -0
  36. data/lib/http_stub/server/application/session_uri_support.rb +15 -0
  37. data/lib/http_stub/server/memory/controller.rb +25 -0
  38. data/lib/http_stub/server/memory/memory.rb +32 -0
  39. data/lib/http_stub/server/registry.rb +25 -8
  40. data/lib/http_stub/server/request/factory.rb +23 -0
  41. data/lib/http_stub/server/request/parameters.rb +2 -2
  42. data/lib/http_stub/server/request/request.rb +6 -11
  43. data/lib/http_stub/server/request/sinatra_request.rb +26 -0
  44. data/lib/http_stub/server/scenario/controller.rb +16 -11
  45. data/lib/http_stub/server/scenario/not_found_error.rb +15 -0
  46. data/lib/http_stub/server/session.rb +14 -0
  47. data/lib/http_stub/server/session/configuration.rb +32 -0
  48. data/lib/http_stub/server/session/controller.rb +40 -0
  49. data/lib/http_stub/server/session/empty.rb +15 -0
  50. data/lib/http_stub/server/session/identifier_strategy.rb +24 -0
  51. data/lib/http_stub/server/session/registry.rb +38 -0
  52. data/lib/http_stub/server/session/session.rb +80 -0
  53. data/lib/http_stub/server/stub/controller.rb +18 -12
  54. data/lib/http_stub/server/stub/empty.rb +1 -1
  55. data/lib/http_stub/server/stub/match/controller.rb +9 -5
  56. data/lib/http_stub/server/stub/payload/response_body_modifier.rb +3 -1
  57. data/lib/http_stub/server/stub/registry.rb +6 -13
  58. data/lib/http_stub/server/stub/triggers.rb +11 -9
  59. data/lib/http_stub/server/views/_home.haml +2 -0
  60. data/lib/http_stub/server/views/_post_link_function.haml +26 -0
  61. data/lib/http_stub/server/views/_session.haml +16 -0
  62. data/lib/http_stub/server/views/_stub.haml +8 -2
  63. data/lib/http_stub/server/views/_stubs.haml +4 -0
  64. data/lib/http_stub/server/views/application.sass +4 -1
  65. data/lib/http_stub/server/views/layout.haml +1 -0
  66. data/lib/http_stub/server/views/scenario.haml +4 -3
  67. data/lib/http_stub/server/views/scenarios.haml +7 -6
  68. data/lib/http_stub/server/views/session.haml +3 -0
  69. data/lib/http_stub/server/views/sessions.haml +6 -0
  70. data/lib/http_stub/server/views/stubs.haml +1 -4
  71. data/lib/http_stub/server/views/transactional_session.haml +2 -0
  72. data/lib/http_stub/version.rb +1 -1
  73. data/spec/acceptance/configurer_initialization_spec.rb +40 -51
  74. data/spec/acceptance/configurer_part_spec.rb +5 -7
  75. data/spec/acceptance/cross_origin_support_spec.rb +7 -13
  76. data/spec/acceptance/endpoint_template_spec.rb +2 -4
  77. data/spec/acceptance/request_reference_spec.rb +1 -3
  78. data/spec/acceptance/scenario_spec.rb +7 -9
  79. data/spec/acceptance/server_defaults_spec.rb +2 -4
  80. data/spec/acceptance/server_memory_spec.rb +20 -0
  81. data/spec/acceptance/session_spec.rb +125 -0
  82. data/spec/acceptance/stub_body_request_matching_spec.rb +2 -4
  83. data/spec/acceptance/stub_control_values_spec.rb +19 -18
  84. data/spec/acceptance/stub_match_last_spec.rb +0 -4
  85. data/spec/acceptance/stub_match_list_spec.rb +0 -4
  86. data/spec/acceptance/stub_miss_list_spec.rb +0 -4
  87. data/spec/acceptance/stub_spec.rb +11 -11
  88. data/spec/acceptance/stub_trigger_spec.rb +60 -13
  89. data/spec/lib/http_stub/configurer/dsl/scenario_builder_spec.rb +83 -30
  90. data/spec/lib/http_stub/configurer/dsl/server_endpoint_template_spec.rb +167 -0
  91. data/spec/lib/http_stub/configurer/dsl/server_spec.rb +306 -176
  92. data/spec/lib/http_stub/configurer/dsl/session_endpoint_template_spec.rb +150 -0
  93. data/spec/lib/http_stub/configurer/dsl/session_factory_spec.rb +97 -0
  94. data/spec/lib/http_stub/configurer/dsl/session_spec.rb +222 -0
  95. data/spec/lib/http_stub/configurer/dsl/stub_builder_spec.rb +209 -48
  96. data/spec/lib/http_stub/configurer/dsl/stub_builder_template_spec.rb +203 -0
  97. data/spec/lib/http_stub/configurer/request/http/basic_spec.rb +80 -4
  98. data/spec/lib/http_stub/configurer/request/http/factory_spec.rb +115 -56
  99. data/spec/lib/http_stub/configurer/request/http/multipart_spec.rb +39 -6
  100. data/spec/lib/http_stub/configurer/request/stub_spec.rb +50 -108
  101. data/spec/lib/http_stub/configurer/request/triggers_spec.rb +101 -0
  102. data/spec/lib/http_stub/configurer/server/command_processor_integration_spec.rb +11 -10
  103. data/spec/lib/http_stub/configurer/server/configuration_spec.rb +139 -0
  104. data/spec/lib/http_stub/configurer/server/facade_spec.rb +65 -142
  105. data/spec/lib/http_stub/configurer/server/request_processor_spec.rb +7 -6
  106. data/spec/lib/http_stub/configurer/server/session_facade_spec.rb +336 -0
  107. data/spec/lib/http_stub/configurer_spec.rb +105 -0
  108. data/spec/lib/http_stub/extensions/core/uri_spec.rb +75 -0
  109. data/spec/lib/http_stub/extensions/rack/handler_spec.rb +1 -1
  110. data/spec/lib/http_stub/extensions/sinatra/namespace_spec.rb +36 -0
  111. data/spec/lib/http_stub/rake/server_tasks_smoke_spec.rb +4 -3
  112. data/spec/lib/http_stub/rake/server_tasks_spec.rb +47 -10
  113. data/spec/lib/http_stub/server/application/application_spec.rb +44 -301
  114. data/spec/lib/http_stub/server/application/configuration_spec.rb +59 -0
  115. data/spec/lib/http_stub/server/application/cross_origin_support_spec.rb +23 -20
  116. data/spec/lib/http_stub/server/application/request_support_integration_spec.rb +93 -0
  117. data/spec/lib/http_stub/server/application/request_support_spec.rb +61 -0
  118. data/spec/lib/http_stub/server/application/response_support_spec.rb +30 -0
  119. data/spec/lib/http_stub/server/application/routes/memory_spec.rb +41 -0
  120. data/spec/lib/http_stub/server/application/routes/resource_spec.rb +16 -0
  121. data/spec/lib/http_stub/server/application/routes/scenario_spec.rb +117 -0
  122. data/spec/lib/http_stub/server/application/routes/session_integration_spec.rb +46 -0
  123. data/spec/lib/http_stub/server/application/routes/session_spec.rb +186 -0
  124. data/spec/lib/http_stub/server/application/{application_integration_spec.rb → routes/stub_integration_spec.rb} +16 -38
  125. data/spec/lib/http_stub/server/application/routes/stub_spec.rb +202 -0
  126. data/spec/lib/http_stub/server/application/session_uri_support_spec.rb +48 -0
  127. data/spec/lib/http_stub/server/application/text_formatting_support_spec.rb +3 -3
  128. data/spec/lib/http_stub/server/memory/controller_spec.rb +47 -0
  129. data/spec/lib/http_stub/server/memory/memory_spec.rb +122 -0
  130. data/spec/lib/http_stub/server/registry_spec.rb +153 -34
  131. data/spec/lib/http_stub/server/request/factory_spec.rb +74 -0
  132. data/spec/lib/http_stub/server/request/parameters_spec.rb +5 -8
  133. data/spec/lib/http_stub/server/request/request_spec.rb +52 -52
  134. data/spec/lib/http_stub/server/request/sinatra_request_spec.rb +132 -0
  135. data/spec/lib/http_stub/server/scenario/controller_spec.rb +121 -35
  136. data/spec/lib/http_stub/server/scenario/not_found_error_spec.rb +25 -0
  137. data/spec/lib/http_stub/server/session/configuration_spec.rb +94 -0
  138. data/spec/lib/http_stub/server/session/controller_spec.rb +106 -0
  139. data/spec/lib/http_stub/server/session/empty_spec.rb +13 -0
  140. data/spec/lib/http_stub/server/session/identifier_strategy_spec.rb +126 -0
  141. data/spec/lib/http_stub/server/session/registry_spec.rb +214 -0
  142. data/spec/lib/http_stub/server/session/session_spec.rb +444 -0
  143. data/spec/lib/http_stub/server/stub/controller_spec.rb +89 -37
  144. data/spec/lib/http_stub/server/stub/empty_spec.rb +2 -2
  145. data/spec/lib/http_stub/server/stub/match/controller_spec.rb +59 -16
  146. data/spec/lib/http_stub/server/stub/payload/response_body_modifier_spec.rb +7 -8
  147. data/spec/lib/http_stub/server/stub/registry_integration_spec.rb +42 -15
  148. data/spec/lib/http_stub/server/stub/registry_spec.rb +38 -59
  149. data/spec/lib/http_stub/server/stub/triggers_spec.rb +93 -34
  150. data/spec/spec_helper.rb +13 -1
  151. data/spec/support/contain_file.rb +23 -0
  152. data/spec/support/cross_origin_server/application.rb +1 -1
  153. data/spec/support/cross_origin_server/index_page.rb +2 -2
  154. data/spec/support/cross_origin_server/integration.rb +3 -2
  155. data/spec/support/cross_origin_server/public/index.html +2 -1
  156. data/spec/support/http_stub/configurer_integration.rb +12 -14
  157. data/spec/support/http_stub/server/application/http_stub_rack_application_test.rb +47 -0
  158. data/spec/support/http_stub/server/driver.rb +56 -0
  159. data/spec/support/http_stub/server/memory_fixture.rb +17 -0
  160. data/spec/support/http_stub/server/request_fixture.rb +7 -6
  161. data/spec/support/http_stub/server/scenario_fixture.rb +13 -0
  162. data/spec/support/http_stub/server/session_fixture.rb +27 -0
  163. data/spec/support/http_stub/server_integration.rb +12 -17
  164. data/spec/support/http_stub/stub_fixture.rb +19 -6
  165. data/spec/support/include_in_json.rb +4 -4
  166. data/spec/support/rack/rack_application_test.rb +9 -0
  167. data/spec/support/rack/request_fixture.rb +13 -0
  168. data/spec/support/surpressed_output.rb +10 -0
  169. metadata +149 -36
  170. data/lib/http_stub/configurer/dsl/deprecated.rb +0 -60
  171. data/lib/http_stub/configurer/dsl/endpoint_template.rb +0 -38
  172. data/lib/http_stub/configurer/dsl/scenario_activator.rb +0 -15
  173. data/lib/http_stub/configurer/dsl/stub_activator_builder.rb +0 -24
  174. data/lib/http_stub/configurer/dsl/stub_builder_producer.rb +0 -21
  175. data/lib/http_stub/server/request.rb +0 -12
  176. data/lib/http_stub/server/scenario/activator.rb +0 -25
  177. data/lib/http_stub/server/views/_activate_scenario.haml +0 -23
  178. data/lib/http_stub/server/views/index.haml +0 -8
  179. data/spec/acceptance/activator_spec.rb +0 -81
  180. data/spec/lib/http_stub/configurer/dsl/deprecated_spec.rb +0 -240
  181. data/spec/lib/http_stub/configurer/dsl/endpoint_template_spec.rb +0 -255
  182. data/spec/lib/http_stub/configurer/dsl/scenario_activator_spec.rb +0 -62
  183. data/spec/lib/http_stub/configurer/dsl/stub_activator_builder_spec.rb +0 -120
  184. data/spec/lib/http_stub/configurer/dsl/stub_builder_producer_spec.rb +0 -85
  185. data/spec/lib/http_stub/server/request_spec.rb +0 -24
  186. data/spec/lib/http_stub/server/scenario/activator_spec.rb +0 -111
  187. data/spec/support/http_stub/server/scenario/scenario_fixture.rb +0 -15
@@ -1,15 +1,91 @@
1
1
  describe HttpStub::Configurer::Request::Http::Basic do
2
2
 
3
- let(:http_request) { instance_double(Net::HTTPRequest) }
3
+ let(:request_method) { :get }
4
+ let(:path) { "some/path" }
5
+ let(:args) { { method: request_method, path: path } }
4
6
 
5
- let(:basic_request) { HttpStub::Configurer::Request::Http::Basic.new(http_request) }
7
+ let(:basic_request) { described_class.new(args) }
6
8
 
7
9
  describe "#to_http_request" do
8
10
 
9
- it "returns the provided HTTP request" do
10
- expect(basic_request.to_http_request).to eql(http_request)
11
+ subject { basic_request.to_http_request }
12
+
13
+ it "returns a HTTP request of the provided type" do
14
+ expect(subject).to be_a(Net::HTTP::Get)
15
+ end
16
+
17
+ it "returns a request with the provided path prefixed with http_stub scoping" do
18
+ expect(subject.path).to eql("/http_stub/#{path}")
19
+ end
20
+
21
+ context "when headers are provided" do
22
+
23
+ let(:args) { { method: request_method, path: path, headers: headers } }
24
+
25
+ context "whose names are symbols" do
26
+
27
+ let(:headers) { (1..3).each_with_object({}) { |i, result| result["header#{i}".to_sym] = "value#{i}" } }
28
+
29
+ it "returns a request with the provided headers with names converted to strings" do
30
+ expect(request_headers).to include(headers.stringify_keys)
31
+ end
32
+
33
+ end
34
+
35
+ context "whose names are strings" do
36
+
37
+ let(:headers) { (1..3).each_with_object({}) { |i, result| result["header#{i}"] = "value#{i}" } }
38
+
39
+ it "returns a request with the provided headers" do
40
+ expect(request_headers).to include(headers)
41
+ end
42
+
43
+ end
44
+
45
+ end
46
+
47
+ context "when headers are not provided" do
48
+
49
+ it "returns a request with default headers" do
50
+ expect(request_headers).to_not be_empty
51
+ end
52
+
53
+ end
54
+
55
+ context "when parameters are provided" do
56
+
57
+ let(:parameters) { (1..3).each_with_object({}) { |i, result| result["parameter#{i}"] = "value#{i}" } }
58
+
59
+ let(:args) { { method: request_method, path: path, parameters: parameters } }
60
+
61
+ it "returns a request with the provided parameters" do
62
+ expect(request_parameters).to eql(parameters)
63
+ end
64
+
65
+ end
66
+
67
+ context "when parameters are not provided" do
68
+
69
+ let(:args) { { method: request_method, path: path } }
70
+
71
+ it "returns a request without parameters" do
72
+ expect(subject.body).to eql(nil)
73
+ end
74
+
75
+ end
76
+
77
+ it "returns a request with the provided path prefixed with http_stub scoping" do
78
+ expect(subject.path).to eql("/http_stub/#{path}")
11
79
  end
12
80
 
13
81
  end
14
82
 
83
+ def request_headers
84
+ subject.to_hash.each_with_object({}) { |(key, value), result| result[key] = value.first }
85
+ end
86
+
87
+ def request_parameters
88
+ URI.decode_www_form(subject.body).to_h
89
+ end
90
+
15
91
  end
@@ -2,21 +2,55 @@ describe HttpStub::Configurer::Request::Http::Factory do
2
2
 
3
3
  describe "::multipart" do
4
4
 
5
- let(:model) { double("HttpStub::Configurer::Request::SomeModel") }
5
+ let(:model) { double("HttpStub::Configurer::Request::SomeModel") }
6
+ let(:path) { "some/model/path" }
7
+
6
8
  let(:multipart_request) { instance_double(HttpStub::Configurer::Request::Http::Multipart) }
7
9
 
8
- subject { HttpStub::Configurer::Request::Http::Factory.multipart(model) }
10
+ subject { described_class.multipart(path, model) }
9
11
 
10
12
  before(:example) do
11
13
  allow(HttpStub::Configurer::Request::Http::Multipart).to receive(:new).and_return(multipart_request)
12
14
  end
13
15
 
16
+ it "creates a multipart request with the provided path" do
17
+ expect(HttpStub::Configurer::Request::Http::Multipart).to receive(:new).with(hash_including(path: path))
18
+
19
+ subject
20
+ end
21
+
14
22
  it "creates a multipart request with the provided model" do
15
- expect(HttpStub::Configurer::Request::Http::Multipart).to receive(:new).with(model)
23
+ expect(HttpStub::Configurer::Request::Http::Multipart).to receive(:new).with(hash_including(model: model))
16
24
 
17
25
  subject
18
26
  end
19
27
 
28
+ context "when headers are provided" do
29
+
30
+ let(:headers) { (1..3).each_with_object({}) { |i, result| result["header#{i}"] = "value#{i}" } }
31
+
32
+ subject { described_class.multipart(path, model, headers) }
33
+
34
+ it "creates a multipart request with the provided headers" do
35
+ expect(HttpStub::Configurer::Request::Http::Multipart).to receive(:new).with(hash_including(headers: headers))
36
+
37
+ subject
38
+ end
39
+
40
+ end
41
+
42
+ context "when no headers are provided" do
43
+
44
+ subject { described_class.multipart(path, model) }
45
+
46
+ it "creates a multipart request with the empty headers" do
47
+ expect(HttpStub::Configurer::Request::Http::Multipart).to receive(:new).with(hash_including(headers: {}))
48
+
49
+ subject
50
+ end
51
+
52
+ end
53
+
20
54
  it "returns the created request" do
21
55
  expect(subject).to eql(multipart_request)
22
56
  end
@@ -25,47 +59,52 @@ describe HttpStub::Configurer::Request::Http::Factory do
25
59
 
26
60
  describe "::get" do
27
61
 
28
- let(:path) { "/some/get/path" }
29
- let(:get_request) { instance_double(Net::HTTP::Get) }
62
+ let(:path) { "/some/get/path" }
63
+
30
64
  let(:basic_request) { instance_double(HttpStub::Configurer::Request::Http::Basic) }
31
65
 
32
- subject { HttpStub::Configurer::Request::Http::Factory.get(path) }
66
+ subject { described_class.get(path) }
33
67
 
34
- before(:example) do
35
- allow(Net::HTTP::Get).to receive(:new).and_return(get_request)
36
- allow(HttpStub::Configurer::Request::Http::Basic).to receive(:new).and_return(basic_request)
68
+ before(:example) { allow(HttpStub::Configurer::Request::Http::Basic).to receive(:new).and_return(basic_request) }
69
+
70
+ it "creates a basic GET request" do
71
+ expect(HttpStub::Configurer::Request::Http::Basic).to receive(:new).with(hash_including(method: :get))
72
+
73
+ subject
74
+ end
75
+
76
+ it "creates a basic request with the provided path" do
77
+ expect(HttpStub::Configurer::Request::Http::Basic).to receive(:new).with(hash_including(path: path))
78
+
79
+ subject
37
80
  end
38
81
 
39
- context "when the path is absolute" do
82
+ context "when headers are provided" do
40
83
 
41
- let(:path) { "/some/absolute/get/path" }
84
+ let(:headers) { (1..3).each_with_object({}) { |i, result| result["header#{i}"] = "value#{i}" } }
42
85
 
43
- it "creates a GET request with the provided path" do
44
- expect(Net::HTTP::Get).to receive(:new).with(path)
86
+ subject { described_class.get(path, headers) }
87
+
88
+ it "creates a basic request with the provided headers" do
89
+ expect(HttpStub::Configurer::Request::Http::Basic).to receive(:new).with(hash_including(headers: headers))
45
90
 
46
91
  subject
47
92
  end
48
93
 
49
94
  end
50
95
 
51
- context "when the path is relative" do
96
+ context "when no headers are provided" do
52
97
 
53
- let(:path) { "some/relative/get/path" }
98
+ subject { described_class.get(path) }
54
99
 
55
- it "creates a GET request with the path prefixed by '/'" do
56
- expect(Net::HTTP::Get).to receive(:new).with("/#{path}")
100
+ it "creates a basic request with empty headers" do
101
+ expect(HttpStub::Configurer::Request::Http::Basic).to receive(:new).with(hash_including(headers: {}))
57
102
 
58
103
  subject
59
104
  end
60
105
 
61
106
  end
62
107
 
63
- it "creates a basic request wrapping the GET request" do
64
- expect(HttpStub::Configurer::Request::Http::Basic).to receive(:new).with(get_request)
65
-
66
- subject
67
- end
68
-
69
108
  it "returns the created basic request" do
70
109
  expect(subject).to eql(basic_request)
71
110
  end
@@ -74,31 +113,34 @@ describe HttpStub::Configurer::Request::Http::Factory do
74
113
 
75
114
  describe "::post" do
76
115
 
77
- let(:path) { "/some/post/path" }
78
- let(:post_request) { instance_double(Net::HTTP::Post).as_null_object }
116
+ let(:path) { "/some/post/path" }
117
+
79
118
  let(:basic_request) { instance_double(HttpStub::Configurer::Request::Http::Basic) }
80
119
 
81
- subject { HttpStub::Configurer::Request::Http::Factory.post(path) }
120
+ subject { described_class.post(path) }
82
121
 
83
- before(:example) do
84
- allow(Net::HTTP::Post).to receive(:new).and_return(post_request)
85
- allow(HttpStub::Configurer::Request::Http::Basic).to receive(:new).and_return(basic_request)
122
+ before(:example) { allow(HttpStub::Configurer::Request::Http::Basic).to receive(:new).and_return(basic_request) }
123
+
124
+ it "creates a basic POST request" do
125
+ expect(HttpStub::Configurer::Request::Http::Basic).to receive(:new).with(hash_including(method: :post))
126
+
127
+ subject
86
128
  end
87
129
 
88
- it "creates a POST request with the provided path" do
89
- expect(Net::HTTP::Post).to receive(:new).with(path)
130
+ it "creates a basic request with the provided path" do
131
+ expect(HttpStub::Configurer::Request::Http::Basic).to receive(:new).with(hash_including(path: path))
90
132
 
91
133
  subject
92
134
  end
93
135
 
94
136
  context "when parameters are provided" do
95
137
 
96
- let(:parameters) { (1..3).each_with_object({}) { |i, result| result["key#{i}"] = "value#{i}" } }
138
+ let(:parameters) { (1..3).each_with_object({}) { |i, result| result["parameter#{i}"] = "value#{i}" } }
97
139
 
98
- subject { HttpStub::Configurer::Request::Http::Factory.post(path, parameters) }
140
+ subject { described_class.post(path, parameters) }
99
141
 
100
- it "establishes the provided parameters as the form data in the POST request" do
101
- expect(post_request).to receive(:set_form_data).with(parameters)
142
+ it "creates a basic request with the provided parameters" do
143
+ expect(HttpStub::Configurer::Request::Http::Basic).to receive(:new).with(hash_including(parameters: parameters))
102
144
 
103
145
  subject
104
146
  end
@@ -107,22 +149,16 @@ describe HttpStub::Configurer::Request::Http::Factory do
107
149
 
108
150
  context "when no parameters are provided" do
109
151
 
110
- subject { HttpStub::Configurer::Request::Http::Factory.post(path) }
152
+ subject { described_class.post(path) }
111
153
 
112
- it "establishes empty form data in the POST request" do
113
- expect(post_request).to receive(:set_form_data).with({})
154
+ it "creates a basic POST request with empty parameters" do
155
+ allow(HttpStub::Configurer::Request::Http::Basic).to receive(:new).with(hash_including(parameters: {}))
114
156
 
115
157
  subject
116
158
  end
117
159
 
118
160
  end
119
161
 
120
- it "creates a basic request wrapping the POST request" do
121
- expect(HttpStub::Configurer::Request::Http::Basic).to receive(:new).with(post_request)
122
-
123
- subject
124
- end
125
-
126
162
  it "returns the created basic request" do
127
163
  expect(subject).to eql(basic_request)
128
164
  end
@@ -131,29 +167,52 @@ describe HttpStub::Configurer::Request::Http::Factory do
131
167
 
132
168
  describe "::delete" do
133
169
 
134
- let(:path) { "/some/delete/path" }
135
- let(:delete_request) { instance_double(Net::HTTP::Delete) }
136
- let(:basic_request) { instance_double(HttpStub::Configurer::Request::Http::Basic) }
170
+ let(:path) { "/some/delete/path" }
171
+
172
+ let(:basic_request) { instance_double(HttpStub::Configurer::Request::Http::Basic) }
137
173
 
138
- subject { HttpStub::Configurer::Request::Http::Factory.delete(path) }
174
+ subject { described_class.delete(path) }
139
175
 
140
- before(:example) do
141
- allow(Net::HTTP::Delete).to receive(:new).and_return(delete_request)
142
- allow(HttpStub::Configurer::Request::Http::Basic).to receive(:new).and_return(basic_request)
143
- end
176
+ before(:example) { allow(HttpStub::Configurer::Request::Http::Basic).to receive(:new).and_return(basic_request) }
144
177
 
145
- it "creates a DELETE request with the provided path" do
146
- expect(Net::HTTP::Delete).to receive(:new).with(path)
178
+ it "creates a basic DELETE request" do
179
+ expect(HttpStub::Configurer::Request::Http::Basic).to receive(:new).with(hash_including(method: :delete))
147
180
 
148
181
  subject
149
182
  end
150
183
 
151
- it "creates a basic request wrapping the DELETE request" do
152
- expect(HttpStub::Configurer::Request::Http::Basic).to receive(:new).with(delete_request)
184
+ it "creates a basic request with the provided path" do
185
+ expect(HttpStub::Configurer::Request::Http::Basic).to receive(:new).with(hash_including(path: path))
153
186
 
154
187
  subject
155
188
  end
156
189
 
190
+ context "when headers are provided" do
191
+
192
+ let(:headers) { (1..3).each_with_object({}) { |i, result| result["header#{i}"] = "value#{i}" } }
193
+
194
+ subject { described_class.delete(path, headers) }
195
+
196
+ it "creates a basic request with the provided headers" do
197
+ expect(HttpStub::Configurer::Request::Http::Basic).to receive(:new).with(hash_including(headers: headers))
198
+
199
+ subject
200
+ end
201
+
202
+ end
203
+
204
+ context "when no headers are provided" do
205
+
206
+ subject { described_class.delete(path) }
207
+
208
+ it "creates a basic request with empty headers" do
209
+ expect(HttpStub::Configurer::Request::Http::Basic).to receive(:new).with(hash_including(headers: {}))
210
+
211
+ subject
212
+ end
213
+
214
+ end
215
+
157
216
  it "returns the created basic request" do
158
217
  expect(subject).to eql(basic_request)
159
218
  end
@@ -13,9 +13,12 @@ describe HttpStub::Configurer::Request::Http::Multipart do
13
13
 
14
14
  let(:payload) { { key: "value" } }
15
15
  let(:response_files) { [] }
16
+
17
+ let(:path) { "some/model/path" }
16
18
  let(:model) { HttpStub::Configurer::Request::SomeModel.new(payload: payload, response_files: response_files) }
19
+ let(:args) { { path: path , model: model } }
17
20
 
18
- let(:multipart_request) { HttpStub::Configurer::Request::Http::Multipart.new(model) }
21
+ let(:multipart_request) { HttpStub::Configurer::Request::Http::Multipart.new(args) }
19
22
 
20
23
  describe "#to_http_request" do
21
24
 
@@ -34,14 +37,16 @@ describe HttpStub::Configurer::Request::Http::Multipart do
34
37
  expect(subject).to eql(http_multipart_request)
35
38
  end
36
39
 
37
- it "creates a HTTP request with a path to the pluralized name for the model" do
38
- expect(Net::HTTP::Post::Multipart).to receive(:new).with("/http_stub/some_models", anything)
40
+ it "creates a HTTP request with the provided path prefixed with http_stub scoping" do
41
+ expect(Net::HTTP::Post::Multipart).to receive(:new).with("/http_stub/some/model/path", anything, anything)
39
42
 
40
43
  subject
41
44
  end
42
45
 
43
46
  it "creates a HTTP request with a payload parameter containing the JSON representation of the payload" do
44
- expect(Net::HTTP::Post::Multipart).to receive(:new).with(anything, hash_including(payload: payload.to_json))
47
+ expect(Net::HTTP::Post::Multipart).to(
48
+ receive(:new).with(anything, hash_including(payload: payload.to_json), anything)
49
+ )
45
50
 
46
51
  subject
47
52
  end
@@ -63,7 +68,9 @@ describe HttpStub::Configurer::Request::Http::Multipart do
63
68
  )
64
69
  result.tap { |hash| hash["response_file_#{response_file.id}"] = upload_file }
65
70
  end
66
- expect(Net::HTTP::Post::Multipart).to receive(:new).with(anything, hash_including(expected_upload_parameters))
71
+ expect(Net::HTTP::Post::Multipart).to(
72
+ receive(:new).with(anything, hash_including(expected_upload_parameters), anything)
73
+ )
67
74
 
68
75
  subject
69
76
  end
@@ -73,7 +80,33 @@ describe HttpStub::Configurer::Request::Http::Multipart do
73
80
  context "when the payload contains no response files" do
74
81
 
75
82
  it "creates a HTTP request with no upload parameters" do
76
- expect(Net::HTTP::Post::Multipart).to receive(:new).with(anything, payload: payload.to_json)
83
+ expect(Net::HTTP::Post::Multipart).to receive(:new).with(anything, { payload: payload.to_json }, anything)
84
+
85
+ subject
86
+ end
87
+
88
+ end
89
+
90
+ context "when headers are provided" do
91
+
92
+ let(:headers) { (1..3).each_with_object({}) { |i, result| result["header#{i}"] = "value#{i}" } }
93
+
94
+ let(:args) { { path: path, headers: headers, model: model } }
95
+
96
+ it "creates a HTTP request with the provided headers" do
97
+ expect(Net::HTTP::Post::Multipart).to receive(:new).with(anything, anything, headers)
98
+
99
+ subject
100
+ end
101
+
102
+ end
103
+
104
+ context "when no headers are provided" do
105
+
106
+ let(:args) { { path: path, model: model } }
107
+
108
+ it "creates a HTTP request with empty headers" do
109
+ expect(Net::HTTP::Post::Multipart).to receive(:new).with(anything, anything, {})
77
110
 
78
111
  subject
79
112
  end
@@ -1,51 +1,25 @@
1
1
  describe HttpStub::Configurer::Request::Stub do
2
2
 
3
3
  let(:fixture) { HttpStub::StubFixture.new }
4
- let(:stub_response) { instance_double(HttpStub::Configurer::Request::StubResponse).as_null_object }
5
- let(:trigger_builders) { [] }
4
+ let(:stub_response) { instance_double(HttpStub::Configurer::Request::StubResponse) }
5
+ let(:stub_triggers) { instance_double(HttpStub::Configurer::Request::Triggers) }
6
6
 
7
- let(:stub) do
8
- HttpStub::Configurer::Request::Stub.new(fixture.configurer_payload.merge(triggers: trigger_builders))
9
- end
10
-
11
- before(:example) { allow(HttpStub::Configurer::Request::StubResponse).to receive(:new).and_return(stub_response) }
12
-
13
- shared_context "triggers one stub" do
14
-
15
- let(:trigger_payload) { HttpStub::StubFixture.new.configurer_payload }
16
- let(:trigger_files) { [] }
17
- let(:trigger) do
18
- instance_double(HttpStub::Configurer::Request::Stub,
19
- payload: trigger_payload, response_files: trigger_files)
20
- end
21
- let(:trigger_builders) do
22
- [ instance_double(HttpStub::Configurer::DSL::StubBuilder, build: trigger) ]
23
- end
24
-
25
- end
26
-
27
- shared_context "triggers many stubs" do
28
-
29
- let(:trigger_payloads) { (1..3).map { HttpStub::StubFixture.new.configurer_payload } }
30
- let(:triggers_files) { trigger_payloads.map { [] } }
31
- let(:triggers) do
32
- trigger_payloads.zip(triggers_files).map do |payload, files|
33
- instance_double(HttpStub::Configurer::Request::Stub, payload: payload, response_files: files)
34
- end
35
- end
36
- let(:trigger_builders) do
37
- triggers.map do |trigger|
38
- instance_double(HttpStub::Configurer::DSL::StubBuilder, build: trigger)
39
- end
40
- end
7
+ let(:stub) { HttpStub::Configurer::Request::Stub.new(fixture.configurer_payload) }
41
8
 
9
+ before(:example) do
10
+ allow(HttpStub::Configurer::Request::StubResponse).to receive(:new).and_return(stub_response)
11
+ allow(HttpStub::Configurer::Request::Triggers).to receive(:new).and_return(stub_triggers)
42
12
  end
43
13
 
44
14
  describe "#payload" do
45
15
 
46
16
  subject { stub.payload }
47
17
 
48
- before(:example) { allow(HttpStub::Configurer::Request::ControllableValue).to receive(:format) }
18
+ before(:example) do
19
+ allow(HttpStub::Configurer::Request::ControllableValue).to receive(:format)
20
+ allow(stub_response).to receive(:payload)
21
+ allow(stub_triggers).to receive(:payload)
22
+ end
49
23
 
50
24
  context "when request headers" do
51
25
 
@@ -127,21 +101,18 @@ describe HttpStub::Configurer::Request::Stub do
127
101
 
128
102
  it "creates a stub response with the provided response options" do
129
103
  expect(HttpStub::Configurer::Request::StubResponse).to(
130
- receive(:new).with(anything, fixture.response.symbolized).and_return(stub_response)
104
+ receive(:new).with(anything, fixture.configurer_payload[:response]).and_return(stub_response)
131
105
  )
132
106
 
133
107
  subject
134
108
  end
135
109
 
136
- context "when many triggers are provided" do
137
- include_context "triggers many stubs"
138
-
139
- it "builds the request object for each trigger" do
140
- trigger_builders.each { |trigger_builder| expect(trigger_builder).to receive(:build) }
141
-
142
- subject
143
- end
110
+ it "creates stub triggers with the provided triggers options" do
111
+ expect(HttpStub::Configurer::Request::Triggers).to(
112
+ receive(:new).with(fixture.configurer_payload[:triggers]).and_return(stub_triggers)
113
+ )
144
114
 
115
+ subject
145
116
  end
146
117
 
147
118
  context "builds a hash that" do
@@ -180,30 +151,10 @@ describe HttpStub::Configurer::Request::Stub do
180
151
  expect(subject).to include(response: "response payload")
181
152
  end
182
153
 
183
- context "when a trigger is added" do
184
- include_context "triggers one stub"
185
-
186
- it "has a triggers entry containing the stub trigger payload" do
187
- expect(subject).to include(triggers: [ trigger_payload ])
188
- end
189
-
190
- end
191
-
192
- context "when many triggers are added" do
193
- include_context "triggers many stubs"
194
-
195
- it "has a triggers entry containing the stub trigger payloads" do
196
- expect(subject).to include(triggers: trigger_payloads)
197
- end
198
-
199
- end
200
-
201
- context "when no triggers are added" do
202
-
203
- it "has a triggers entry containing an empty hash" do
204
- expect(subject).to include(triggers: [])
205
- end
154
+ it "has an entry containing the triggers payload" do
155
+ allow(stub_triggers).to receive(:payload).and_return("triggers payload")
206
156
 
157
+ expect(subject).to include(triggers: "triggers payload")
207
158
  end
208
159
 
209
160
  end
@@ -212,66 +163,57 @@ describe HttpStub::Configurer::Request::Stub do
212
163
 
213
164
  describe "#response_files" do
214
165
 
215
- subject { stub.response_files }
166
+ let(:stub_response_file) { nil }
167
+ let(:triggers_response_files) { [] }
216
168
 
217
- context "when the response contains a file" do
218
-
219
- let(:response_file) { instance_double(HttpStub::Configurer::Request::StubResponseFile) }
169
+ subject { stub.response_files }
220
170
 
221
- before(:example) { allow(stub_response).to receive(:file).and_return(response_file) }
171
+ before(:example) do
172
+ allow(stub_response).to receive(:file).and_return(stub_response_file)
173
+ allow(stub_triggers).to receive(:response_files).and_return(triggers_response_files)
174
+ end
222
175
 
223
- context "and the triggers contain files" do
224
- include_context "triggers many stubs"
176
+ context "when the response contains a file" do
225
177
 
226
- let(:triggers_files) do
227
- trigger_payloads.map { (1..3).map { instance_double(HttpStub::Configurer::Request::StubResponseFile) } }
228
- end
178
+ let(:stub_response_file) { instance_double(HttpStub::Configurer::Request::StubResponseFile) }
229
179
 
230
- it "returns the stub response's file and the triggers files" do
231
- expect(subject).to eql(([ response_file ] + triggers_files).flatten)
232
- end
180
+ let(:triggers_response_files) { [] }
233
181
 
182
+ it "includes the stub response's file" do
183
+ expect(subject).to include(stub_response_file)
234
184
  end
235
185
 
236
- context "and the triggers contain no files" do
237
- include_context "triggers many stubs"
186
+ end
238
187
 
239
- let(:triggers_files) { trigger_payloads.map { (1..3).map { [] } } }
188
+ context "when the triggers contain files" do
240
189
 
241
- it "returns the stub response's file" do
242
- expect(subject).to eql([ response_file ])
243
- end
190
+ let(:triggers_response_files) do
191
+ (1..3).map { instance_double(HttpStub::Configurer::Request::StubResponseFile) }
192
+ end
244
193
 
194
+ it "includes the triggers files" do
195
+ expect(subject).to include(*triggers_response_files)
245
196
  end
246
197
 
247
198
  end
248
199
 
249
- context "when the response body does not contain a file" do
250
-
251
- before(:example) { allow(stub_response).to receive(:file).and_return(nil) }
252
-
253
- context "and the triggers contain files" do
254
- include_context "triggers many stubs"
255
-
256
- let(:triggers_files) do
257
- trigger_payloads.map { (1..3).map { instance_double(HttpStub::Configurer::Request::StubResponseFile) } }
258
- end
259
-
260
- it "returns the stub response's file and the triggers files" do
261
- expect(subject).to eql(triggers_files.flatten)
262
- end
200
+ context "when both the response and triggers contain files" do
263
201
 
202
+ let(:stub_response_file) { instance_double(HttpStub::Configurer::Request::StubResponseFile) }
203
+ let(:triggers_response_files) do
204
+ (1..3).map { instance_double(HttpStub::Configurer::Request::StubResponseFile) }
264
205
  end
265
206
 
266
- context "and no triggers contain a file" do
267
- include_context "triggers many stubs"
207
+ it "is all the files" do
208
+ expect(subject).to eql(([ stub_response_file ] + triggers_response_files).flatten)
209
+ end
268
210
 
269
- let(:triggers_files) { trigger_payloads.map { (1..3).map { [] } } }
211
+ end
270
212
 
271
- it "returns a empty array" do
272
- expect(subject).to eql([])
273
- end
213
+ context "when neither the response or triggers contain files" do
274
214
 
215
+ it "returns an empty array" do
216
+ expect(subject).to eql([])
275
217
  end
276
218
 
277
219
  end