http_stub 0.14.2 → 0.15.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (118) hide show
  1. checksums.yaml +4 -4
  2. data/lib/http_stub/configurer/request/http/basic.rb +21 -0
  3. data/lib/http_stub/configurer/request/http/factory.rb +43 -0
  4. data/lib/http_stub/configurer/request/http/multipart.rb +33 -0
  5. data/lib/http_stub/configurer/request/stub.rb +24 -8
  6. data/lib/http_stub/configurer/request/stub_activator.rb +12 -8
  7. data/lib/http_stub/configurer/request/{stub_activator_payload_builder.rb → stub_activator_builder.rb} +4 -4
  8. data/lib/http_stub/configurer/request/stub_builder.rb +34 -0
  9. data/lib/http_stub/configurer/request/stub_response.rb +40 -0
  10. data/lib/http_stub/configurer/request/stub_response_file.rb +20 -0
  11. data/lib/http_stub/configurer/server/command.rb +5 -1
  12. data/lib/http_stub/configurer/server/command_processor.rb +1 -1
  13. data/lib/http_stub/configurer/server/dsl.rb +4 -4
  14. data/lib/http_stub/configurer/server/facade.rb +25 -11
  15. data/lib/http_stub/extensions/core/hash.rb +26 -0
  16. data/lib/http_stub/rake/server_daemon_tasks.rb +1 -1
  17. data/lib/http_stub/rake/server_tasks.rb +1 -1
  18. data/lib/http_stub/rake/task_generators.rb +3 -3
  19. data/lib/http_stub/server/application.rb +117 -0
  20. data/lib/http_stub/server/daemon.rb +39 -0
  21. data/lib/http_stub/{models → server}/exact_value_matcher.rb +1 -1
  22. data/lib/http_stub/{models → server}/hash_with_string_value_matchers.rb +2 -2
  23. data/lib/http_stub/{models → server}/headers.rb +1 -1
  24. data/lib/http_stub/{models → server}/omitted_value_matcher.rb +1 -1
  25. data/lib/http_stub/{models → server}/regexp_value_matcher.rb +1 -1
  26. data/lib/http_stub/{models → server}/registry.rb +10 -3
  27. data/lib/http_stub/{models → server}/request_header_parser.rb +1 -1
  28. data/lib/http_stub/server/response.rb +13 -0
  29. data/lib/http_stub/server/response_pipeline.rb +18 -0
  30. data/lib/http_stub/{models → server}/string_value_matcher.rb +4 -4
  31. data/lib/http_stub/server/stub.rb +37 -0
  32. data/lib/http_stub/{models → server}/stub_activator.rb +10 -2
  33. data/lib/http_stub/{controllers → server}/stub_activator_controller.rb +5 -5
  34. data/lib/http_stub/{controllers → server}/stub_controller.rb +4 -4
  35. data/lib/http_stub/server/stub_factory.rb +30 -0
  36. data/lib/http_stub/{models → server}/stub_headers.rb +4 -4
  37. data/lib/http_stub/{models → server}/stub_parameters.rb +2 -2
  38. data/lib/http_stub/{models → server}/stub_registry.rb +4 -4
  39. data/lib/http_stub/server/stub_response/base.rb +62 -0
  40. data/lib/http_stub/server/stub_response/file.rb +48 -0
  41. data/lib/http_stub/server/stub_response/text.rb +17 -0
  42. data/lib/http_stub/server/stub_response.rb +14 -0
  43. data/lib/http_stub/{models → server}/stub_triggers.rb +6 -2
  44. data/lib/http_stub/{models → server}/stub_uri.rb +2 -2
  45. data/lib/http_stub/{views → server/views}/_stub.haml +0 -0
  46. data/lib/http_stub/{views → server/views}/application.sass +0 -0
  47. data/lib/http_stub/{views → server/views}/layout.haml +0 -0
  48. data/lib/http_stub/{views → server/views}/stub_activators.haml +0 -0
  49. data/lib/http_stub/{views → server/views}/stubs.haml +0 -0
  50. data/lib/http_stub/version.rb +1 -1
  51. data/lib/http_stub.rb +41 -24
  52. data/spec/lib/http_stub/configurer/deprecated_dsl_spec.rb +2 -2
  53. data/spec/lib/http_stub/configurer/request/http/basic_spec.rb +15 -0
  54. data/spec/lib/http_stub/configurer/request/http/factory_spec.rb +148 -0
  55. data/spec/lib/http_stub/configurer/request/http/multipart_spec.rb +74 -0
  56. data/spec/lib/http_stub/configurer/request/stub_activator_builder_spec.rb +119 -0
  57. data/spec/lib/http_stub/configurer/request/stub_activator_spec.rb +24 -22
  58. data/spec/lib/http_stub/configurer/request/stub_builder_spec.rb +154 -0
  59. data/spec/lib/http_stub/configurer/request/stub_response_spec.rb +136 -0
  60. data/spec/lib/http_stub/configurer/request/stub_spec.rb +238 -17
  61. data/spec/lib/http_stub/configurer/server/command_processor_integration_spec.rb +7 -3
  62. data/spec/lib/http_stub/configurer/server/command_spec.rb +7 -6
  63. data/spec/lib/http_stub/configurer/server/dsl_spec.rb +58 -94
  64. data/spec/lib/http_stub/configurer/server/facade_spec.rb +56 -41
  65. data/spec/lib/http_stub/configurer_integration_spec.rb +180 -81
  66. data/spec/lib/http_stub/{hash_extensions_spec.rb → extensions/core/hash_spec.rb} +1 -1
  67. data/spec/lib/http_stub/rake/server_daemon_tasks_smoke_spec.rb +1 -1
  68. data/spec/lib/http_stub/{server_integration_spec.rb → server/application_integration_spec.rb} +1 -1
  69. data/spec/lib/http_stub/server/application_spec.rb +205 -0
  70. data/spec/lib/http_stub/server/daemon_integration_spec.rb +7 -0
  71. data/spec/lib/http_stub/{server_daemon_spec.rb → server/daemon_spec.rb} +4 -4
  72. data/spec/lib/http_stub/{models → server}/exact_value_matcher_spec.rb +2 -2
  73. data/spec/lib/http_stub/{models → server}/hash_with_string_value_matchers_spec.rb +5 -5
  74. data/spec/lib/http_stub/{models → server}/headers_spec.rb +2 -2
  75. data/spec/lib/http_stub/{models → server}/omitted_value_matcher_spec.rb +2 -2
  76. data/spec/lib/http_stub/{models → server}/regexp_value_matcher_spec.rb +2 -2
  77. data/spec/lib/http_stub/{models → server}/registry_spec.rb +52 -55
  78. data/spec/lib/http_stub/{models → server}/request_header_parser_spec.rb +4 -4
  79. data/spec/lib/http_stub/server/response_pipeline_spec.rb +29 -0
  80. data/spec/lib/http_stub/server/response_spec.rb +41 -0
  81. data/spec/lib/http_stub/{models → server}/string_value_matcher_spec.rb +11 -11
  82. data/spec/lib/http_stub/server/stub_activator_controller_spec.rb +85 -0
  83. data/spec/lib/http_stub/server/stub_activator_spec.rb +124 -0
  84. data/spec/lib/http_stub/server/stub_controller_spec.rb +74 -0
  85. data/spec/lib/http_stub/server/stub_factory_spec.rb +117 -0
  86. data/spec/lib/http_stub/{models → server}/stub_headers_spec.rb +9 -9
  87. data/spec/lib/http_stub/{models → server}/stub_parameters_spec.rb +6 -6
  88. data/spec/lib/http_stub/{models → server}/stub_registry_integration_spec.rb +3 -3
  89. data/spec/lib/http_stub/{models → server}/stub_registry_spec.rb +15 -35
  90. data/spec/lib/http_stub/server/stub_response/base_spec.rb +225 -0
  91. data/spec/lib/http_stub/server/stub_response/file_spec.rb +140 -0
  92. data/spec/lib/http_stub/server/stub_response/text_spec.rb +103 -0
  93. data/spec/lib/http_stub/server/stub_response_spec.rb +49 -0
  94. data/spec/lib/http_stub/{models → server}/stub_spec.rb +34 -18
  95. data/spec/lib/http_stub/{models → server}/stub_triggers_spec.rb +26 -16
  96. data/spec/lib/http_stub/{models → server}/stub_uri_spec.rb +5 -5
  97. data/spec/resources/sample.pdf +0 -0
  98. data/spec/resources/sample.txt +1 -0
  99. data/spec/spec_helper.rb +13 -4
  100. data/spec/support/stub_fixture.rb +106 -0
  101. metadata +148 -102
  102. data/lib/http_stub/configurer/request/stub_payload_builder.rb +0 -46
  103. data/lib/http_stub/hash_extensions.rb +0 -22
  104. data/lib/http_stub/models/request_pipeline.rb +0 -13
  105. data/lib/http_stub/models/response.rb +0 -43
  106. data/lib/http_stub/models/stub.rb +0 -35
  107. data/lib/http_stub/server.rb +0 -114
  108. data/lib/http_stub/server_daemon.rb +0 -36
  109. data/spec/lib/http_stub/configurer/request/stub_activator_payload_builder_spec.rb +0 -103
  110. data/spec/lib/http_stub/configurer/request/stub_payload_builder_spec.rb +0 -304
  111. data/spec/lib/http_stub/controllers/stub_activator_controller_spec.rb +0 -94
  112. data/spec/lib/http_stub/controllers/stub_controller_spec.rb +0 -83
  113. data/spec/lib/http_stub/models/request_pipeline_spec.rb +0 -17
  114. data/spec/lib/http_stub/models/response_spec.rb +0 -206
  115. data/spec/lib/http_stub/models/stub_activator_spec.rb +0 -76
  116. data/spec/lib/http_stub/server_daemon_integration_spec.rb +0 -7
  117. data/spec/lib/http_stub/server_spec.rb +0 -234
  118. data/spec/support/http_stub/configurer/request/stub_payload_builder_shared_context.rb +0 -27
