http_stub 0.17.0 → 0.18.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- MjE5ZmY4YmJhNjA3OGI2NWU1MWMxNmIzODk3OTQwMDI3NWZjOTY4Ng==
5
- data.tar.gz: !binary |-
6
- NTA4YjMxNzYyNGI4YTM3MzI1M2Y4YTRjYTUzOTIzZjgwMTBkODAxYQ==
2
+ SHA1:
3
+ metadata.gz: 7e6b513e163138fb09f45fcd3ad25690c0e49720
4
+ data.tar.gz: d01a275fbc644b47ca4c13f38082112ad99d9810
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- Mjk1NGI5YjE3Y2EwNjIxZmQwYzIyOGM5ZDU0ZDRhNzUzODAwNmMwYTA5NmY5
10
- YWI3YmQ1YTkxZDA0ODE1MDgzYmM1OGEyYWY5OTIyMDI4MmNmMjQ2MmI3MmQ4
11
- NGVkNjljOThkMTkyMTYzNjRkOWNiZTljNTQ0ZDIyZGJkZGUwYjM=
12
- data.tar.gz: !binary |-
13
- OGZkOTY0YWRlZDAwZjYwZGE0ODA0ZmE3YTEyNDA0ZWRmYmQzZTIwYzQwNjcw
14
- OTE0NmNhZmUwNWYxMjliZDBiZTdiYWRkNjc5YjExMDc0YjJmOThjNzQyMGRj
15
- YzAwZjU2N2NmMzk2ZGZjYThlNjZlOGU1YWQ2ZGYyMDE1NGI3OGQ=
6
+ metadata.gz: c73d28013f716113eeda47db419e4e4b7bd6c2c25152369ec9d9570c388e4fa7f3a8585b64314be7cca267aba2bdb11b73be2a29d492e9ab816a69544740cbd3
7
+ data.tar.gz: 4ee8e85dd25bd6c87ae6b501e6d4f9012d49d036c43c84897779e33725db7dc7eee9b71b1bd650cc01935d1665fd6029595f1784f1169baed93a292cdc27a66a
data/lib/http_stub.rb CHANGED
@@ -74,7 +74,9 @@ require_relative 'http_stub/configurer/dsl/stub_builder'
74
74
  require_relative 'http_stub/configurer/dsl/stub_builder_producer'
75
75
  require_relative 'http_stub/configurer/dsl/scenario_activator'
76
76
  require_relative 'http_stub/configurer/dsl/scenario_builder'
77
+ require_relative 'http_stub/configurer/dsl/endpoint_template'
77
78
  require_relative 'http_stub/configurer/dsl/stub_activator_builder'
78
79
  require_relative 'http_stub/configurer/dsl/server'
79
80
  require_relative 'http_stub/configurer/dsl/deprecated'
81
+ require_relative 'http_stub/configurer/part'
80
82
  require_relative 'http_stub/configurer'
@@ -21,6 +21,14 @@ module HttpStub
21
21
  @initialized = true
22
22
  end
23
23
 
24
+ def parts=(parts)
25
+ parts.each do |name, part|
26
+ part.configure(self)
27
+ self.define_singleton_method(name) { part }
28
+ self.send(:define_method, name) { part }
29
+ end
30
+ end
31
+
24
32
  private
25
33
 
26
34
  def server_facade
@@ -0,0 +1,26 @@
1
+ module HttpStub
2
+ module Configurer
3
+ module DSL
4
+
5
+ class EndpointTemplate
6
+
7
+ delegate :match_requests, :schema, :respond_with, :trigger, :invoke, to: :@default_stub_builder
8
+
9
+ def initialize(server, response_defaults)
10
+ @server = server
11
+ @default_stub_builder = HttpStub::Configurer::DSL::StubBuilder.new(response_defaults)
12
+ end
13
+
14
+ def add_scenario!(name, response_overrides={}, &block)
15
+ @server.add_one_stub_scenario!(name) do |stub_builder|
16
+ stub_builder.merge!(@default_stub_builder)
17
+ stub_builder.respond_with(response_overrides)
18
+ stub_builder.invoke(&block)
19
+ end
20
+ end
21
+
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -36,6 +36,18 @@ module HttpStub
36
36
  @server_facade.define_scenario(builder.build)
