http_stub 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/http_stub.rb +2 -0
- data/lib/http_stub/configurer.rb +2 -0
- data/lib/http_stub/hash_extensions.rb +22 -0
- data/lib/http_stub/models/headers.rb +31 -0
- data/lib/http_stub/models/parameters.rb +2 -4
- data/lib/http_stub/models/stub.rb +6 -6
- data/lib/http_stub/server.rb +4 -0
- data/lib/http_stub/version.rb +1 -1
- data/lib/http_stub/views/_stub.haml +3 -0
- data/spec/lib/http_stub/configurer_integration_spec.rb +34 -4
- data/spec/lib/http_stub/hash_extensions_spec.rb +143 -0
- data/spec/lib/http_stub/models/headers_spec.rb +153 -0
- data/spec/lib/http_stub/models/parameters_spec.rb +15 -101
- data/spec/lib/http_stub/models/stub_spec.rb +71 -20
- data/spec/lib/http_stub/server_integration_spec.rb +10 -2
- data/spec/spec_helper.rb +2 -1
- metadata +9 -3
data/lib/http_stub.rb
CHANGED
@@ -9,7 +9,9 @@ require 'sass'
|
|
9
9
|
require 'net/http'
|
10
10
|
require 'json'
|
11
11
|
|
12
|
+
require File.expand_path('../http_stub/hash_extensions', __FILE__)
|
12
13
|
require File.expand_path('../http_stub/response', __FILE__)
|
14
|
+
require File.expand_path('../http_stub/models/headers', __FILE__)
|
13
15
|
require File.expand_path('../http_stub/models/parameters', __FILE__)
|
14
16
|
require File.expand_path('../http_stub/models/stub', __FILE__)
|
15
17
|
require File.expand_path('../http_stub/models/stub_activator', __FILE__)
|
data/lib/http_stub/configurer.rb
CHANGED
@@ -25,6 +25,7 @@ module HttpStub
|
|
25
25
|
"activation_uri" => activation_uri,
|
26
26
|
"uri" => stub_uri,
|
27
27
|
"method" => options[:method],
|
28
|
+
"headers" => options[:headers] || {},
|
28
29
|
"parameters" => options[:parameters] || {},
|
29
30
|
"response" => {
|
30
31
|
"status" => response_options[:status] || "200",
|
@@ -68,6 +69,7 @@ module HttpStub
|
|
68
69
|
request.body = {
|
69
70
|
"uri" => uri,
|
70
71
|
"method" => options[:method],
|
72
|
+
"headers" => options[:headers] || {},
|
71
73
|
"parameters" => options[:parameters] || {},
|
72
74
|
"response" => {
|
73
75
|
"status" => response_options[:status] || "200",
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module HttpStub
|
2
|
+
|
3
|
+
module HashExtensions
|
4
|
+
|
5
|
+
def downcase_keys
|
6
|
+
self.reduce({}) do |result, element|
|
7
|
+
result[element[0].is_a?(::String) ? element[0].downcase : element[0]] = element[1]
|
8
|
+
result
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def has_hash?(other_hash)
|
13
|
+
other_hash.nil? || other_hash.reduce(true) do |result, element|
|
14
|
+
result && (self[element[0]] == element[1])
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
::Hash.send(:include, HttpStub::HashExtensions)
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module HttpStub
|
2
|
+
module Models
|
3
|
+
|
4
|
+
class Headers
|
5
|
+
|
6
|
+
def initialize(headers)
|
7
|
+
@headers = headers || {}
|
8
|
+
end
|
9
|
+
|
10
|
+
def match?(request)
|
11
|
+
headers_in(request).downcase_keys.has_hash?(@headers.downcase_keys)
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_s
|
15
|
+
@headers ? @headers.map { |key_and_value| key_and_value.join(":") }.join(", ") : ""
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def headers_in(request)
|
21
|
+
request.env.reduce({}) do |result, element|
|
22
|
+
match = element[0].match(/^HTTP_(.*)/)
|
23
|
+
result[match[1]] = element[1] if match
|
24
|
+
result
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
@@ -8,13 +8,11 @@ module HttpStub
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def match?(request)
|
11
|
-
|
12
|
-
result && (request.params[parameter[0]] == parameter[1])
|
13
|
-
end
|
11
|
+
request.params.has_hash?(@parameters)
|
14
12
|
end
|
15
13
|
|
16
14
|
def to_s
|
17
|
-
@parameters ? @parameters.map { |
|
15
|
+
@parameters ? @parameters.map { |key_and_value| key_and_value.join("=") }.join("&") : ""
|
18
16
|
end
|
19
17
|
|
20
18
|
end
|
@@ -3,16 +3,20 @@ module HttpStub
|
|
3
3
|
|
4
4
|
class Stub
|
5
5
|
|
6
|
-
attr_reader :response
|
6
|
+
attr_reader :headers, :parameters, :response
|
7
7
|
|
8
8
|
def initialize(options)
|
9
9
|
@stub_options = options
|
10
|
+
@headers = HttpStub::Models::Headers.new(options["headers"])
|
10
11
|
@parameters = HttpStub::Models::Parameters.new(options["parameters"])
|
11
12
|
@response = HttpStub::Response.new(options["response"])
|
12
13
|
end
|
13
14
|
|
14
15
|
def satisfies?(request)
|
15
|
-
uri == request.path_info &&
|
16
|
+
uri == request.path_info &&
|
17
|
+
method.downcase == request.request_method.downcase &&
|
18
|
+
@headers.match?(request) &&
|
19
|
+
@parameters.match?(request)
|
16
20
|
end
|
17
21
|
|
18
22
|
def uri
|
@@ -23,10 +27,6 @@ module HttpStub
|
|
23
27
|
@stub_options["method"]
|
24
28
|
end
|
25
29
|
|
26
|
-
def parameters
|
27
|
-
@parameters
|
28
|
-
end
|
29
|
-
|
30
30
|
def to_s
|
31
31
|
@stub_options.to_s
|
32
32
|
end
|
data/lib/http_stub/server.rb
CHANGED
data/lib/http_stub/version.rb
CHANGED
@@ -61,7 +61,7 @@ describe HttpStub::Configurer, "when the server is running" do
|
|
61
61
|
|
62
62
|
describe "when a response for a request is stubbed" do
|
63
63
|
|
64
|
-
describe "that contains no parameters" do
|
64
|
+
describe "that contains no headers or parameters" do
|
65
65
|
|
66
66
|
before(:each) do
|
67
67
|
configurer.stub_response!("/path2", method: :get, response: { status: 201, body: "Stub body" })
|
@@ -96,16 +96,46 @@ describe HttpStub::Configurer, "when the server is running" do
|
|
96
96
|
|
97
97
|
end
|
98
98
|
|
99
|
+
describe "that contains headers" do
|
100
|
+
|
101
|
+
before(:each) do
|
102
|
+
configurer.stub_response!("/path3", method: :get, headers: { key: "value" },
|
103
|
+
response: { status: 202, body: "Another stub body" })
|
104
|
+
end
|
105
|
+
|
106
|
+
describe "and that request is made" do
|
107
|
+
|
108
|
+
let(:response) { HTTParty.get("http://localhost:8001/path3", headers: { "key" => "value" }) }
|
109
|
+
|
110
|
+
it "should replay the stubbed response" do
|
111
|
+
response.code.should eql(202)
|
112
|
+
response.body.should eql("Another stub body")
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|
116
|
+
|
117
|
+
describe "and a request with different headers is made" do
|
118
|
+
|
119
|
+
let(:response) { HTTParty.get("http://localhost:8001/path3", headers: { "key" => "another_value" }) }
|
120
|
+
|
121
|
+
it "should respond with a 404 status code" do
|
122
|
+
response.code.should eql(404)
|
123
|
+
end
|
124
|
+
|
125
|
+
end
|
126
|
+
|
127
|
+
end
|
128
|
+
|
99
129
|
describe "that contains parameters" do
|
100
130
|
|
101
131
|
before(:each) do
|
102
|
-
configurer.stub_response!("/
|
132
|
+
configurer.stub_response!("/path4", method: :get, parameters: { key: "value" },
|
103
133
|
response: { status: 202, body: "Another stub body" })
|
104
134
|
end
|
105
135
|
|
106
136
|
describe "and that request is made" do
|
107
137
|
|
108
|
-
let(:response) { Net::HTTP.get_response("localhost", "/
|
138
|
+
let(:response) { Net::HTTP.get_response("localhost", "/path4?key=value", 8001) }
|
109
139
|
|
110
140
|
it "should replay the stubbed response" do
|
111
141
|
response.code.should eql("202")
|
@@ -116,7 +146,7 @@ describe HttpStub::Configurer, "when the server is running" do
|
|
116
146
|
|
117
147
|
describe "and a request with different parameters is made" do
|
118
148
|
|
119
|
-
let(:response) { Net::HTTP.get_response("localhost", "/
|
149
|
+
let(:response) { Net::HTTP.get_response("localhost", "/path4?key=another_value", 8001) }
|
120
150
|
|
121
151
|
it "should respond with a 404 status code" do
|
122
152
|
response.code.should eql("404")
|
@@ -0,0 +1,143 @@
|
|
1
|
+
describe HttpStub::HashExtensions do
|
2
|
+
|
3
|
+
describe "#downcase_keys" do
|
4
|
+
|
5
|
+
describe "when the hash contains keys which are strings" do
|
6
|
+
|
7
|
+
let(:hash) do
|
8
|
+
{ "lower" => 1, "UPPER" => 2, "MiXeDcAsE" => 3 }
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should downcase the string keys" do
|
12
|
+
hash.downcase_keys.should eql({ "lower" => 1, "upper" => 2, "mixedcase" => 3 })
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "when the hash contains keys which are not strings" do
|
18
|
+
|
19
|
+
let(:hash) do
|
20
|
+
{ 1 => 2, :symbol => 3, nil => 4 }
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should not alter a hash" do
|
24
|
+
hash.downcase_keys.should eql({ 1 => 2, :symbol => 3, nil => 4 })
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "#has_hash?" do
|
32
|
+
|
33
|
+
describe "when many elements are provided" do
|
34
|
+
|
35
|
+
let(:hash_parameter) { { "key1" => "value1", "key2" => "value2", "key3" => "value3" } }
|
36
|
+
|
37
|
+
describe "and the hash contains only those elements" do
|
38
|
+
|
39
|
+
let(:hash) { hash_parameter }
|
40
|
+
|
41
|
+
it "should return true" do
|
42
|
+
hash.has_hash?(hash_parameter).should be(true)
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
describe "and the hash contains more than those elements" do
|
48
|
+
|
49
|
+
let(:hash) { hash_parameter.merge("key4" => "value4") }
|
50
|
+
|
51
|
+
it "should return true" do
|
52
|
+
hash.has_hash?(hash_parameter).should be(true)
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
describe "and the hash contains keys that match but values that do not match" do
|
58
|
+
|
59
|
+
let(:hash) { { "key1" => "value1", "key2" => "nonMatchingValue", "key3" => "value3" } }
|
60
|
+
|
61
|
+
it "should return false" do
|
62
|
+
hash.has_hash?(hash_parameter).should be(false)
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
describe "and the hash contains less than those elements" do
|
68
|
+
|
69
|
+
let(:hash) { { "key1" => "value1", "key3" => "value3" } }
|
70
|
+
|
71
|
+
it "should return false" do
|
72
|
+
hash.has_hash?(hash_parameter).should be(false)
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
describe "and the hash contains no elements" do
|
78
|
+
|
79
|
+
let(:hash) { {} }
|
80
|
+
|
81
|
+
it "should return false" do
|
82
|
+
hash.has_hash?(hash_parameter).should be(false)
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
|
89
|
+
describe "when no elements are provided" do
|
90
|
+
|
91
|
+
let(:hash_parameter) { {} }
|
92
|
+
|
93
|
+
describe "and the hash contains no element" do
|
94
|
+
|
95
|
+
let(:hash) { {} }
|
96
|
+
|
97
|
+
it "should return true" do
|
98
|
+
hash.has_hash?(hash_parameter).should be(true)
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
102
|
+
|
103
|
+
describe "when the hash contains elements" do
|
104
|
+
|
105
|
+
let(:hash) { { "key" => "value" } }
|
106
|
+
|
107
|
+
it "should return true" do
|
108
|
+
hash.has_hash?(hash_parameter).should be(true)
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
114
|
+
|
115
|
+
describe "when a nil hash is provided" do
|
116
|
+
|
117
|
+
let(:hash_parameter) { nil }
|
118
|
+
|
119
|
+
describe "and the hash contains no elements" do
|
120
|
+
|
121
|
+
let(:hash) { {} }
|
122
|
+
|
123
|
+
it "should return true" do
|
124
|
+
hash.has_hash?(hash_parameter).should be(true)
|
125
|
+
end
|
126
|
+
|
127
|
+
end
|
128
|
+
|
129
|
+
describe "when the hash contains elements" do
|
130
|
+
|
131
|
+
let(:hash) { { "key" => "value" } }
|
132
|
+
|
133
|
+
it "should return true" do
|
134
|
+
hash.has_hash?(hash_parameter).should be(true)
|
135
|
+
end
|
136
|
+
|
137
|
+
end
|
138
|
+
|
139
|
+
end
|
140
|
+
|
141
|
+
end
|
142
|
+
|
143
|
+
end
|
@@ -0,0 +1,153 @@
|
|
1
|
+
describe HttpStub::Models::Headers do
|
2
|
+
|
3
|
+
let(:non_http_env_elements) do
|
4
|
+
{
|
5
|
+
"GATEWAY_INTERFACE" => "CGI/1.1",
|
6
|
+
"QUERY_STRING" => "some string",
|
7
|
+
"REMOTE_ADDR" => "127.0.0.1",
|
8
|
+
"SCRIPT_NAME" => "some script",
|
9
|
+
"SERVER_NAME" => "localhost",
|
10
|
+
}
|
11
|
+
end
|
12
|
+
let(:env) { non_http_env_elements.merge(request_headers) }
|
13
|
+
let(:request) { double("HttpRequest", env: env) }
|
14
|
+
|
15
|
+
let(:model) { HttpStub::Models::Headers.new(mandatory_headers) }
|
16
|
+
|
17
|
+
describe "#match?" do
|
18
|
+
|
19
|
+
describe "when multiple headers are mandatory" do
|
20
|
+
|
21
|
+
let(:mandatory_headers) { { "KEY1" => "value1", "KEY2" => "value2", "KEY3" => "value3" } }
|
22
|
+
|
23
|
+
describe "and the mandatory headers are provided" do
|
24
|
+
|
25
|
+
let(:request_headers) { { "HTTP_KEY1" => "value1", "HTTP_KEY2" => "value2", "HTTP_KEY3" => "value3" } }
|
26
|
+
|
27
|
+
describe "and the casing of the header names is identical" do
|
28
|
+
|
29
|
+
it "should return true" do
|
30
|
+
model.match?(request).should be_true
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
describe "and the casing of the header names is different" do
|
36
|
+
|
37
|
+
let(:mandatory_headers) { { "key1" => "value1", "KEY2" => "value2", "key3" => "value3" } }
|
38
|
+
|
39
|
+
it "should return true" do
|
40
|
+
model.match?(request).should be_true
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
describe "and the request headers have different values" do
|
48
|
+
|
49
|
+
let(:request_headers) { { "HTTP_KEY1" => "value1", "HTTP_KEY2" => "doesNotMatch", "HTTP_KEY3" => "value3" } }
|
50
|
+
|
51
|
+
it "should return false" do
|
52
|
+
model.match?(request).should be_false
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
describe "and some mandatory headers are omitted" do
|
58
|
+
|
59
|
+
let(:request_headers) { { "HTTP_KEY1" => "value1", "HTTP_KEY3" => "value3" } }
|
60
|
+
|
61
|
+
it "should return false" do
|
62
|
+
model.match?(request).should be_false
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
describe "and all mandatory headers are omitted" do
|
68
|
+
|
69
|
+
let(:request_headers) { {} }
|
70
|
+
|
71
|
+
it "should return false" do
|
72
|
+
model.match?(request).should be_false
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
describe "when no headers are mandatory" do
|
80
|
+
|
81
|
+
let(:mandatory_headers) { {} }
|
82
|
+
|
83
|
+
describe "and headers are provided" do
|
84
|
+
|
85
|
+
let(:request_headers) { { "HTTP_KEY" => "value" } }
|
86
|
+
|
87
|
+
it "should return true" do
|
88
|
+
model.match?(request).should be_true
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
94
|
+
|
95
|
+
describe "when the mandatory headers are nil" do
|
96
|
+
|
97
|
+
let(:mandatory_headers) { nil }
|
98
|
+
|
99
|
+
describe "and headers are provided" do
|
100
|
+
|
101
|
+
let(:request_headers) { { "HTTP_KEY" => "value" } }
|
102
|
+
|
103
|
+
it "should return true" do
|
104
|
+
model.match?(request).should be_true
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
112
|
+
|
113
|
+
describe "#to_s" do
|
114
|
+
|
115
|
+
describe "when multiple headers are provided" do
|
116
|
+
|
117
|
+
let(:mandatory_headers) { { "key1" => "value1", "key2" => "value2", "key3" => "value3" } }
|
118
|
+
|
119
|
+
it "should return a string containing each header formatted as a conventional request header" do
|
120
|
+
result = model.to_s
|
121
|
+
|
122
|
+
mandatory_headers.each { |key, value| result.should match(/#{key}:#{value}/) }
|
123
|
+
end
|
124
|
+
|
125
|
+
it "should comma delimit the headers" do
|
126
|
+
model.to_s.should match(/key\d.value\d\, key\d.value\d\, key\d.value\d/)
|
127
|
+
end
|
128
|
+
|
129
|
+
end
|
130
|
+
|
131
|
+
describe "when empty headers are provided" do
|
132
|
+
|
133
|
+
let(:mandatory_headers) { {} }
|
134
|
+
|
135
|
+
it "should return an empty string" do
|
136
|
+
model.to_s.should eql("")
|
137
|
+
end
|
138
|
+
|
139
|
+
end
|
140
|
+
|
141
|
+
describe "when nil headers are provided" do
|
142
|
+
|
143
|
+
let(:mandatory_headers) { nil }
|
144
|
+
|
145
|
+
it "should return an empty string" do
|
146
|
+
model.to_s.should eql("")
|
147
|
+
end
|
148
|
+
|
149
|
+
end
|
150
|
+
|
151
|
+
end
|
152
|
+
|
153
|
+
end
|
@@ -1,115 +1,29 @@
|
|
1
1
|
describe HttpStub::Models::Parameters do
|
2
2
|
|
3
|
+
let(:request_params) { double("RequestParameters") }
|
3
4
|
let(:request) { double("HttpRequest", params: request_params) }
|
4
5
|
|
5
|
-
let(:
|
6
|
+
let(:params) { { "key1" => "value1", "key2" => "value2", "key3" => "value3" } }
|
7
|
+
let(:model) { HttpStub::Models::Parameters.new(params) }
|
6
8
|
|
7
9
|
describe "#match?" do
|
8
10
|
|
9
|
-
describe "when
|
10
|
-
|
11
|
-
let(:params) { { "key1" => "value1", "key2" => "value2", "key3" => "value3" } }
|
12
|
-
|
13
|
-
describe "and the request contains only those parameters" do
|
14
|
-
|
15
|
-
let(:request_params) { params }
|
16
|
-
|
17
|
-
it "should return true" do
|
18
|
-
parameters.match?(request).should be(true)
|
19
|
-
end
|
20
|
-
|
21
|
-
end
|
22
|
-
|
23
|
-
describe "and the request contains more than those parameters" do
|
24
|
-
|
25
|
-
let(:request_params) { params.merge("key4" => "value4") }
|
26
|
-
|
27
|
-
it "should return true" do
|
28
|
-
parameters.match?(request).should be(true)
|
29
|
-
end
|
30
|
-
|
31
|
-
end
|
11
|
+
describe "when the request parameters contain the model parameters" do
|
32
12
|
|
33
|
-
|
34
|
-
|
35
|
-
let(:request_params) { { "key1" => "value1", "key2" => "nonMatchingValue", "key3" => "value3" } }
|
36
|
-
|
37
|
-
it "should return false" do
|
38
|
-
parameters.match?(request).should be(false)
|
39
|
-
end
|
40
|
-
|
41
|
-
end
|
42
|
-
|
43
|
-
describe "and the request contains a subset of those parameters" do
|
44
|
-
|
45
|
-
let(:request_params) { { "key1" => "value1", "key3" => "value3" } }
|
46
|
-
|
47
|
-
it "should return false" do
|
48
|
-
parameters.match?(request).should be(false)
|
49
|
-
end
|
50
|
-
|
51
|
-
end
|
52
|
-
|
53
|
-
describe "and the request contains no parameters" do
|
54
|
-
|
55
|
-
let(:request_params) { {} }
|
56
|
-
|
57
|
-
it "should return false" do
|
58
|
-
parameters.match?(request).should be(false)
|
59
|
-
end
|
13
|
+
before(:each) { request_params.stub!(:has_hash?).with(params).and_return(true) }
|
60
14
|
|
15
|
+
it "should return true" do
|
16
|
+
model.match?(request).should be(true)
|
61
17
|
end
|
62
18
|
|
63
19
|
end
|
64
20
|
|
65
|
-
describe "when
|
66
|
-
|
67
|
-
let(:params) { {} }
|
68
|
-
|
69
|
-
describe "when the request contains no parameters" do
|
70
|
-
|
71
|
-
let(:request_params) { {} }
|
72
|
-
|
73
|
-
it "should return true" do
|
74
|
-
parameters.match?(request).should be(true)
|
75
|
-
end
|
76
|
-
|
77
|
-
end
|
78
|
-
|
79
|
-
describe "when the request contains parameters" do
|
80
|
-
|
81
|
-
let(:request_params) { { "key" => "value" } }
|
82
|
-
|
83
|
-
it "should return true" do
|
84
|
-
parameters.match?(request).should be(true)
|
85
|
-
end
|
86
|
-
|
87
|
-
end
|
88
|
-
|
89
|
-
end
|
90
|
-
|
91
|
-
describe "when nil parameters are provided" do
|
92
|
-
|
93
|
-
let(:params) { nil }
|
94
|
-
|
95
|
-
describe "when the request contains no parameters" do
|
96
|
-
|
97
|
-
let(:request_params) { {} }
|
98
|
-
|
99
|
-
it "should return true" do
|
100
|
-
parameters.match?(request).should be(true)
|
101
|
-
end
|
102
|
-
|
103
|
-
end
|
104
|
-
|
105
|
-
describe "when the request contains parameters" do
|
106
|
-
|
107
|
-
let(:request_params) { { "key" => "value" } }
|
21
|
+
describe "when the request parameters do not contain the model parameters" do
|
108
22
|
|
109
|
-
|
110
|
-
parameters.match?(request).should be(true)
|
111
|
-
end
|
23
|
+
before(:each) { request_params.stub!(:has_hash?).with(params).and_return(false) }
|
112
24
|
|
25
|
+
it "should return false" do
|
26
|
+
model.match?(request).should be(false)
|
113
27
|
end
|
114
28
|
|
115
29
|
end
|
@@ -123,13 +37,13 @@ describe HttpStub::Models::Parameters do
|
|
123
37
|
let(:params) { { "key1" => "value1", "key2" => "value2", "key3" => "value3" } }
|
124
38
|
|
125
39
|
it "should return a string containing each parameter formatted as a conventional request parameter" do
|
126
|
-
result =
|
40
|
+
result = model.to_s
|
127
41
|
|
128
42
|
params.each { |key, value| result.should match(/#{key}=#{value}/) }
|
129
43
|
end
|
130
44
|
|
131
45
|
it "should separate each parameter with the conventional request parameter delimiter" do
|
132
|
-
|
46
|
+
model.to_s.should match(/key\d.value\d\&key\d.value\d\&key\d.value\d/)
|
133
47
|
end
|
134
48
|
|
135
49
|
end
|
@@ -139,7 +53,7 @@ describe HttpStub::Models::Parameters do
|
|
139
53
|
let(:params) { {} }
|
140
54
|
|
141
55
|
it "should return an empty string" do
|
142
|
-
|
56
|
+
model.to_s.should eql("")
|
143
57
|
end
|
144
58
|
|
145
59
|
end
|
@@ -149,7 +63,7 @@ describe HttpStub::Models::Parameters do
|
|
149
63
|
let(:params) { nil }
|
150
64
|
|
151
65
|
it "should return an empty string" do
|
152
|
-
|
66
|
+
model.to_s.should eql("")
|
153
67
|
end
|
154
68
|
|
155
69
|
end
|
@@ -3,13 +3,17 @@ describe HttpStub::Models::Stub do
|
|
3
3
|
let(:stub_uri) { "/a_path" }
|
4
4
|
let(:stub_method) { "get" }
|
5
5
|
let(:stub_parameters) do
|
6
|
-
{ "param" => "
|
6
|
+
{ "param" => "param_value" }
|
7
|
+
end
|
8
|
+
let(:stub_headers) do
|
9
|
+
{ "header" => "header_value" }
|
7
10
|
end
|
8
11
|
let(:stub_options) do
|
9
12
|
{
|
10
13
|
"uri" => stub_uri,
|
11
14
|
"method" => stub_method,
|
12
15
|
"parameters" => stub_parameters,
|
16
|
+
"headers" => stub_headers,
|
13
17
|
"response" => {
|
14
18
|
"status" => 201,
|
15
19
|
"body" => "Foo"
|
@@ -18,8 +22,12 @@ describe HttpStub::Models::Stub do
|
|
18
22
|
end
|
19
23
|
let(:the_stub) { HttpStub::Models::Stub.new(stub_options) }
|
20
24
|
let(:parameters_model) { double(HttpStub::Models::Parameters, match?: true) }
|
21
|
-
|
22
|
-
|
25
|
+
let(:headers_model) { double(HttpStub::Models::Headers, match?: true) }
|
26
|
+
|
27
|
+
before(:each) do
|
28
|
+
HttpStub::Models::Parameters.stub!(:new).and_return(parameters_model)
|
29
|
+
HttpStub::Models::Headers.stub!(:new).and_return(headers_model)
|
30
|
+
end
|
23
31
|
|
24
32
|
describe "#satisfies?" do
|
25
33
|
|
@@ -31,24 +39,34 @@ describe HttpStub::Models::Stub do
|
|
31
39
|
|
32
40
|
describe "and the request method matches" do
|
33
41
|
|
34
|
-
describe "and a
|
35
|
-
|
42
|
+
describe "and a header match is configured" do
|
43
|
+
|
36
44
|
describe "that matches" do
|
37
|
-
|
38
|
-
before(:each) { parameters_model.stub!(:match?).with(request).and_return(true) }
|
39
45
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
46
|
+
before(:each) { headers_model.stub!(:match?).with(request).and_return(true) }
|
47
|
+
|
48
|
+
describe "and a parameter match is configured" do
|
49
|
+
|
50
|
+
describe "that matches" do
|
51
|
+
|
52
|
+
before(:each) { parameters_model.stub!(:match?).with(request).and_return(true) }
|
47
53
|
|
48
|
-
|
54
|
+
it "should return true" do
|
55
|
+
the_stub.satisfies?(request).should be_true
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
describe "that does not match" do
|
61
|
+
|
62
|
+
before(:each) { parameters_model.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
|
49
69
|
|
50
|
-
it "should return false" do
|
51
|
-
the_stub.satisfies?(request).should be_false
|
52
70
|
end
|
53
71
|
|
54
72
|
end
|
@@ -79,6 +97,16 @@ describe HttpStub::Models::Stub do
|
|
79
97
|
|
80
98
|
end
|
81
99
|
|
100
|
+
describe "when the headers do not match" do
|
101
|
+
|
102
|
+
before(:each) { headers_model.stub!(:match?).with(request).and_return(false) }
|
103
|
+
|
104
|
+
it "should return false" do
|
105
|
+
the_stub.satisfies?(request).should be_false
|
106
|
+
end
|
107
|
+
|
108
|
+
end
|
109
|
+
|
82
110
|
end
|
83
111
|
|
84
112
|
describe "#uri" do
|
@@ -97,6 +125,14 @@ describe HttpStub::Models::Stub do
|
|
97
125
|
|
98
126
|
end
|
99
127
|
|
128
|
+
describe "#headers" do
|
129
|
+
|
130
|
+
it "should return the headers model encapsulating the headers provided in the request body" do
|
131
|
+
the_stub.headers.should eql(headers_model)
|
132
|
+
end
|
133
|
+
|
134
|
+
end
|
135
|
+
|
100
136
|
describe "#parameters" do
|
101
137
|
|
102
138
|
it "should return the parameters model encapsulating the parameters provided in the request body" do
|
@@ -119,11 +155,19 @@ describe HttpStub::Models::Stub do
|
|
119
155
|
|
120
156
|
describe "#to_s" do
|
121
157
|
|
158
|
+
let(:stub_headers) do
|
159
|
+
{
|
160
|
+
"header1" => "header_value1",
|
161
|
+
"header2" => "header_value2",
|
162
|
+
"header3" => "header_value3"
|
163
|
+
}
|
164
|
+
end
|
165
|
+
|
122
166
|
let(:stub_parameters) do
|
123
167
|
{
|
124
|
-
"param1" => "
|
125
|
-
"param2" => "
|
126
|
-
"param3" => "
|
168
|
+
"param1" => "param_value1",
|
169
|
+
"param2" => "param_value2",
|
170
|
+
"param3" => "param_value3"
|
127
171
|
}
|
128
172
|
end
|
129
173
|
|
@@ -135,6 +179,13 @@ describe HttpStub::Models::Stub do
|
|
135
179
|
the_stub.to_s.should match(/get/)
|
136
180
|
end
|
137
181
|
|
182
|
+
it "should return a string containing the stubbed headers" do
|
183
|
+
stub_headers.each_pair do |key, value|
|
184
|
+
the_stub.to_s.should match(/#{Regexp.escape(key)}/)
|
185
|
+
the_stub.to_s.should match(/#{Regexp.escape(value)}/)
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
138
189
|
it "should return a string containing the stubbed parameters" do
|
139
190
|
stub_parameters.each_pair do |key, value|
|
140
191
|
the_stub.to_s.should match(/#{Regexp.escape(key)}/)
|
@@ -27,8 +27,12 @@ describe HttpStub::Server, "when the server is running" do
|
|
27
27
|
(1..3).each { |i| response.body.should match(/#{escape_html("/path#{i}")}/) }
|
28
28
|
end
|
29
29
|
|
30
|
+
it "should return a response whose body contains the headers of each activators stub" do
|
31
|
+
(1..3).each { |i| response.body.should match(/header#{i}:header_value#{i}/) }
|
32
|
+
end
|
33
|
+
|
30
34
|
it "should return a response whose body contains the parameters of each activators stub" do
|
31
|
-
(1..3).each { |i| response.body.should match(/param#{i}=
|
35
|
+
(1..3).each { |i| response.body.should match(/param#{i}=param_value#{i}/) }
|
32
36
|
end
|
33
37
|
|
34
38
|
it "should return a response whose body contains the response status of each activators stub" do
|
@@ -62,8 +66,12 @@ describe HttpStub::Server, "when the server is running" do
|
|
62
66
|
(1..3).each { |i| response.body.should match(/#{escape_html("/path#{i}")}/) }
|
63
67
|
end
|
64
68
|
|
69
|
+
it "should return a response whose body contains the headers of each stub" do
|
70
|
+
(1..3).each { |i| response.body.should match(/header#{i}:header_value#{i}/) }
|
71
|
+
end
|
72
|
+
|
65
73
|
it "should return a response whose body contains the parameters of each stub" do
|
66
|
-
(1..3).each { |i| response.body.should match(/param#{i}=
|
74
|
+
(1..3).each { |i| response.body.should match(/param#{i}=param_value#{i}/) }
|
67
75
|
end
|
68
76
|
|
69
77
|
it "should return a response whose body contains the response status of each stub" do
|
data/spec/spec_helper.rb
CHANGED
@@ -1,11 +1,12 @@
|
|
1
1
|
require 'simplecov'
|
2
2
|
SimpleCov.start do
|
3
3
|
add_filter "/spec/"
|
4
|
-
minimum_coverage 96.
|
4
|
+
minimum_coverage 96.5
|
5
5
|
refuse_coverage_drop
|
6
6
|
end if ENV["coverage"]
|
7
7
|
|
8
8
|
require 'rack/test'
|
9
|
+
require 'httparty'
|
9
10
|
require 'nokogiri'
|
10
11
|
|
11
12
|
require File.expand_path('../../lib/http_stub/start_server_rake_task', __FILE__)
|
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.
|
4
|
+
version: 0.5.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2013-03-
|
13
|
+
date: 2013-03-08 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: sinatra
|
@@ -197,6 +197,8 @@ files:
|
|
197
197
|
- ./lib/http_stub/configurer.rb
|
198
198
|
- ./lib/http_stub/controllers/stub_activator_controller.rb
|
199
199
|
- ./lib/http_stub/controllers/stub_controller.rb
|
200
|
+
- ./lib/http_stub/hash_extensions.rb
|
201
|
+
- ./lib/http_stub/models/headers.rb
|
200
202
|
- ./lib/http_stub/models/parameters.rb
|
201
203
|
- ./lib/http_stub/models/registry.rb
|
202
204
|
- ./lib/http_stub/models/stub.rb
|
@@ -215,6 +217,8 @@ files:
|
|
215
217
|
- ./spec/lib/http_stub/configurer_integration_spec.rb
|
216
218
|
- ./spec/lib/http_stub/controllers/stub_activator_controller_spec.rb
|
217
219
|
- ./spec/lib/http_stub/controllers/stub_controller_spec.rb
|
220
|
+
- ./spec/lib/http_stub/hash_extensions_spec.rb
|
221
|
+
- ./spec/lib/http_stub/models/headers_spec.rb
|
218
222
|
- ./spec/lib/http_stub/models/parameters_spec.rb
|
219
223
|
- ./spec/lib/http_stub/models/registry_spec.rb
|
220
224
|
- ./spec/lib/http_stub/models/stub_activator_spec.rb
|
@@ -246,7 +250,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
246
250
|
version: '0'
|
247
251
|
segments:
|
248
252
|
- 0
|
249
|
-
hash:
|
253
|
+
hash: -2534473441515686622
|
250
254
|
requirements: []
|
251
255
|
rubyforge_project: http_stub
|
252
256
|
rubygems_version: 1.8.25
|
@@ -258,6 +262,8 @@ test_files:
|
|
258
262
|
- ./spec/lib/http_stub/configurer_integration_spec.rb
|
259
263
|
- ./spec/lib/http_stub/controllers/stub_activator_controller_spec.rb
|
260
264
|
- ./spec/lib/http_stub/controllers/stub_controller_spec.rb
|
265
|
+
- ./spec/lib/http_stub/hash_extensions_spec.rb
|
266
|
+
- ./spec/lib/http_stub/models/headers_spec.rb
|
261
267
|
- ./spec/lib/http_stub/models/parameters_spec.rb
|
262
268
|
- ./spec/lib/http_stub/models/registry_spec.rb
|
263
269
|
- ./spec/lib/http_stub/models/stub_activator_spec.rb
|