http_stub 0.5.7 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/http_stub.rb CHANGED
@@ -12,6 +12,7 @@ require 'json'
12
12
  require File.expand_path('../http_stub/hash_extensions', __FILE__)
13
13
  require File.expand_path('../http_stub/models/response', __FILE__)
14
14
  require File.expand_path('../http_stub/models/request_header_parser', __FILE__)
15
+ require File.expand_path('../http_stub/models/stub_uri', __FILE__)
15
16
  require File.expand_path('../http_stub/models/stub_headers', __FILE__)
16
17
  require File.expand_path('../http_stub/models/stub_parameters', __FILE__)
17
18
  require File.expand_path('../http_stub/models/stub', __FILE__)
@@ -7,7 +7,7 @@ module HttpStub
7
7
  super("/stubs")
8
8
  self.content_type = "application/json"
9
9
  self.body = {
10
- "uri" => uri,
10
+ "uri" => uri.is_a?(Regexp) ? "regexp:#{uri.source.gsub(/\\/, "")}" : uri,
11
11
  "method" => options[:method],
12
12
  "headers" => options[:headers] || {},
13
13
  "parameters" => options[:parameters] || {},
@@ -3,26 +3,23 @@ module HttpStub
3
3
 
4
4
  class Stub
5
5
 
6
- attr_reader :headers, :parameters, :response
6
+ attr_reader :uri, :headers, :parameters, :response
7
7
 
8
8
  def initialize(options)
9
9
  @stub_options = options
10
+ @uri = HttpStub::Models::StubUri.new(options["uri"])
10
11
  @headers = HttpStub::Models::StubHeaders.new(options["headers"])
11
12
  @parameters = HttpStub::Models::StubParameters.new(options["parameters"])
12
13
  @response = HttpStub::Models::Response.new(options["response"])
13
14
  end
14
15
 
15
16
  def satisfies?(request)
16
- uri == request.path_info &&
17
+ @uri.match?(request) &&
17
18
  method.downcase == request.request_method.downcase &&
18
19
  @headers.match?(request) &&
19
20
  @parameters.match?(request)
20
21
  end
21
22
 
22
- def uri
23
- @stub_options["uri"]
24
- end
25
-
26
23
  def method
27
24
  @stub_options["method"]
28
25
  end
@@ -0,0 +1,22 @@
1
+ module HttpStub
2
+ module Models
3
+
4
+ class StubUri
5
+
6
+ def initialize(uri)
7
+ @uri = uri
8
+ end
9
+
10
+ def match?(request)
11
+ match_data = @uri.match(/^regexp:(.*)/)
12
+ match_data ? !!Regexp.new(match_data[1]).match(request.path_info) : request.path_info == @uri
13
+ end
14
+
15
+ def to_s
16
+ @uri
17
+ end
18
+
19
+ end
20
+
21
+ end
22
+ end
@@ -1,3 +1,3 @@
1
1
  module HttpStub
2
- VERSION = "0.5.7"
2
+ VERSION = "0.6.0"
3
3
  end
@@ -4,7 +4,7 @@ describe HttpStub::Configurer::StubRequest do
4
4
 
5
5
  describe "when provided a uri and stub options" do
6
6
 
7
- let(:uri) { "Some URI" }
7
+ let(:uri) { "/some/uri" }
8
8
  let(:stub_method) { "Some Method" }
9
9
  let(:headers) { { "header_name" => "value" } }
10
10
  let(:parameters) { { "parameter_name" => "value" } }
@@ -40,8 +40,22 @@ describe HttpStub::Configurer::StubRequest do
40
40
 
41
41
  describe "generates a JSON body which" do
42
42
 
43
- it "should have an entry for the provided URI" do
44
- request_body.should include({ "uri" => uri })
43
+ describe "when the uri is a string" do
44
+
45
+ it "should have an entry containing the provided URI unaltered" do
46
+ request_body.should include({ "uri" => uri })
47
+ end
48
+
49
+ end
50
+
51
+ describe "when the uri is a regular expression" do
52
+
53
+ let(:uri) { /\/some\/uri/ }
54
+
55
+ it "should have an entry containing the provided URI converted to a string and prefixed with 'regexp:'" do
56
+ request_body.should include({ "uri" => "regexp:/some/uri" })
57
+ end
58
+
45
59
  end
46
60
 
47
61
  it "should have an entry for the method option" do
@@ -135,6 +135,35 @@ describe HttpStub::Configurer, "when the server is running" do
135
135
 
136
136
  end
137
137
 
138
+ describe "and the stub uri is regular expression" do
139
+
140
+ before(:each) do
141
+ configurer.stub_response!(/\/stub\/regexp\/.*/, method: :get, response: { body: "Stub body" })
142
+ end
143
+
144
+ describe "and a request is made whose uri matches the regular expression" do
145
+
146
+ let(:response) { Net::HTTP.get_response("localhost", "/stub/regexp/match", 8001) }
147
+
148
+ it "should respond with the stubbed body" do
149
+ response.body.should eql("Stub body")
150
+ end
151
+
152
+ end
153
+
154
+ describe "and a request is made whose uri does not match the regular expression" do
155
+
156
+ let(:response) { Net::HTTP.get_response("localhost", "/stub/no_match/regexp", 8001) }
157
+
158
+ it "should respond with a 404 status code" do
159
+ response.code.should eql("404")
160
+ end
161
+
162
+ end
163
+
164
+
165
+ end
166
+
138
167
  end
139
168
 
140
169
  end
@@ -1,42 +1,54 @@
1
1
  describe HttpStub::Models::Stub do
2
2
 
3
- let(:stub_uri) { "/a_path" }
4
- let(:stub_method) { "get" }
5
- let(:stub_parameters) do
6
- { "param" => "param_value" }
3
+ let(:raw_stub_headers) do
4
+ {
5
+ "header1" => "header_value1",
6
+ "header2" => "header_value2",
7
+ "header3" => "header_value3"
8
+ }
7
9
  end
8
- let(:stub_headers) do
9
- { "header" => "header_value" }
10
+ let(:raw_stub_parameters) do
11
+ {
12
+ "param1" => "param_value1",
13
+ "param2" => "param_value2",
14
+ "param3" => "param_value3"
15
+ }
10
16
  end
17
+ let(:stub_method) { "get" }
11
18
  let(:stub_options) do
12
19
  {
13
- "uri" => stub_uri,
20
+ "uri" => "/a_path",
14
21
  "method" => stub_method,
15
- "parameters" => stub_parameters,
16
- "headers" => stub_headers,
22
+ "headers" => raw_stub_headers,
23
+ "parameters" => raw_stub_parameters,
17
24
  "response" => {
18
25
  "status" => 201,
19
26
  "body" => "Foo"
20
27
  }
21
28
  }
22
29
  end
23
- let(:the_stub) { HttpStub::Models::Stub.new(stub_options) }
30
+ let(:stub_uri) { double(HttpStub::Models::StubUri, match?: true) }
24
31
  let(:stub_parameters) { double(HttpStub::Models::StubParameters, match?: true) }
25
32
  let(:stub_headers) { double(HttpStub::Models::StubHeaders, match?: true) }
26
33
 
34
+ let(:the_stub) { HttpStub::Models::Stub.new(stub_options) }
35
+
27
36
  before(:each) do
37
+ HttpStub::Models::StubUri.stub!(:new).and_return(stub_uri)
28
38
  HttpStub::Models::StubParameters.stub!(:new).and_return(stub_parameters)
29
39
  HttpStub::Models::StubHeaders.stub!(:new).and_return(stub_headers)
30
40
  end
31
41
 
32
42
  describe "#satisfies?" do
33
43
 
34
- let(:request_uri) { stub_uri }
44
+ let(:request_uri) { "/a_request_uri" }
35
45
  let(:request_method) { stub_method }
36
- let(:request) { double("HttpRequest", :path_info => request_uri, :request_method => request_method) }
46
+ let(:request) { double("HttpRequest", :request_method => request_method) }
37
47
 
38
48
  describe "when the request uri matches" do
39
49
 
50
+ before(:each) { stub_uri.stub!(:match?).with(request).and_return(true) }
51
+
40
52
  describe "and the request method matches" do
41
53
 
42
54
  describe "and a header match is configured" do
@@ -57,16 +69,6 @@ describe HttpStub::Models::Stub do
57
69
 
58
70
  end
59
71
 
60
- describe "that does not match" do
61
-
62
- before(:each) { stub_parameters.stub!(:match?).with(request).and_return(false) }
63
-
64
- it "should return false" do
65
- the_stub.satisfies?(request).should be_false
66
- end
67
-
68
- end
69
-
70
72
  end
71
73
 
72
74
  end
@@ -79,7 +81,7 @@ describe HttpStub::Models::Stub do
79
81
 
80
82
  describe "when the request uri does not match" do
81
83
 
82
- let(:request_uri) { "/a_different_path" }
84
+ before(:each) { stub_uri.stub!(:match?).with(request).and_return(false) }
83
85
 
84
86
  it "should return false" do
85
87
  the_stub.satisfies?(request).should be_false
@@ -107,11 +109,21 @@ describe HttpStub::Models::Stub do
107
109
 
108
110
  end
109
111
 
112
+ describe "when the parameters do not match" do
113
+
114
+ before(:each) { stub_parameters.stub!(:match?).with(request).and_return(false) }
115
+
116
+ it "should return false" do
117
+ the_stub.satisfies?(request).should be_false
118
+ end
119
+
120
+ end
121
+
110
122
  end
111
123
 
112
124
  describe "#uri" do
113
125
 
114
- it "should return the value provided in the request body" do
126
+ it "should return the parameters model encapsulating the uri provided in the request body" do
115
127
  the_stub.uri.should eql(stub_uri)
116
128
  end
117
129
 
@@ -155,22 +167,6 @@ describe HttpStub::Models::Stub do
155
167
 
156
168
  describe "#to_s" do
157
169
 
158
- let(:stub_headers) do
159
- {
160
- "header1" => "header_value1",
161
- "header2" => "header_value2",
162
- "header3" => "header_value3"
163
- }
164
- end
165
-
166
- let(:stub_parameters) do
167
- {
168
- "param1" => "param_value1",
169
- "param2" => "param_value2",
170
- "param3" => "param_value3"
171
- }
172
- end
173
-
174
170
  it "should return a string containing the stubbed uri" do
175
171
  the_stub.to_s.should match(/\/a_path/)
176
172
  end
@@ -180,14 +176,14 @@ describe HttpStub::Models::Stub do
180
176
  end
181
177
 
182
178
  it "should return a string containing the stubbed headers" do
183
- stub_headers.each_pair do |key, value|
179
+ raw_stub_headers.each_pair do |key, value|
184
180
  the_stub.to_s.should match(/#{Regexp.escape(key)}/)
185
181
  the_stub.to_s.should match(/#{Regexp.escape(value)}/)
186
182
  end
187
183
  end
188
184
 
189
185
  it "should return a string containing the stubbed parameters" do
190
- stub_parameters.each_pair do |key, value|
186
+ raw_stub_parameters.each_pair do |key, value|
191
187
  the_stub.to_s.should match(/#{Regexp.escape(key)}/)
192
188
  the_stub.to_s.should match(/#{Regexp.escape(value)}/)
193
189
  end
@@ -0,0 +1,73 @@
1
+ describe HttpStub::Models::StubUri do
2
+
3
+ let(:request) { double("HttpRequest", path_info: request_uri) }
4
+
5
+ let(:stub_uri) { HttpStub::Models::StubUri.new(stubbed_uri) }
6
+
7
+ describe "#match?" do
8
+
9
+ describe "when the uri provided has no 'regexp' prefix" do
10
+
11
+ let(:stubbed_uri) { "/some/uri" }
12
+
13
+ describe "and the request uri is equal" do
14
+
15
+ let(:request_uri) { "/some/uri" }
16
+
17
+ it "should return true" do
18
+ stub_uri.match?(request).should be_true
19
+ end
20
+
21
+ end
22
+
23
+ describe "and the request uri is not equal" do
24
+
25
+ let(:request_uri) { "/some/other/uri" }
26
+
27
+ it "should return false" do
28
+ stub_uri.match?(request).should be_false
29
+ end
30
+
31
+ end
32
+
33
+ end
34
+
35
+ describe "when the uri provided has a 'regexp' prefix" do
36
+
37
+ let(:stubbed_uri) { "regexp:/some/regexp/uri/.+" }
38
+
39
+ describe "and the request uri matches the regular expression" do
40
+
41
+ let(:request_uri) { "/some/regexp/uri/match" }
42
+
43
+ it "should return true" do
44
+ stub_uri.match?(request).should be_true
45
+ end
46
+
47
+ end
48
+
49
+ describe "and the request uri does not match the regular expression" do
50
+
51
+ let(:request_uri) { "/some/other/uri" }
52
+
53
+ it "should return false" do
54
+ stub_uri.match?(request).should be_false
55
+ end
56
+
57
+ end
58
+
59
+ end
60
+
61
+ end
62
+
63
+ describe "#to_s" do
64
+
65
+ let(:stubbed_uri) { "some/uri" }
66
+
67
+ it "should return the uri" do
68
+ stub_uri.to_s.should eql(stubbed_uri)
69
+ end
70
+
71
+ end
72
+
73
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,13 +1,14 @@
1
1
  require 'simplecov'
2
2
  SimpleCov.start do
3
3
  add_filter "/spec/"
4
- minimum_coverage 97.1
4
+ minimum_coverage 97.2
5
5
  refuse_coverage_drop
6
6
  end if ENV["coverage"]
7
7
 
8
- require 'rack/test'
9
8
  require 'httparty'
10
9
  require 'nokogiri'
10
+ require 'rack/test'
11
+ require 'wait_until'
11
12
 
12
13
  require File.expand_path('../../lib/http_stub/rake/task_generators', __FILE__)
13
14
  require File.expand_path('../../lib/http_stub', __FILE__)
@@ -1,28 +1,12 @@
1
1
  shared_context "server integration" do
2
2
 
3
- FIVE_SECONDS = 5
4
-
5
3
  before(:all) do
6
4
  @pid = Process.spawn("rake start_example_server --trace")
7
- wait_until("http stub server started") { Net::HTTP.get_response("localhost", "/", 8001) }
5
+ ::Wait.until!("http stub server started") { Net::HTTP.get_response("localhost", "/", 8001) }
8
6
  end
9
7
 
10
8
  after(:all) do
11
9
  Process.kill(9, @pid)
12
10
  end
13
11
 
14
- def wait_until(description, &block)
15
- start_time = Time.now
16
- while true
17
- begin
18
- block.call
19
- return
20
- rescue
21
- if Time.now - start_time > FIVE_SECONDS
22
- raise "Timed out waiting until #{description}"
23
- end
24
- end
25
- end
26
- end
27
-
28
12
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http_stub
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.7
4
+ version: 0.6.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -93,13 +93,13 @@ dependencies:
93
93
  - !ruby/object:Gem::Version
94
94
  version: '2.12'
95
95
  - !ruby/object:Gem::Dependency
96
- name: rack-test
96
+ name: simplecov
97
97
  requirement: !ruby/object:Gem::Requirement
98
98
  none: false
99
99
  requirements:
100
100
  - - ~>
101
101
  - !ruby/object:Gem::Version
102
- version: 0.6.2
102
+ version: 0.7.1
103
103
  type: :development
104
104
  prerelease: false
105
105
  version_requirements: !ruby/object:Gem::Requirement
@@ -107,15 +107,15 @@ dependencies:
107
107
  requirements:
108
108
  - - ~>
109
109
  - !ruby/object:Gem::Version
110
- version: 0.6.2
110
+ version: 0.7.1
111
111
  - !ruby/object:Gem::Dependency
112
- name: nokogiri
112
+ name: flog
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  none: false
115
115
  requirements:
116
116
  - - ~>
117
117
  - !ruby/object:Gem::Version
118
- version: 1.5.6
118
+ version: 3.2.2
119
119
  type: :development
120
120
  prerelease: false
121
121
  version_requirements: !ruby/object:Gem::Requirement
@@ -123,7 +123,23 @@ dependencies:
123
123
  requirements:
124
124
  - - ~>
125
125
  - !ruby/object:Gem::Version
126
- version: 1.5.6
126
+ version: 3.2.2
127
+ - !ruby/object:Gem::Dependency
128
+ name: travis-lint
129
+ requirement: !ruby/object:Gem::Requirement
130
+ none: false
131
+ requirements:
132
+ - - ~>
133
+ - !ruby/object:Gem::Version
134
+ version: 1.6.0
135
+ type: :development
136
+ prerelease: false
137
+ version_requirements: !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ~>
141
+ - !ruby/object:Gem::Version
142
+ version: 1.6.0
127
143
  - !ruby/object:Gem::Dependency
128
144
  name: rake
129
145
  requirement: !ruby/object:Gem::Requirement
@@ -141,13 +157,13 @@ dependencies:
141
157
  - !ruby/object:Gem::Version
142
158
  version: 10.0.3
143
159
  - !ruby/object:Gem::Dependency
144
- name: simplecov
160
+ name: rack-test
145
161
  requirement: !ruby/object:Gem::Requirement
146
162
  none: false
147
163
  requirements:
148
164
  - - ~>
149
165
  - !ruby/object:Gem::Version
150
- version: 0.7.1
166
+ version: 0.6.2
151
167
  type: :development
152
168
  prerelease: false
153
169
  version_requirements: !ruby/object:Gem::Requirement
@@ -155,15 +171,15 @@ dependencies:
155
171
  requirements:
156
172
  - - ~>
157
173
  - !ruby/object:Gem::Version
158
- version: 0.7.1
174
+ version: 0.6.2
159
175
  - !ruby/object:Gem::Dependency
160
- name: flog
176
+ name: nokogiri
161
177
  requirement: !ruby/object:Gem::Requirement
162
178
  none: false
163
179
  requirements:
164
180
  - - ~>
165
181
  - !ruby/object:Gem::Version
166
- version: 3.2.2
182
+ version: 1.5.6
167
183
  type: :development
168
184
  prerelease: false
169
185
  version_requirements: !ruby/object:Gem::Requirement
@@ -171,15 +187,15 @@ dependencies:
171
187
  requirements:
172
188
  - - ~>
173
189
  - !ruby/object:Gem::Version
174
- version: 3.2.2
190
+ version: 1.5.6
175
191
  - !ruby/object:Gem::Dependency
176
- name: travis-lint
192
+ name: wait_until
177
193
  requirement: !ruby/object:Gem::Requirement
178
194
  none: false
179
195
  requirements:
180
196
  - - ~>
181
197
  - !ruby/object:Gem::Version
182
- version: 1.6.0
198
+ version: 0.0.1
183
199
  type: :development
184
200
  prerelease: false
185
201
  version_requirements: !ruby/object:Gem::Requirement
@@ -187,7 +203,7 @@ dependencies:
187
203
  requirements:
188
204
  - - ~>
189
205
  - !ruby/object:Gem::Version
190
- version: 1.6.0
206
+ version: 0.0.1
191
207
  description: fakeweb for a HTTP server, informing it to stub / fake responses.
192
208
  email: matthew.ueckerman@myob.com
193
209
  executables: []
@@ -210,6 +226,7 @@ files:
210
226
  - ./lib/http_stub/models/stub_activator.rb
211
227
  - ./lib/http_stub/models/stub_headers.rb
212
228
  - ./lib/http_stub/models/stub_parameters.rb
229
+ - ./lib/http_stub/models/stub_uri.rb
213
230
  - ./lib/http_stub/rake/start_server_task.rb
214
231
  - ./lib/http_stub/rake/task_generators.rb
215
232
  - ./lib/http_stub/server.rb
@@ -237,6 +254,7 @@ files:
237
254
  - ./spec/lib/http_stub/models/stub_headers_spec.rb
238
255
  - ./spec/lib/http_stub/models/stub_parameters_spec.rb
239
256
  - ./spec/lib/http_stub/models/stub_spec.rb
257
+ - ./spec/lib/http_stub/models/stub_uri_spec.rb
240
258
  - ./spec/lib/http_stub/rake/start_server_rake_integration_spec.rb
241
259
  - ./spec/lib/http_stub/server_integration_spec.rb
242
260
  - ./spec/lib/http_stub/server_spec.rb
@@ -263,7 +281,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
263
281
  version: '0'
264
282
  segments:
265
283
  - 0
266
- hash: 1104906034138605574
284
+ hash: 3857634377426384844
267
285
  requirements: []
268
286
  rubyforge_project: http_stub
269
287
  rubygems_version: 1.8.25
@@ -288,6 +306,7 @@ test_files:
288
306
  - ./spec/lib/http_stub/models/stub_headers_spec.rb
289
307
  - ./spec/lib/http_stub/models/stub_parameters_spec.rb
290
308
  - ./spec/lib/http_stub/models/stub_spec.rb
309
+ - ./spec/lib/http_stub/models/stub_uri_spec.rb
291
310
  - ./spec/lib/http_stub/rake/start_server_rake_integration_spec.rb
292
311
  - ./spec/lib/http_stub/server_integration_spec.rb
293
312
  - ./spec/lib/http_stub/server_spec.rb