37
37
  end
38
38
 
39
+ def add_one_stub_scenario!(name, &block)
40
+ add_scenario!(name) do |scenario|
41
+ scenario.add_stub! { |stub| stub.invoke(&block) }
42
+ end
43
+ end
44
+
45
+ def endpoint_template(&block)
46
+ HttpStub::Configurer::DSL::EndpointTemplate.new(self, @response_defaults).tap do |template|
47
+ template.invoke(&block)
48
+ end
49
+ end
50
+
39
51
  def add_activator!(&block)
40
52
  builder = HttpStub::Configurer::DSL::StubActivatorBuilder.new(@response_defaults)
41
53
  block.call(builder)
@@ -3,6 +3,10 @@ module HttpStub
3
3
  module DSL
4
4
 
5
5
  class StubBuilder
6
+
7
+ attr_reader :request, :response, :triggers
8
+
9
+ public
6
10
 
7
11
  def initialize(response_defaults)
8
12
  @response = response_defaults ? response_defaults.clone : {}
@@ -28,6 +32,16 @@ module HttpStub
28
32
  self
29
33
  end
30
34
 
35
+ def invoke(&block)
36
+ block.arity == 0 ? self.instance_eval(&block) : block.call(self)
37
+ end
38
+
39
+ def merge!(stub_builder)
40
+ @request = (@request || {}).deep_merge(stub_builder.request || {})
41
+ self.respond_with(stub_builder.response)
42
+ self.trigger(stub_builder.triggers)
43
+ end
44
+
31
45
  def build
32
46
  HttpStub::Configurer::Request::Stub.new(request: @request, response: @response, triggers: @triggers)
33
47
  end
@@ -6,7 +6,7 @@ module HttpStub
6
6
 
7
7
  def build_stub(&block)
8
8
  builder = HttpStub::Configurer::DSL::StubBuilder.new(@response_defaults)
9
- block.arity == 0 ? builder.instance_eval(&block) : block.call(builder) if block_given?
9
+ builder.invoke(&block) if block_given?
10
10
  builder
11
11
  end
12
12
 
@@ -0,0 +1,19 @@
1
+ module HttpStub
2
+ module Configurer
3
+
4
+ module Part
5
+
6
+ def configure(configurer)
7
+ @configurer = configurer
8
+ configure_methods = self.methods.find_all { |method| method.to_s =~ /^configure_.+_(stub|scenario)s?$/ }
9
+ configure_methods.each { |configure_method| self.send(configure_method) }
10
+ end
11
+
12
+ def method_missing(name, *args, &block)
13
+ @configurer.send(name, *args, &block)
14
+ end
15
+
16
+ end
17
+
18
+ end
19
+ end
@@ -1,3 +1,3 @@
1
1
  module HttpStub
2
- VERSION = "0.17.0".freeze
2
+ VERSION = "0.18.0".freeze
3
3
  end
