http_stub 0.7.0 → 0.7.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (27) hide show
  1. data/lib/http_stub.rb +6 -2
  2. data/lib/http_stub/configurer.rb +2 -2
  3. data/lib/http_stub/configurer/request/hash_with_regexpable_values.rb +22 -0
  4. data/lib/http_stub/configurer/request/regexpable_value.rb +18 -0
  5. data/lib/http_stub/configurer/request/stub.rb +26 -0
  6. data/lib/http_stub/configurer/request/stub_activator.rb +18 -0
  7. data/lib/http_stub/models/hash_with_regexpable_values.rb +20 -0
  8. data/lib/http_stub/models/regexpable_value.rb +22 -0
  9. data/lib/http_stub/models/stub_headers.rb +3 -3
  10. data/lib/http_stub/models/stub_parameters.rb +3 -3
  11. data/lib/http_stub/models/stub_uri.rb +3 -4
  12. data/lib/http_stub/version.rb +1 -1
  13. data/spec/lib/http_stub/configurer/request/hash_with_regexpable_values_spec.rb +35 -0
  14. data/spec/lib/http_stub/configurer/request/regexpable_value_spec.rb +55 -0
  15. data/spec/lib/http_stub/configurer/{stub_activator_request_spec.rb → request/stub_activator_spec.rb} +4 -4
  16. data/spec/lib/http_stub/configurer/request/stub_spec.rb +152 -0
  17. data/spec/lib/http_stub/configurer_integration_spec.rb +102 -26
  18. data/spec/lib/http_stub/models/hash_with_regexpable_values_spec.rb +149 -0
  19. data/spec/lib/http_stub/models/regexpable_value_spec.rb +69 -0
  20. data/spec/lib/http_stub/models/stub_headers_spec.rb +34 -86
  21. data/spec/lib/http_stub/models/stub_parameters_spec.rb +22 -11
  22. data/spec/lib/http_stub/models/stub_uri_spec.rb +16 -47
  23. data/spec/spec_helper.rb +1 -1
  24. metadata +21 -9
  25. data/lib/http_stub/configurer/stub_activator_request.rb +0 -16
  26. data/lib/http_stub/configurer/stub_request.rb +0 -24
  27. data/spec/lib/http_stub/configurer/stub_request_spec.rb +0 -129
data/lib/http_stub.rb CHANGED
@@ -11,6 +11,8 @@ require 'json'
11
11
 
12
12
  require File.expand_path('../http_stub/hash_extensions', __FILE__)
13
13
  require File.expand_path('../http_stub/models/response', __FILE__)
14
+ require File.expand_path('../http_stub/models/regexpable_value', __FILE__)
15
+ require File.expand_path('../http_stub/models/hash_with_regexpable_values', __FILE__)
14
16
  require File.expand_path('../http_stub/models/request_header_parser', __FILE__)
15
17
  require File.expand_path('../http_stub/models/stub_uri', __FILE__)
16
18
  require File.expand_path('../http_stub/models/stub_headers', __FILE__)
@@ -21,8 +23,10 @@ require File.expand_path('../http_stub/models/registry', __FILE__)
21
23
  require File.expand_path('../http_stub/controllers/stub_controller', __FILE__)
22
24
  require File.expand_path('../http_stub/controllers/stub_activator_controller', __FILE__)
23
25
  require File.expand_path('../http_stub/server', __FILE__)
24
- require File.expand_path('../http_stub/configurer/stub_request', __FILE__)
25
- require File.expand_path('../http_stub/configurer/stub_activator_request', __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__)
28
+ require File.expand_path('../http_stub/configurer/request/stub', __FILE__)
29
+ require File.expand_path('../http_stub/configurer/request/stub_activator', __FILE__)
26
30
  require File.expand_path('../http_stub/configurer/command', __FILE__)
27
31
  require File.expand_path('../http_stub/configurer/patient_command_chain', __FILE__)
28
32
  require File.expand_path('../http_stub/configurer/impatient_command_chain', __FILE__)