@@ -0,0 +1,148 @@
1
+ describe HttpStub::Configurer::Request::Http::Factory do
2
+
3
+ describe "::stub" do
4
+
5
+ let(:model) { instance_double(HttpStub::Configurer::Request::Stub) }
6
+ let(:multipart_request) { instance_double(HttpStub::Configurer::Request::Http::Multipart) }
7
+
8
+ subject { HttpStub::Configurer::Request::Http::Factory.stub(model) }
9
+
10
+ before(:example) do
11
+ allow(HttpStub::Configurer::Request::Http::Multipart).to receive(:new).and_return(multipart_request)
12
+ end
13
+
14
+ it "creates a multipart request for the stubs endpoint with the provided model" do
15
+ expect(HttpStub::Configurer::Request::Http::Multipart).to receive(:new).with("/stubs", model)
16
+
17
+ subject
18
+ end
19
+
20
+ it "returns the created request" do
21
+ expect(subject).to eql(multipart_request)
22
+ end
23
+
24
+ end
25
+
26
+ describe "::stub_activator" do
27
+
28
+ let(:model) { instance_double(HttpStub::Configurer::Request::StubActivator) }
29
+ let(:multipart_request) { instance_double(HttpStub::Configurer::Request::Http::Multipart) }
30
+
31
+ subject { HttpStub::Configurer::Request::Http::Factory.stub_activator(model) }
32
+
33
+ before(:example) do
34
+ allow(HttpStub::Configurer::Request::Http::Multipart).to receive(:new).and_return(multipart_request)
35
+ end
36
+
37
+ it "creates a multipart request for the stub activators endpoint with the provided model" do
38
+ expect(HttpStub::Configurer::Request::Http::Multipart).to receive(:new).with("/stubs/activators", model)
39
+
40
+ subject
41
+ end
42
+
43
+ it "returns the created request" do
44
+ expect(subject).to eql(multipart_request)
45
+ end
46
+
47
+ end
48
+
49
+ describe "::get" do
50
+
51
+ let(:path) { "some/get/path" }
52
+ let(:get_request) { instance_double(Net::HTTP::Get) }
53
+ let(:basic_request) { instance_double(HttpStub::Configurer::Request::Http::Basic) }
54
+
55
+ subject { HttpStub::Configurer::Request::Http::Factory.get(path) }
56
+
57
+ before(:example) do
58
+ allow(Net::HTTP::Get).to receive(:new).and_return(get_request)
59
+ allow(HttpStub::Configurer::Request::Http::Basic).to receive(:new).and_return(basic_request)
60
+ end
61
+
62
+ it "creates a GET request with the provided path" do
63
+ expect(Net::HTTP::Get).to receive(:new).with(path)
64
+
65
+ subject
66
+ end
67
+
68
+ it "creates a basic request wrapping the GET request" do
69
+ expect(HttpStub::Configurer::Request::Http::Basic).to receive(:new).with(get_request)
70
+
71
+ subject
72
+ end
73
+
74
+ it "returns the created basic request" do
75
+ expect(subject).to eql(basic_request)
76
+ end
77
+
78
+ end
79
+
80
+ describe "::post" do
81
+
82
+ let(:path) { "some/post/path" }
83
+ let(:post_request) { instance_double(Net::HTTP::Post).as_null_object }
84
+ let(:basic_request) { instance_double(HttpStub::Configurer::Request::Http::Basic) }
85
+
86
+ subject { HttpStub::Configurer::Request::Http::Factory.post(path) }
87
+
88
+ before(:example) do
89
+ allow(Net::HTTP::Post).to receive(:new).and_return(post_request)
90
+ allow(HttpStub::Configurer::Request::Http::Basic).to receive(:new).and_return(basic_request)
91
+ end
92
+
93
+ it "creates a POST request with the provided path" do
94
+ expect(Net::HTTP::Post).to receive(:new).with(path)
95
+
96
+ subject
97
+ end
98
+
99
+ it "establishes an empty body in the POST request" do
100
+ expect(post_request).to receive(:body=).with("")
101
+
102
+ subject
103
+ end
104
+
105
+ it "creates a basic request wrapping the POST request" do
106
+ expect(HttpStub::Configurer::Request::Http::Basic).to receive(:new).with(post_request)
107
+
108
+ subject
109
+ end
110
+
111
+ it "returns the created basic request" do
112
+ expect(subject).to eql(basic_request)
113
+ end
114
+
115
+ end
116
+
117
+ describe "::delete" do
118
+
119
+ let(:path) { "some/get/path" }
120
+ let(:delete_request) { instance_double(Net::HTTP::Delete) }
121
+ let(:basic_request) { instance_double(HttpStub::Configurer::Request::Http::Basic) }
122
+
123
+ subject { HttpStub::Configurer::Request::Http::Factory.delete(path) }
124
+
125
+ before(:example) do
126
+ allow(Net::HTTP::Delete).to receive(:new).and_return(delete_request)
127
+ allow(HttpStub::Configurer::Request::Http::Basic).to receive(:new).and_return(basic_request)
128
+ end
129
+
130
+ it "creates a DELETE request with the provided path" do
131
+ expect(Net::HTTP::Delete).to receive(:new).with(path)
132
+
133
+ subject
134
+ end
135
+
136
+ it "creates a basic request wrapping the DELETE request" do
137
+ expect(HttpStub::Configurer::Request::Http::Basic).to receive(:new).with(delete_request)
138
+
139
+ subject
140
+ end
141
+
142
+ it "returns the created basic request" do
143
+ expect(subject).to eql(basic_request)
144
+ end
145
+
146
+ end
147
+
148
+ end
@@ -0,0 +1,74 @@
1
+ describe HttpStub::Configurer::Request::Http::Multipart do
2
+
3
+ let(:path) { "/a/request/path" }
4
+ let(:payload) { { key: "value" } }
5
+ let(:response_files) { [] }
6
+ let(:model) { instance_double(HttpStub::Configurer::Request::Stub, payload: payload, response_files: response_files) }
7
+ let(:multipart_request) { HttpStub::Configurer::Request::Http::Multipart.new(path, model) }
8
+
9
+ describe "#to_http_request" do
10
+
11
+ subject { multipart_request.to_http_request }
12
+
13
+ it "creates a HTTP multipart request" do
14
+ expect(Net::HTTP::Post::Multipart).to receive(:new)
15
+
16
+ subject
17
+ end
18
+
19
+ it "returns the created HTTP multipart request" do
20
+ http_multipart_request = instance_double(Net::HTTP::Post::Multipart)
21
+ allow(Net::HTTP::Post::Multipart).to receive(:new).and_return(http_multipart_request)
22
+
23
+ expect(subject).to eql(http_multipart_request)
24
+ end
25
+
26
+ it "creates a HTTP request with the provided path" do
27
+ expect(Net::HTTP::Post::Multipart).to receive(:new).with(path, anything)
28
+
29
+ subject
30
+ end
31
+
32
+ it "creates a HTTP request with a payload parameter containing the JSON representation of the payload" do
33
+ expect(Net::HTTP::Post::Multipart).to receive(:new).with(anything, hash_including(payload: payload.to_json))
34
+
35
+ subject
36
+ end
37
+
38
+ context "when the payload contains response files" do
39
+
40
+ let(:response_files) do
41
+ (1..3).map do |i|
42
+ instance_double(HttpStub::Configurer::Request::StubResponseFile,
43
+ id: "id#{i}", path: "file/path/#{i}", type: "file-type-#{i}", name: "file_name_#{i}")
44
+ end
45
+ end
46
+
47
+ it "creates a HTTP request with an upload parameter for each file keyed on the file ID" do
48
+ expected_upload_parameters = response_files.reduce({}) do |result, response_file|
49
+ upload_file = instance_double(UploadIO)
50
+ expect(UploadIO).to(
51
+ receive(:new).with(response_file.path, response_file.type, response_file.name).and_return(upload_file)
52
+ )
53
+ result.tap { |hash| hash["response_file_#{response_file.id}"] = upload_file }
54
+ end
55
+ expect(Net::HTTP::Post::Multipart).to receive(:new).with(anything, hash_including(expected_upload_parameters))
56
+
57
+ subject
58
+ end
59
+
60
+ end
61
+
62
+ context "when the payload contains no response files" do
63
+
64
+ it "creates a HTTP request with no upload parameters" do
65
+ expect(Net::HTTP::Post::Multipart).to receive(:new).with(anything, payload: payload.to_json)
66
+
67
+ subject
68
+ end
69
+
70
+ end
71
+
72
+ end
73
+
74
+ end
@@ -0,0 +1,119 @@
1
+ describe HttpStub::Configurer::Request::StubActivatorBuilder do
2
+
3
+ let(:stub_fixture) { HttpStub::StubFixture.new }
4
+
5
+ let(:response_defaults) { {} }
6
+ let(:stub) { instance_double(HttpStub::Configurer::Request::Stub) }
7
+ let(:stub_builder) { instance_double(HttpStub::Configurer::Request::StubBuilder, build: stub) }
8
+
9
+ let(:builder) { HttpStub::Configurer::Request::StubActivatorBuilder.new(response_defaults) }
10
+
11
+ before(:example) { allow(HttpStub::Configurer::Request::StubBuilder).to receive(:new).and_return(stub_builder) }
12
+
13
+ describe "constructor" do
14
+
15
+ subject { builder }
16
+
17
+ it "creates a stub builder with the provided response defaults" do
18
+ expect(HttpStub::Configurer::Request::StubBuilder).to receive(:new).with(response_defaults)
19
+
20
+ subject
21
+ end
22
+
23
+ end
24
+
25
+ describe "#match_requests" do
26
+
27
+ let(:request_payload) { stub_fixture.request.configurer_payload }
28
+
29
+ it "delegates to a stub builder" do
30
+ expect(stub_builder).to receive(:match_requests).with(stub_fixture.request.uri, request_payload)
31
+
32
+ builder.match_requests(stub_fixture.request.uri, request_payload)
33
+ end
34
+
35
+ end
36
+
37
+ describe "#respond_with" do
38
+
39
+ let(:response_payload) { stub_fixture.response.configurer_payload }
40
+
41
+ it "delegates to a stub builder" do
42
+ expect(stub_builder).to receive(:respond_with).with(response_payload)
43
+
44
+ builder.respond_with(response_payload)
45
+ end
46
+
47
+ end
48
+
49
+ describe "#trigger" do
50
+
51
+ context "when one triggered stub is provided" do
52
+
53
+ let(:trigger_builder) { instance_double(HttpStub::Configurer::Request::StubBuilder) }
54
+
55
+ it "delegates to a stub builder" do
56
+ expect(stub_builder).to receive(:trigger).with(trigger_builder)
57
+
58
+ builder.trigger(trigger_builder)
59
+ end
60
+
61
+ end
62
+
63
+ context "when many triggered stubs are provided" do
64
+
65
+ let(:trigger_builders) { (1..3).map { instance_double(HttpStub::Configurer::Request::StubBuilder) } }
66
+
67
+ it "delegates to a stub builder" do
68
+ expect(stub_builder).to receive(:trigger).with(trigger_builders)
69
+
70
+ builder.trigger(trigger_builders)
71
+ end
72
+
73
+ end
74
+
75
+ end
76
+
77
+ describe "#build" do
78
+
79
+ let(:stub_activator) { instance_double(HttpStub::Configurer::Request::StubActivator) }
80
+
81
+ subject { builder.build }
82
+
83
+ before(:example) { allow(HttpStub::Configurer::Request::StubActivator).to receive(:new).and_return(stub_activator) }
84
+
85
+ context "when a path on which the activator is activated is established" do
86
+
87
+ let(:activation_uri) { "http://some/activation/uri" }
88
+
89
+ before(:example) { builder.on(activation_uri) }
90
+
91
+ it "creates a stub activator that includes the activation uri" do
92
+ expect(HttpStub::Configurer::Request::StubActivator).to(
93
+ receive(:new).with(hash_including(activation_uri: activation_uri))
94
+ )
95
+
96
+ subject
97
+ end
98
+
99
+ it "builds the stub" do
100
+ expect(stub_builder).to receive(:build)
101
+
102
+ subject
103
+ end
104
+
105
+ it "creates a stub activator that includes the built stub" do
106
+ expect(HttpStub::Configurer::Request::StubActivator).to receive(:new).with(hash_including(stub: stub))
107
+
108
+ subject
109
+ end
110
+
111
+ it "returns the created stub activator" do
112
+ expect(subject).to eql(stub_activator)
113
+ end
114
+
115
+ end
116
+
117
+ end
118
+
119
+ end
@@ -1,41 +1,43 @@
1
1
  describe HttpStub::Configurer::Request::StubActivator do