@@ -0,0 +1,66 @@
1
+ describe "Configurer Part acceptance" do
2
+ include_context "configurer integration"
3
+
4
+ context "when a configurer contains a part" do
5
+
6
+ let(:configurer) { HttpStub::Examples::ConfigurerWithParts.new }
7
+
8
+ before(:example) { configurer.class.initialize! }
9
+
10
+ context "that contains a configure stub method" do
11
+
12
+ let(:response) { issue_request("registered_in_configure_stub_method") }
13
+
14
+ it "invokes the method" do
15
+ expect(response.body).to eql("configure stub response")
16
+ end
17
+
18
+ end
19
+
20
+ context "that contains a configure stubs methods" do
21
+
22
+ let(:response) { issue_request("registered_in_configure_stubs_method") }
23
+
24
+ it "invokes the method" do
25
+ expect(response.body).to eql("configure stubs response")
26
+ end
27
+
28
+ end
29
+
30
+ context "that contains a configure scenario methods" do
31
+
32
+ let(:response) { issue_request("registered_in_configure_scenario_method") }
33
+
34
+ it "invokes the method" do
35
+ expect(response.body).to eql("configure scenario response")
36
+ end
37
+
38
+ end
39
+
40
+ context "that contains a configure scenarios methods" do
41
+
42
+ let(:response) { issue_request("registered_in_configure_scenarios_method") }
43
+
44
+ it "invokes the method" do
45
+ expect(response.body).to eql("configure scenarios response")
46
+ end
47
+
48
+ end
49
+
50
+ context "that contains a part referencing another part" do
51
+
52
+ let(:response) { issue_request("registered_in_another_part") }
53
+
54
+ it "resolves the other part successfully" do
55
+ expect(response.body).to eql("response from another part")
56
+ end
57
+
58
+ end
59
+
60
+ def issue_request(uri)
61
+ HTTParty.get("#{server_uri}/#{uri}")
62
+ end
63
+
64
+ end
65
+
66
+ end
@@ -0,0 +1,54 @@
1
+ describe "Endpoint Template acceptance" do
2
+ include_context "configurer integration"
3
+
4
+ context "when a configurer contains an endpoint template" do
5
+
6
+ let(:configurer) { HttpStub::Examples::ConfigurerWithEndpointTemplate.new }
7
+
8
+ before(:example) { configurer.class.initialize! }
9
+
10
+ it "does not register a stub when the template is defined" do
11
+ response = issue_request("template_uri")
12
+
13
+ expect(response.code).to eql(404)
14
+ end
15
+
16
+ context "and a templated scenario is activated" do
17
+
18
+ before(:example) { stub_server.activate!(scenario_name) }
19
+
20
+ context "that customises the request matching rules" do
21
+
22
+ let(:scenario_name) { "custom_request" }
23
+
24
+ let(:response) { issue_request("custom_uri") }
25
+
26
+ it "registers a templated stub for the scenario" do
27
+ expect(response.code).to eql(200)
28
+ expect(response.body).to eql("template body")
29
+ end
30
+
31
+ end
32
+
33
+ context "that customises the response" do
34
+
35
+ let(:scenario_name) { "custom_response" }
36
+
37
+ let(:response) { issue_request("template_uri") }
38
+
39
+ it "registers a templated stub for the scenario" do
40
+ expect(response.code).to eql(202)
41
+ expect(response.body).to eql("custom body")
42
+ end
43
+
44
+ end
45
+
46
+ end
47
+
48
+ end
49
+
50
+ def issue_request(uri)
51
+ HTTParty.get("#{server_uri}/#{uri}")
52
+ end
53
+
54
+ end
@@ -0,0 +1,163 @@
1
+ describe HttpStub::Configurer::DSL::EndpointTemplate do
2
+
3
+ let(:stub_fixture) { HttpStub::StubFixture.new }
4
+
5
+ let(:server) { instance_double(HttpStub::Configurer::DSL::Server) }
6
+ let(:response_defaults) { {} }
7
+ let(:default_stub_builder) { instance_double(HttpStub::Configurer::DSL::StubBuilder) }
8
+
9
+ let(:endpoint_template) { HttpStub::Configurer::DSL::EndpointTemplate.new(server, response_defaults) }
10
+
11
+ before(:example) do
12
+ allow(HttpStub::Configurer::DSL::StubBuilder).to(
13
+ receive(:new).with(response_defaults).and_return(default_stub_builder)
14
+ )
15
+ end
16
+
17
+ describe "#match_requests" do
18
+
19
+ let(:uri) { "/some/uri" }
20
+ let(:args) { { key: :value } }
21
+
22
+ subject { endpoint_template.match_requests(uri, args) }
23
+
24
+ it "delegates to the default stub builder" do
25
+ expect(default_stub_builder).to receive(:match_requests).with(uri, args)
26
+
27
+ subject
28
+ end
29
+
30
+ end
31
+
32
+ describe "#schema" do
33
+
34
+ let(:type) { :some_type }
35
+ let(:definition) { { key: :value } }
36
+
37
+ subject { endpoint_template.schema(type, definition) }
38
+
39
+ it "delegates to the default stub builder" do
40
+ expect(default_stub_builder).to receive(:schema).with(type, definition)
41
+
42
+ subject
43
+ end
44
+
45
+ end
46
+
47
+ describe "#respond_with" do
48
+
49
+ let(:args) { { status: 204 } }
50
+
51
+ subject { endpoint_template.respond_with(args) }
52
+
53
+ it "delegates to the default stub builder" do
54
+ expect(default_stub_builder).to receive(:respond_with).with(args)
55
+
56
+ subject
57
+ end
58
+
59
+ end
60
+
61
+ describe "#trigger" do
62
+
63
+ let(:trigger) { instance_double(HttpStub::Configurer::DSL::StubBuilder) }
64
+
65
+ subject { endpoint_template.trigger(trigger) }
66
+
67
+ it "delegates to the default stub builder" do
68
+ expect(default_stub_builder).to receive(:trigger).with(trigger)
69
+
70
+ subject
71
+ end
72
+
73
+ end
74
+
75
+ describe "#invoke" do
76
+
77
+ let(:block_verifier) { double("BlockVerifier") }
78
+ let(:block) { lambda { block_verifier.verify } }
79
+
80
+ subject { endpoint_template.invoke(&block) }
81
+
82
+ it "delegates to the default stub builder" do
83
+ expect(default_stub_builder).to receive(:invoke).and_yield
84
+ expect(block_verifier).to receive(:verify)
85
+
86
+ subject
87
+ end
88
+
89
+ end
90
+
91
+ describe "#add_scenario!" do
92
+
93
+ let(:name) { "some_scenario_name" }
94
+ let(:response_overrides) { {} }
95
+ let(:block_verifier) { double("BlockVerifier") }
96
+ let(:block) { lambda { block_verifier.verify } }
97
+
98
+ let(:stub_builder) { instance_double(HttpStub::Configurer::DSL::StubBuilder).as_null_object }
99
+
100
+ before(:example) { allow(server).to receive(:add_one_stub_scenario!).and_yield(stub_builder) }
101
+
102
+ subject { endpoint_template.add_scenario!(name, response_overrides, &block) }
103
+
104
+ it "add a one stub scenario to the server" do
105
+ expect(server).to receive(:add_one_stub_scenario!).with(name)
106
+
107
+ subject
108
+ end
109
+
110
+ it "merges the added stub with the default stub builder" do
111
+ expect(stub_builder).to receive(:merge!).with(default_stub_builder)
112
+
113
+ subject
114
+ end
115
+
116
+ context "when response overrides are provided" do
117
+
118
+ let(:response_overrides) { { status: 302 } }
119
+
120
+ it "informs the stub builder to respond with the response overrides" do
121
+ expect(stub_builder).to receive(:respond_with).with(response_overrides)
122
+
123
+ subject
124
+ end
125
+
126
+ end
127
+
128
+ context "when response overrides are not provided" do
129
+
130
+ subject { endpoint_template.add_scenario!(name, &block) }
131
+
132
+ it "does not change the stub builder by requesting it respond with an empty hash" do
133
+ expect(stub_builder).to receive(:respond_with).with({})
134
+
135
+ subject
136
+ end
137
+
138
+ end
139
+
140
+ it "merges the response defaults before applying the response overrides" do
141
+ expect(stub_builder).to receive(:merge!).ordered
142
+ expect(stub_builder).to receive(:respond_with).ordered
143
+
144
+ subject
145
+ end
146
+
147
+ it "requests the added stub builder invoke the provided block" do
148
+ expect(stub_builder).to receive(:invoke).and_yield
149
+ expect(block_verifier).to receive(:verify)
150
+
151
+ subject
152
+ end
153
+
154
+ it "applies the response overrides before invoking the provided block" do
155
+ expect(stub_builder).to receive(:respond_with).ordered
156
+ expect(stub_builder).to receive(:invoke).ordered
157
+
158
+ subject
159
+ end
160
+
161
+ end
162
+
163
+ end