@@ -18,12 +18,12 @@ module HttpStub
18
18
  end
19
19
 
20
20
  def stub_activator(activation_uri, stub_uri, options)
21
- request = HttpStub::Configurer::StubActivatorRequest.new(activation_uri, stub_uri, options)
21
+ request = HttpStub::Configurer::Request::StubActivator.new(activation_uri, stub_uri, options)
22
22
  handle(request: request, description: "registering activator '#{activation_uri}'")
23
23
  end
24
24
 
25
25
  def stub!(uri, options)
26
- request = HttpStub::Configurer::StubRequest.new(uri, options)
26
+ request = HttpStub::Configurer::Request::Stub.new(uri, options)
27
27
  handle(request: request, description: "stubbing '#{uri}'", resetable: true)
28
28
  end
29
29
 
@@ -0,0 +1,22 @@
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
@@ -0,0 +1,18 @@
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
@@ -0,0 +1,26 @@
1
+ module HttpStub
2
+ module Configurer
3
+ module Request
4
+
5
+ class Stub < Net::HTTP::Post
6
+
7
+ def initialize(uri, options)
8
+ super("/stubs")
9
+ self.content_type = "application/json"
10
+ self.body = {
11
+ "uri" => HttpStub::Configurer::Request::RegexpableValue.new(uri),
12
+ "method" => options[:method],
13
+ "headers" => HttpStub::Configurer::Request::HashWithRegexpableValues.new(options[:headers] || {}),
14
+ "parameters" => HttpStub::Configurer::Request::HashWithRegexpableValues.new(options[:parameters] || {}),
15
+ "response" => {
16
+ "status" => options[:response][:status] || 200,
17
+ "body" => options[:response][:body]
18
+ }
19
+ }.to_json
20
+ end
21
+
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,18 @@
1
+ module HttpStub
2
+ module Configurer
3
+ module Request
4
+
5
+ class StubActivator < Net::HTTP::Post
6
+
7
+ def initialize(activation_uri, stub_uri, options)
8
+ super("/stubs/activators")
9
+ stub_request = HttpStub::Configurer::Request::Stub.new(stub_uri, options)
10
+ self.content_type = stub_request.content_type
11
+ self.body = JSON.parse(stub_request.body).merge({ "activation_uri" => activation_uri }).to_json
12
+ end
13
+
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,20 @@
1
+ module HttpStub
2
+ module Models
3
+
4
+ class HashWithRegexpableValues < Hash
5
+
6
+ def initialize(hash)
7
+ hash.each { |entry| self[entry[0]] = HttpStub::Models::RegexpableValue.new(entry[1]) }
8
+ end
9
+
10
+ def match?(other_hash)
11
+ !(self.find do |key_and_regexpable_value|
12
+ other_value = other_hash[key_and_regexpable_value[0]]
13
+ !(other_value && key_and_regexpable_value[1].match?(other_value))
14
+ end)
15
+ end
16
+
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,22 @@
1
+ module HttpStub
2
+ module Models
3
+
4
+ class RegexpableValue
5
+
6
+ def initialize(value)
7
+ @value = value
8
+ end
9
+
10
+ def match?(other_value)
11
+ match_data = @value.match(/^regexp:(.*)/)
12
+ match_data ? !!Regexp.new(match_data[1]).match(other_value) : other_value == @value
13
+ end
14
+
15
+ def to_s
16
+ @value
17
+ end
18
+
19
+ end
20
+
21
+ end
22
+ end
@@ -4,15 +4,15 @@ module HttpStub
4
4
  class StubHeaders
5
5
 
6
6
  def initialize(headers)
7
- @headers = headers || {}
7
+ @headers = HttpStub::Models::HashWithRegexpableValues.new((headers || {}).downcase_and_underscore_keys)
8
8
  end
9
9
 
10
10
  def match?(request)