2
2
 
3
- describe "#initialize" do
3
+ let(:activation_uri) { "some/activation/uri" }
4
+ let(:stub) { instance_double(HttpStub::Configurer::Request::Stub) }
4
5
 
5
- context "when provided a payload" do
6
+ let(:stub_activator) { HttpStub::Configurer::Request::StubActivator.new(activation_uri: activation_uri, stub: stub) }
6
7
 
7
- let(:payload) { { payload_key: "payload value" } }
8
+ describe "#payload" do
8
9
 
9
- let(:request) { HttpStub::Configurer::Request::StubActivator.new(payload) }
10
+ let(:stub_payload) { { stub_payload_key: "stub payload value " } }
10
11
 
11
- it "creates a HTTP POST request" do
12
- expect(request.method).to eql("POST")
13
- end
12
+ subject { stub_activator.payload }
14
13
 
15
- it "submits the request to '/stubs/activators'" do
16
- expect(request.path).to eql("/stubs/activators")
17
- end
14
+ before(:example) { allow(stub).to receive(:payload).and_return(stub_payload) }
18
15
 
19
- it "sets the content type to json" do
20
- expect(request.content_type).to eql("application/json")
21
- end
22
-
23
- it "sets the body to the JSON representation of the provided payload" do
24
- expect(request.body).to eql(payload.to_json)
25
- end
16
+ it "returns a hash containing the activation uri" do
17
+ expect(subject).to include(activation_uri: activation_uri)
18
+ end
26
19
 
