http_stub 0.7.2 → 0.7.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/http_stub.rb CHANGED
@@ -23,8 +23,7 @@ require File.expand_path('../http_stub/models/registry', __FILE__)
23
23
  require File.expand_path('../http_stub/controllers/stub_controller', __FILE__)
24
24
  require File.expand_path('../http_stub/controllers/stub_activator_controller', __FILE__)
25
25
  require File.expand_path('../http_stub/server', __FILE__)
26
- require File.expand_path('../http_stub/configurer/request/regexpable_value', __FILE__)
27
- require File.expand_path('../http_stub/configurer/request/hash_with_regexpable_values', __FILE__)
26
+ require File.expand_path('../http_stub/configurer/request/regexpable', __FILE__)
28
27
  require File.expand_path('../http_stub/configurer/request/stub', __FILE__)
29
28
  require File.expand_path('../http_stub/configurer/request/stub_activator', __FILE__)
30
29
  require File.expand_path('../http_stub/configurer/command', __FILE__)
@@ -0,0 +1,43 @@
1
+ module HttpStub
2
+ module Configurer
3
+ module Request
4
+
5
+ class Regexpable
6
+
7
+ class << self
8
+
9
+ private
10
+
11
+ FORMATTERS = { String => :format_string, Regexp => :format_regexp, Hash => :format_hash }
12
+
13
+ public
14
+
15
+ def format(value)
16
+ formatter = FORMATTERS.find { |formatter_entry| value.is_a?(formatter_entry[0]) }
17
+ raise "No Regexp formatter found for #{value}" unless formatter
18
+ self.send(formatter[1], value)
19
+ end
20
+
21
+ private
22
+
23
+ def format_string(string)
24
+ string
25
+ end
26
+
27
+ def format_regexp(regexp)
28
+ "regexp:#{regexp.source.gsub(/\\/, "")}"
29
+ end
30
+
31
+ def format_hash(hash)
32
+ hash.reduce({}) do |result, entry|
33
+ result[entry[0]] = format(entry[1])
34
+ result
35
+ end
36
+ end
37
+
38
+ end
39
+
40
+ end
41
+ end
42
+ end
43
+ end
@@ -8,10 +8,10 @@ module HttpStub
8
8
  super("/stubs")
9
9
  self.content_type = "application/json"