11
- headers_in(request).downcase_and_underscore_keys.has_hash?(@headers.downcase_and_underscore_keys)
11
+ @headers.match?(headers_in(request).downcase_and_underscore_keys)
12
12
  end
13
13
 
14
14
  def to_s
15
- @headers ? @headers.map { |key_and_value| key_and_value.join(":") }.join(", ") : ""
15
+ @headers ? @headers.map { |key_and_value| key_and_value.map(&:to_s).join(":") }.join(", ") : ""
16
16
  end
17
17
 
18
18
  private
@@ -4,15 +4,15 @@ module HttpStub
4
4
  class StubParameters
5
5
 
6
6
  def initialize(parameters)
7
- @parameters = parameters
7
+ @parameters = HttpStub::Models::HashWithRegexpableValues.new(parameters || {})
8
8
  end
9
9
 
10
10
  def match?(request)
11
- request.params.has_hash?(@parameters)
11
+ @parameters.match?(request.params)
12
12
  end
13
13
 
14
14
  def to_s
15
- @parameters ? @parameters.map { |key_and_value| key_and_value.join("=") }.join("&") : ""
15
+ @parameters ? @parameters.map { |key_and_value| key_and_value.map(&:to_s).join("=") }.join("&") : ""
16
16
  end
17
17
 
18
18
  end
@@ -4,16 +4,15 @@ module HttpStub
4
4
  class StubUri
5
5
 
6
6
  def initialize(uri)
7
- @uri = uri
7
+ @uri = HttpStub::Models::RegexpableValue.new(uri)
8
8
  end
9
9
 
10
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
11
+ @uri.match?(request.path_info)
13
12
  end
14
13
 
15
14
  def to_s
16
- @uri
15
+ @uri.to_s
17
16
  end
18
17
 
19
18
  end
@@ -1,3 +1,3 @@
1
1
  module HttpStub
2
- VERSION = "0.7.0"
2
+ VERSION = "0.7.1"
3
3
  end
@@ -0,0 +1,35 @@
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
@@ -0,0 +1,55 @@
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
@@ -1,4 +1,4 @@
1
- describe HttpStub::Configurer::StubActivatorRequest do
1
+ describe HttpStub::Configurer::Request::StubActivator do
2
2
 
3
3
  describe "#initialize" do
4
4
 
@@ -6,7 +6,7 @@ describe HttpStub::Configurer::StubActivatorRequest do
6
6
  let(:stub_request_content_type) { "Some content type" }
7
7
  let(:stub_request) { double("StubRequest", :content_type => stub_request_content_type, :body => stub_request_body) }
8
8
 
9
- before(:each) { HttpStub::Configurer::StubRequest.stub!(:new).and_return(stub_request) }
9
+ before(:each) { HttpStub::Configurer::Request::Stub.stub!(:new).and_return(stub_request) }
10
10
 
11
11
  describe "when provided an activation uri, stub uri and stub options" do
12
12
 
@@ -14,7 +14,7 @@ describe HttpStub::Configurer::StubActivatorRequest do
14
14
  let(:stub_uri) { "Some stub URI" }
15
15
  let(:stub_options) { "Some options" }
16
16
 
17
- let(:request) { HttpStub::Configurer::StubActivatorRequest.new(activation_uri, stub_uri, stub_options) }
17
+ let(:request) { HttpStub::Configurer::Request::StubActivator.new(activation_uri, stub_uri, stub_options) }
18
18
  let(:request_body) { JSON.parse(request.body) }
19
19
 
20
20
  it "should create a HTTP POST request" do
@@ -32,7 +32,7 @@ describe HttpStub::Configurer::StubActivatorRequest do
32
32
  describe "generates a JSON body which" do
33
33
 
34
34
  it "should contain entries for a stub request" do
35
- HttpStub::Configurer::StubRequest.should_receive(:new).with(stub_uri, stub_options).and_return(stub_request)
35
+ HttpStub::Configurer::Request::Stub.should_receive(:new).with(stub_uri, stub_options).and_return(stub_request)
36
36
 