20
+ it "returns a hash containing the stub payload" do
21
+ expect(subject).to include(stub_payload)
27
22
  end
28
23
 
29
24
  end
30
25
 
31
- describe "#activation_uri" do
26
+ describe "#response_files" do
27
+
28
+ it "delegates to the stub" do
29
+ stub_response_files = (1..3).each { instance_double(HttpStub::Configurer::Request::StubResponseFile) }
30
+ allow(stub).to receive(:response_files).and_return(stub_response_files)
32
31
 
33
- let(:activation_uri) { "http://some/activation/uri" }
32
+ expect(stub_activator.response_files).to eql(stub_response_files)
33
+ end
34
+
35
+ end
34
36
 
35
- let(:request) { HttpStub::Configurer::Request::StubActivator.new(activation_uri: activation_uri) }
37
+ describe "#to_s" do
36
38
 
37
- it "should return the activation uri from the payload" do
38
- expect(request.activation_uri).to eql(activation_uri)
39
+ it "returns the activation uri" do
40
+ expect(stub_activator.to_s).to eql(activation_uri)
39
41
  end
40
42
 
41
43
  end
@@ -0,0 +1,154 @@
1
+ describe HttpStub::Configurer::Request::StubBuilder do
2
+
3
+ let(:response_defaults) { {} }
4
+
5
+ let(:builder) { HttpStub::Configurer::Request::StubBuilder.new(response_defaults) }
6
+
7
+ shared_context "triggers one stub" do
8
+
9
+ let(:trigger_stub) { instance_double(HttpStub::Configurer::Request::Stub) }
10
+ let(:trigger_builder) do
11
+ instance_double(HttpStub::Configurer::Request::StubBuilder, build: trigger_stub)
12
+ end
13
+
14
+ before(:example) { builder.trigger(trigger_builder) }
15
+
16
+ end
17
+
18
+ shared_context "triggers many stubs" do
19
+
20
+ let(:trigger_stubs) { (1..3).map { instance_double(HttpStub::Configurer::Request::Stub) } }
21
+ let(:trigger_builders) do
22
+ trigger_stubs.map { |stub| instance_double(HttpStub::Configurer::Request::StubBuilder, build: stub) }
23
+ end
24
+
25
+ before(:example) { builder.trigger(trigger_builders) }
26
+
27
+ end
28
+
29
+ describe "#respond_with" do
30
+
31
+ it "does not modify any provided response defaults" do
32
+ original_response_defaults = response_defaults.clone
33
+
34
+ builder.respond_with(status: 201)
35
+
36
+ expect(response_defaults).to eql(original_response_defaults)
37
+ end
38
+
39
+ end
40
+
41
+ describe "#build" do
42
+
43
+ let(:fixture) { HttpStub::StubFixture.new }
44
+ let(:stub) { instance_double(HttpStub::Configurer::Request::Stub) }
45
+
46
+ subject do
47
+ builder.match_requests(fixture.request.uri, fixture.request.symbolized)
48
+ builder.respond_with(fixture.response.symbolized)
49
+
50
+ builder.build
51
+ end
52
+
53
+ before(:example) { allow(HttpStub::Configurer::Request::Stub).to receive(:new).and_return(stub) }
54
+
55
+ context "when provided a request match and response data" do
56
+
57
+ it "creates a stub payload with request options that include the uri and the provided request options" do
58
+ expect_stub_to_be_created_with(request: { uri: fixture.request.uri }.merge(fixture.request.symbolized))
59
+
60
+ subject
61
+ end
62
+
63
+ it "creates a stub payload with response arguments" do
64
+ expect_stub_to_be_created_with(response: fixture.response.symbolized)
65
+
66
+ subject
67
+ end
68
+
69
+ context "when response default options are established" do
70
+
71
+ let(:response_defaults) { { some_options: "default value" } }
72
+
73
+ it "creates a stub payload with response arguments that includes the defaults" do
74
+ expect_stub_to_be_created_with(response: fixture.response.symbolized.merge(response_defaults))
75
+
76
+ subject
77
+ end
78
+
79
+ context "and response options provided match the defaults" do
80
+
81
+ let(:response_defaults) do
82
+ {
83
+ status: 203,
84
+ headers: { "header-name-1" => "value 1", "header-name-2" => "value 2" },
85
+ body: "some body",
86
+ delay_in_seconds: 8
87
+ }
88
+ end
89
+ let(:response_overrides) do
90
+ {
91
+ status: 302,
92
+ headers: { "header-name-2" => "updated value 2", "header-name-3" => "value 3" },
93
+ body: "another body"
94
+ }
95
+ end
96
+
97
+ before(:example) { fixture.response = response_overrides }
98
+
99
+ it "overriddes the defaults with the provided options" do
100
+ expect_stub_to_be_created_with(
101
+ response: {
102
+ status: 302,
103
+ headers: { "header-name-1" => "value 1",
104
+ "header-name-2" => "updated value 2",
105
+ "header-name-3" => "value 3" },
106
+ body: "another body",
107
+ delay_in_seconds: 8
108
+ }
109
+ )
110
+
111
+ subject
112
+ end
113
+
114
+ end
115
+
116
+ end
117
+
118
+ context "when a trigger is added" do
119
+
120
+ include_context "triggers one stub"
121
+
122
+ it "creates a stub payload with the provided trigger builder" do
123
+ expect_stub_to_be_created_with(triggers: [ trigger_builder ])
124
+
125
+ subject
126
+ end
127
+
128
+ end
129
+
130
+ context "when many triggers are added" do
131
+
132
+ include_context "triggers many stubs"
133
+
134
+ it "creates a stub payload with the provided trigger builders" do
135
+ expect_stub_to_be_created_with(triggers: trigger_builders)
136
+
137
+ subject
138
+ end
139
+
140
+ end
141
+
142
+ it "returns the created stub" do
143
+ expect(subject).to eql(stub)
144
+ end
145
+
146
+ end
147
+
148
+ def expect_stub_to_be_created_with(args)
149
+ expect(HttpStub::Configurer::Request::Stub).to receive(:new).with(hash_including(args))
150
+ end
151
+
152
+ end
153
+
154
+ end
@@ -0,0 +1,136 @@
1
+ describe HttpStub::Configurer::Request::StubResponse do
2
+
3
+ let(:stub_fixture) { HttpStub::StubFixture.new }
4
+ let(:fixture) { stub_fixture.response }
5
+
6
+ let(:stub_response) { HttpStub::Configurer::Request::StubResponse.new(stub_fixture.id, fixture.symbolized) }
7
+
8
+ describe "#payload" do
9
+
10
+ subject { stub_response.payload }
11
+
12
+ context "when a status response argument is provided" do
13
+
14
+ it "has a response entry for the argument" do
15
+ expect(subject).to include(status: fixture.status)
16
+ end
17
+
18
+ end
19
+
20
+ context "when no status response argument is provided" do
21
+
22
+ before(:example) { fixture.status = nil }
23
+
24
+ it "has a response entry with an empty status code" do
25
+ expect(subject).to include(status: "")
26
+ end
27
+
28
+ end
29
+
30
+ context "when response headers are provided" do
31
+
32
+ it "has a headers response entry containing the the provided headers" do
33
+ expect(subject).to include(headers: fixture.headers)
34
+ end
35
+
36
+ end
37
+
38
+ context "when response headers are omitted" do
39
+
40
+ before(:example) { fixture.headers = nil }
41
+
42
+ it "has a headers response entry containing an empty hash" do
43
+ expect(subject).to include(headers: {})
44
+ end
45
+
46
+ end
47
+
48
+ it "has an entry for the response body argument" do
49
+ expect(subject).to include(body: fixture.body)
50
+ end
51
+
52
+ context "when a delay option is provided" do
53
+
54
+ it "has a response entry for the argument" do
55
+ expect(subject).to include(delay_in_seconds: fixture.delay_in_seconds)
56
+ end
57
+
58
+ end
59
+
60
+ context "when a delay option is omitted" do
61
+
62
+ before(:example) { fixture.delay_in_seconds = nil }
63
+
64
+ it "has a response entry with an empty delay" do
65
+ expect(subject).to include(delay_in_seconds: "")
66
+ end
67
+
68
+ end
69
+
70
+ end
71
+
72
+ describe "#file" do
73
+
74
+ subject { stub_response.file }
75
+
76
+ context "when the body contains a file hash" do
77
+
78
+ let(:content_type) { "some-content-type" }
79
+ let(:file_path) { "some/file/path" }
80
+ let(:file_name) { "some_file.name" }
81
+
82
+ before(:example) do
83
+ fixture.headers = { "content-type" => content_type }
84
+ fixture.body = { file: { path: file_path, name: file_name } }
85
+ end
86
+
87
+ it "creates a response file containing the stub id" do
88
+ expect_response_file_to_be_created_with(id: stub_fixture.id)
89
+
90
+ subject
91
+ end
92
+
93
+ it "creates a response file containing the provided file path" do
94
+ expect_response_file_to_be_created_with(path: file_path)
95
+
96
+ subject
97
+ end
98
+
99
+ it "creates a response file containing the provided file name" do
100
+ expect_response_file_to_be_created_with(name: file_name)
101
+
102
+ subject
103
+ end
104
+
105
+ it "creates a response file whose type is the content-type response header" do
106
+ expect_response_file_to_be_created_with(type: content_type)
107
+
108
+ subject
109
+ end
110
+
111
+ it "returns the created response file" do
112
+ response_file = instance_double(HttpStub::Configurer::Request::StubResponseFile)
113
+ allow(HttpStub::Configurer::Request::StubResponseFile).to receive(:new).and_return(response_file)
114
+
115
+ expect(subject).to eql(response_file)
116
+ end
117
+
118
+ def expect_response_file_to_be_created_with(hash)
119
+ expect(HttpStub::Configurer::Request::StubResponseFile).to receive(:new).with(hash_including(hash))
120
+ end
121
+
122
+ end
123
+
124
+ context "when the body contains text" do
125
+
126
+ before(:example) { fixture.body = "some text" }
127
+
128
+ it "returns nil" do
129
+ expect(subject).to be(nil)
130
+ end
131
+
132
+ end
133
+
134
+ end
135
+
136
+ end