10
10
  self.body = {
11
- "uri" => HttpStub::Configurer::Request::RegexpableValue.new(uri),
11
+ "uri" => HttpStub::Configurer::Request::Regexpable.format(uri),
12
12
  "method" => options[:method],
13
- "headers" => HttpStub::Configurer::Request::HashWithRegexpableValues.new(options[:headers] || {}),
14
- "parameters" => HttpStub::Configurer::Request::HashWithRegexpableValues.new(options[:parameters] || {}),
13
+ "headers" => HttpStub::Configurer::Request::Regexpable.format(options[:headers] || {}),
14
+ "parameters" => HttpStub::Configurer::Request::Regexpable.format(options[:parameters] || {}),
15
15
  "response" => {
16
16
  "status" => options[:response][:status] || 200,
17
17
  "body" => options[:response][:body]
@@ -1,3 +1,3 @@
1
1
  module HttpStub
2
- VERSION = "0.7.2"
2
+ VERSION = "0.7.3"
3
3
  end
@@ -0,0 +1,67 @@
1
+ describe HttpStub::Configurer::Request::Regexpable do
2
+
3
+ describe "#format" do
4
+
5
+ describe "when the value is a string" do
6
+
7
+ let(:value) { "some string value" }
8
+
9
+ it "should return the value unaltered" do
10
+ HttpStub::Configurer::Request::Regexpable.format(value).should eql(value)
11
+ end
12
+
13
+ end
14
+
15
+ describe "when the value is a regular expression" do
16
+
17
+ let(:value) { /.+?[a-z]/ }
18
+
19
+ it "should return a string prefixed by 'regexp:'" do
20
+ HttpStub::Configurer::Request::Regexpable.format(value).should start_with("regexp:")
21
+ end
22
+
23
+ it "should contain the regular expression converted to a string" do
24
+ HttpStub::Configurer::Request::Regexpable.format(value).should end_with(".+?[a-z]")
25
+ end
26
+
27
+ describe "and the regular expression contain path separators" do
28
+
29
+ let(:value) { /\/some\/path/ }
30
+
31
+ it "should represent the path separators as a single forward-slash" do
32
+ HttpStub::Configurer::Request::Regexpable.format(value).should end_with("/some/path")
33
+ end
34
+
35
+ end
36
+
37
+ end
38
+
39
+ describe "when the value is a hash" do
40
+
41
+ describe "and the hash has multiple entries" do
42
+
43
+ let(:value) { { key1: "value1", key2: /^.*value2$/, key3: "value3" } }
44
+
45
+ it "should return a json representation of the hash whose values are their regexpable form" do
46
+ expected_formatted_hash = {key1: "value1", key2: "regexp:^.*value2$", key3: "value3"}
47
+
48
+ HttpStub::Configurer::Request::Regexpable.format(value).should eql(expected_formatted_hash)
49
+ end
50
+
51
+ end
52
+
53
+ describe "and the hash is empty" do
54
+
55
+ let(:value) { {} }
56
+
57
+ it "should return an empty hash" do
58
+ HttpStub::Configurer::Request::Regexpable.format(value).should eql({})
59
+ end
60
+
61
+ end
62
+
63
+ end
64
+
65
+ end
66
+
67
+ end
@@ -26,11 +26,7 @@ describe HttpStub::Configurer::Request::Stub do
26
26
  let(:request) { HttpStub::Configurer::Request::Stub.new(uri, stub_options) }
27
27
  let(:request_body) { JSON.parse(request.body) }
28
28
 
29
- before(:each) do
30
- HttpStub::Configurer::Request::HashWithRegexpableValues.stub!(:new).and_return(
31
- double(HttpStub::Configurer::Request::HashWithRegexpableValues).as_null_object
32
- )
33
- end
29
+ before(:each) { HttpStub::Configurer::Request::Regexpable.stub!(:format) }
34
30
 
35
31
  it "should create a HTTP POST request" do
36
32
  request.method.should eql("POST")
@@ -44,16 +40,10 @@ describe HttpStub::Configurer::Request::Stub do
44
40
  request.content_type.should eql("application/json")
45
41
  end
46
42
 
47
- it "should create a regexpable representation of the uri" do
48
- HttpStub::Configurer::Request::RegexpableValue.should_receive(:new).with(uri)
49
-
50
- request
51
- end
52
-
53
43
  describe "when a header option is provided" do
54
44
 
55
- it "should create a regexpable representation of the headers" do
56
- HttpStub::Configurer::Request::HashWithRegexpableValues.should_receive(:new).with(headers)
45
+ it "should format the headers with regexp support" do
46
+ HttpStub::Configurer::Request::Regexpable.should_receive(:format).with(headers)
57
47
 
58
48
  request
59
49
  end
@@ -64,8 +54,8 @@ describe HttpStub::Configurer::Request::Stub do
64
54
 
65
55
  let(:headers) { nil }
66
56
 
67
- it "should create a regexpable representation of an empty header hash" do
68
- HttpStub::Configurer::Request::HashWithRegexpableValues.should_receive(:new).with({})
57
+ it "should format an empty header hash" do
58
+ HttpStub::Configurer::Request::Regexpable.should_receive(:format).with({})
69
59
 
70
60
  request
71
61
  end
@@ -74,8 +64,8 @@ describe HttpStub::Configurer::Request::Stub do
74
64
 
75
65
  describe "when a parameter option is provided" do
76
66
 
77
- it "should create a regexpable representation of the parameters" do
78
- HttpStub::Configurer::Request::HashWithRegexpableValues.should_receive(:new).with(parameters)
67
+ it "should format the parameters with regexp support" do
68
+ HttpStub::Configurer::Request::Regexpable.should_receive(:format).with(parameters)
79
69
 
80
70
  request
81
71
  end
@@ -86,8 +76,8 @@ describe HttpStub::Configurer::Request::Stub do
86
76
 
87
77
  let(:parameters) { nil }
88
78
 
89
- it "should create a regexpable representation of an empty parameter hash" do
90
- HttpStub::Configurer::Request::HashWithRegexpableValues.should_receive(:new).with({})
79
+ it "should format an empty parameter hash" do
80
+ HttpStub::Configurer::Request::Regexpable.should_receive(:format).with({})
91
81
 
92
82
  request
93
83
  end
@@ -96,9 +86,8 @@ describe HttpStub::Configurer::Request::Stub do
96
86
 
97
87
  describe "generates a JSON body which" do
98
88
 
99
- it "should have an entry containing the string representation of the uri" do
100
- uri_value = double(HttpStub::Configurer::Request::RegexpableValue, to_s: "uri as a string")
101
- HttpStub::Configurer::Request::RegexpableValue.stub!(:new).and_return(uri_value)
89
+ it "should have an entry containing the regexpable representation of the uri" do
90
+ HttpStub::Configurer::Request::Regexpable.should_receive(:format).with(uri).and_return("uri as a string")
102
91
 
103
92
  request_body.should include({ "uri" => "uri as a string" })
104
93
  end
@@ -108,15 +97,15 @@ describe HttpStub::Configurer::Request::Stub do
108
97
  end
109
98
 
110
99
  it "should have an entry containing the string representation of the headers" do
111
- hash_value = double(HttpStub::Configurer::Request::HashWithRegexpableValues, to_s: "headers as string")
112
- HttpStub::Configurer::Request::HashWithRegexpableValues.stub!(:new).with(headers).and_return(hash_value)
100
+ HttpStub::Configurer::Request::Regexpable.should_receive(:format)
101
+ .with(headers).and_return("headers as string")
113
102
 
114
103
  request_body.should include({ "headers" => "headers as string" })
115
104
  end
116
105
 
117
106
  it "should have an entry containing the string representation of the parameters" do
118
- hash_value = double(HttpStub::Configurer::Request::HashWithRegexpableValues, to_s: "parameters as string")
119
- HttpStub::Configurer::Request::HashWithRegexpableValues.stub!(:new).with(parameters).and_return(hash_value)
107
+ HttpStub::Configurer::Request::Regexpable.should_receive(:format)
108
+ .with(parameters).and_return("parameters as string")
120
109
 
121
110
  request_body.should include({ "parameters" => "parameters as string" })
122
111
  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.7.2
4
+ version: 0.7.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -213,8 +213,7 @@ files:
213
213
  - ./lib/http_stub/configurer/command.rb
214
214
  - ./lib/http_stub/configurer/impatient_command_chain.rb
215
215
  - ./lib/http_stub/configurer/patient_command_chain.rb
216
- - ./lib/http_stub/configurer/request/hash_with_regexpable_values.rb
217
- - ./lib/http_stub/configurer/request/regexpable_value.rb
216
+ - ./lib/http_stub/configurer/request/regexpable.rb
218
217
  - ./lib/http_stub/configurer/request/stub.rb
219
218
  - ./lib/http_stub/configurer/request/stub_activator.rb
220
219
  - ./lib/http_stub/configurer.rb
@@ -246,8 +245,7 @@ files:
246
245
  - ./spec/lib/http_stub/configurer/command_spec.rb
247
246
  - ./spec/lib/http_stub/configurer/impatient_command_chain_spec.rb
248
247
  - ./spec/lib/http_stub/configurer/patient_command_chain_spec.rb
249
- - ./spec/lib/http_stub/configurer/request/hash_with_regexpable_values_spec.rb
250
- - ./spec/lib/http_stub/configurer/request/regexpable_value_spec.rb
248
+ - ./spec/lib/http_stub/configurer/request/regexpable_spec.rb
251
249
  - ./spec/lib/http_stub/configurer/request/stub_activator_spec.rb
252
250
  - ./spec/lib/http_stub/configurer/request/stub_spec.rb
253
251
  - ./spec/lib/http_stub/configurer_integration_spec.rb
@@ -290,7 +288,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
290
288
  version: '0'
291
289
  segments:
292
290
  - 0
293
- hash: 1625561209117934809
291
+ hash: 2072585349946261127
294
292
  requirements: []
295
293
  rubyforge_project: http_stub
296
294
  rubygems_version: 1.8.25
@@ -303,8 +301,7 @@ test_files:
303
301
  - ./spec/lib/http_stub/configurer/command_spec.rb
304
302
  - ./spec/lib/http_stub/configurer/impatient_command_chain_spec.rb
305
303
  - ./spec/lib/http_stub/configurer/patient_command_chain_spec.rb
306
- - ./spec/lib/http_stub/configurer/request/hash_with_regexpable_values_spec.rb
307
- - ./spec/lib/http_stub/configurer/request/regexpable_value_spec.rb
304
+ - ./spec/lib/http_stub/configurer/request/regexpable_spec.rb
308
305
  - ./spec/lib/http_stub/configurer/request/stub_activator_spec.rb
309
306
  - ./spec/lib/http_stub/configurer/request/stub_spec.rb
310
307
  - ./spec/lib/http_stub/configurer_integration_spec.rb
@@ -1,22 +0,0 @@
1
- module HttpStub
2
- module Configurer
3
- module Request
4
-
5
- class HashWithRegexpableValues
6
-
7
- def initialize(hash)
8
- @hash = hash.reduce({}) do |result, entry|
9
- result[entry[0]] = HttpStub::Configurer::Request::RegexpableValue.new(entry[1])
10
- result
11
- end
12
- end
13
-
14
- def to_json(*args)
15
- @hash.to_json(args)
16
- end
17
-
18
- end
19
-
20
- end
21
- end
22
- end
@@ -1,18 +0,0 @@
1
- module HttpStub
2
- module Configurer
3
- module Request
4
-
5
- class RegexpableValue
6
-
7
- def initialize(value)
8
- @value = value
9
- end
10
-
11
- def to_s
12
- @value.is_a?(Regexp) ? "regexp:#{@value.source.gsub(/\\/, "")}" : @value
13
- end
14
-
15
- end
16
- end
17
- end
18
- end
@@ -1,35 +0,0 @@
1
- describe HttpStub::Configurer::Request::HashWithRegexpableValues do
2
-
3
- let(:hash_with_regexpable_values) { HttpStub::Configurer::Request::HashWithRegexpableValues.new(raw_hash) }
4
-
5
- describe "#to_json" do
6
-
7
- describe "when provided a hash with multiple entries" do
8
-
9
- let(:raw_hash) { { key1: "value1", key2: "value2", key3: "value3" } }
10
-
11
- it "should return a json representation of the hash whose values are their regexpable form" do
12
- raw_hash.values.each do |value|
13
- regexpable_value = double(HttpStub::Configurer::Request::RegexpableValue, to_s: "regexpable_#{value}")
14
- HttpStub::Configurer::Request::RegexpableValue.stub!(:new).with(value).and_return(regexpable_value)
15
- end
16
-
17
- expected_json = { key1: "regexpable_value1", key2: "regexpable_value2", key3: "regexpable_value3" }.to_json
18
- hash_with_regexpable_values.to_json.should eql(expected_json)
19
- end
20
-
21
- end
22
-
23
- describe "when provided an empty hash" do
24
-
25
- let(:raw_hash) { {} }
26
-
27
- it "should return a json representation of an empty hash" do
28
- hash_with_regexpable_values.to_json.should eql({}.to_json)
29
- end
30
-
31
- end
32
-
33
- end
34
-
35
- end
@@ -1,55 +0,0 @@
1
- describe HttpStub::Configurer::Request::RegexpableValue do
2
-
3
- let(:regexpable_value) { HttpStub::Configurer::Request::RegexpableValue.new(raw_value) }
4
-
5
- describe "#to_s" do
6
-
7
- describe "when the option's value is a regular expression" do
8
-
9
- let(:raw_value) { /.+?[a-z]/ }
10
-
11
- it "should return a string prefixed by 'regexp:'" do
12
- regexpable_value.to_s.should start_with("regexp:")
13
- end
14
-
15
- it "should contain the regular expression converted to a string" do
16
- regexpable_value.to_s.should end_with(".+?[a-z]")
17
- end
18
-
19
- describe "and the regular expression contain path separators" do
20
-
21
- let(:raw_value) { /\/some\/path/ }
22
-
23
- it "should represent the path separators as a single forward-slash" do
24
- regexpable_value.to_s.should end_with("/some/path")
25
- end
26
-
27
- end
28
-
29
- end
30
-
31
- describe "when the option's value is a string" do
32
-
33
- let(:raw_value) { "some string value" }
34
-
35
- it "should return the value unaltered" do
36
- regexpable_value.to_s.should eql(raw_value)
37
- end
38
-
39
- end
40
-
41
- end
42
-
43
- describe "#inspect" do
44
-
45
- let(:raw_value) { "some raw value" }
46
-
47
- it "should delegate to #to_s" do
48
- regexpable_value.should_receive(:to_s).and_return("some raw value as a string")
49
-
50
- regexpable_value.inspect.should eql("some raw value as a string")
51
- end
52
-
53
- end
54
-
55
- end