37
37
  request_body.should include(JSON.parse(stub_request_body))
38
38
  end
@@ -0,0 +1,152 @@
1
+ describe HttpStub::Configurer::Request::Stub do
2
+
3
+ describe "#initialize" do
4
+
5
+ describe "when provided a uri and stub options" do
6
+
7
+ let(:uri) { "/some/uri" }
8
+ let(:stub_method) { "Some Method" }
9
+ let(:headers) { { "header_name" => "value" } }
10
+ let(:parameters) { { "parameter_name" => "value" } }
11
+ let(:response_status) { 500 }
12
+ let(:response_body) { "Some body" }
13
+
14
+ let(:stub_options) do
15
+ {
16
+ method: stub_method,
17
+ headers: headers,
18
+ parameters: parameters,
19
+ response: {
20
+ status: response_status,
21
+ body: response_body
22
+ }
23
+ }
24
+ end
25
+
26
+ let(:request) { HttpStub::Configurer::Request::Stub.new(uri, stub_options) }
27
+ let(:request_body) { JSON.parse(request.body) }
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
34
+
35
+ it "should create a HTTP POST request" do
36
+ request.method.should eql("POST")
37
+ end
38
+
39
+ it "should submit the request to '/stubs'" do
40
+ request.path.should eql("/stubs")
41
+ end
42
+
43
+ it "should set the content type to json" do
44
+ request.content_type.should eql("application/json")
45
+ end
46
+
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
+ describe "when a header option is provided" do
54
+
55
+ it "should create a regexpable representation of the headers" do
56
+ HttpStub::Configurer::Request::HashWithRegexpableValues.should_receive(:new).with(headers)
57
+
58
+ request
59
+ end
60
+
61
+ end
62
+
63
+ describe "when no header is provided" do
64
+
65
+ let(:headers) { nil }
66
+
67
+ it "should create a regexpable representation of an empty header hash" do
68
+ HttpStub::Configurer::Request::HashWithRegexpableValues.should_receive(:new).with({})
69
+
70
+ request
71
+ end
72
+
73
+ end
74
+
75
+ describe "when a parameter option is provided" do
76
+
77
+ it "should create a regexpable representation of the parameters" do
78
+ HttpStub::Configurer::Request::HashWithRegexpableValues.should_receive(:new).with(parameters)
79
+
80
+ request
81
+ end
82
+
83
+ end
84
+
85
+ describe "when no parameter option is provided" do
86
+
87
+ let(:parameters) { nil }
88
+
89
+ it "should create a regexpable representation of an empty parameter hash" do
90
+ HttpStub::Configurer::Request::HashWithRegexpableValues.should_receive(:new).with({})
91
+
92
+ request
93
+ end
94
+
95
+ end
96
+
97
+ describe "generates a JSON body which" do
98
+
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)
102
+
103
+ request_body.should include({ "uri" => "uri as a string" })
104
+ end
105
+
106
+ it "should have an entry for the method option" do
107
+ request_body.should include({ "method" => stub_method })
108
+ end
109
+
110
+ 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)
113
+
114
+ request_body.should include({ "headers" => "headers as string" })
115
+ end
116
+
117
+ 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)
120
+
121
+ request_body.should include({ "parameters" => "parameters as string" })
122
+ end
123
+
124
+ describe "when a status response option is provided" do
125
+
126
+ it "should have a response entry for the option" do
127
+ request_body["response"].should include({ "status" => response_status })
128
+ end
129
+
130
+ end
131
+
132
+ describe "when no status response option is provided" do
133
+
134
+ let(:response_status) { nil }
135
+
136
+ it "should have a response entry with status code of 200" do
137
+ request_body["response"].should include({ "status" => 200 })
138
+ end
139
+
140
+ end
141
+
142
+ it "should have an entry for the response body option" do
143
+ request_body["response"].should include({ "body" => response_body })
144
+ end
145
+
146
+ end
147
+
148
+ end
149
+
150
+ end
151
+
152
+ end