http_stub 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/http_stub.rb +1 -0
- data/lib/http_stub/models/parameters.rb +23 -0
- data/lib/http_stub/models/stub.rb +9 -15
- 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/lib/http_stub/views/aliases.haml +6 -12
- data/lib/http_stub/views/layout.haml +7 -0
- data/lib/http_stub/views/stubs.haml +3 -0
- data/spec/lib/http_stub/models/parameters_spec.rb +159 -0
- data/spec/lib/http_stub/models/stub_spec.rb +23 -66
- data/spec/lib/http_stub/server_integration_spec.rb +41 -0
- data/spec/spec_helper.rb +1 -1
- metadata +8 -3
data/lib/http_stub.rb
CHANGED
@@ -10,6 +10,7 @@ require 'net/http'
|
|
10
10
|
require 'json'
|
11
11
|
|
12
12
|
require File.expand_path('../http_stub/response', __FILE__)
|
13
|
+
require File.expand_path('../http_stub/models/parameters', __FILE__)
|
13
14
|
require File.expand_path('../http_stub/models/stub', __FILE__)
|
14
15
|
require File.expand_path('../http_stub/models/alias', __FILE__)
|
15
16
|
require File.expand_path('../http_stub/models/registry', __FILE__)
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module HttpStub
|
2
|
+
module Models
|
3
|
+
|
4
|
+
class Parameters
|
5
|
+
|
6
|
+
def initialize(parameters)
|
7
|
+
@parameters = parameters
|
8
|
+
end
|
9
|
+
|
10
|
+
def match?(request)
|
11
|
+
@parameters.nil? || @parameters.reduce(true) do |result, parameter|
|
12
|
+
result && (request.params[parameter[0]] == parameter[1])
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_s
|
17
|
+
@parameters ? @parameters.map { |param| "#{param[0]}=#{param[1]}" }.join("&") : ""
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
@@ -6,35 +6,29 @@ module HttpStub
|
|
6
6
|
attr_reader :response
|
7
7
|
|
8
8
|
def initialize(options)
|
9
|
-
@
|
9
|
+
@stub_options = options
|
10
|
+
@parameters = HttpStub::Models::Parameters.new(options["parameters"])
|
10
11
|
@response = HttpStub::Response.new(options["response"])
|
11
12
|
end
|
12
13
|
|
13
14
|
def satisfies?(request)
|
14
|
-
uri == request.path_info &&
|
15
|
-
method.downcase == request.request_method.downcase &&
|
16
|
-
parameters_match?(request)
|
15
|
+
uri == request.path_info && method.downcase == request.request_method.downcase && @parameters.match?(request)
|
17
16
|
end
|
18
17
|
|
19
18
|
def uri
|
20
|
-
@
|
19
|
+
@stub_options["uri"]
|
21
20
|
end
|
22
21
|
|
23
22
|
def method
|
24
|
-
@
|
23
|
+
@stub_options["method"]
|
25
24
|
end
|
26
25
|
|
27
|
-
def
|
28
|
-
@
|
26
|
+
def parameters
|
27
|
+
@parameters
|
29
28
|
end
|
30
29
|
|
31
|
-
|
32
|
-
|
33
|
-
def parameters_match?(request)
|
34
|
-
parameters = @alias_options["parameters"]
|
35
|
-
parameters.nil? || parameters.reduce(true) do |result, parameter|
|
36
|
-
result && (request.params[parameter[0]] == parameter[1])
|
37
|
-
end
|
30
|
+
def to_s
|
31
|
+
@stub_options.to_s
|
38
32
|
end
|
39
33
|
|
40
34
|
end
|
data/lib/http_stub/server.rb
CHANGED
data/lib/http_stub/version.rb
CHANGED
@@ -1,12 +1,6 @@
|
|
1
|
-
|
2
|
-
%
|
3
|
-
|
4
|
-
%
|
5
|
-
|
6
|
-
%
|
7
|
-
- aliases.each do |the_alias|
|
8
|
-
%div
|
9
|
-
%span Response Activation URI:
|
10
|
-
%a{ href: the_alias.alias_uri }= the_alias.alias_uri
|
11
|
-
= partial :stub, locals: { the_stub: the_alias.the_stub }
|
12
|
-
%br
|
1
|
+
- aliases.each do |the_alias|
|
2
|
+
%div
|
3
|
+
%span Response Activation URI:
|
4
|
+
%a{ href: the_alias.alias_uri }= the_alias.alias_uri
|
5
|
+
= partial :stub, locals: { the_stub: the_alias.the_stub }
|
6
|
+
%br
|
@@ -0,0 +1,159 @@
|
|
1
|
+
describe HttpStub::Models::Parameters do
|
2
|
+
|
3
|
+
let(:request) { double("HttpRequest", params: request_params) }
|
4
|
+
|
5
|
+
let(:parameters) { HttpStub::Models::Parameters.new(params) }
|
6
|
+
|
7
|
+
describe "#match?" do
|
8
|
+
|
9
|
+
describe "when multiple parameters are provided" do
|
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
|
32
|
+
|
33
|
+
describe "and the requests contains only those parameters with values that do not match" do
|
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
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
describe "when empty parameters are provided" do
|
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" } }
|
108
|
+
|
109
|
+
it "should return true" do
|
110
|
+
parameters.match?(request).should be(true)
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
118
|
+
|
119
|
+
describe "#to_s" do
|
120
|
+
|
121
|
+
describe "when multiple parameters are provided" do
|
122
|
+
|
123
|
+
let(:params) { { "key1" => "value1", "key2" => "value2", "key3" => "value3" } }
|
124
|
+
|
125
|
+
it "should return a string containing each parameter formatted as a conventional request parameter" do
|
126
|
+
result = parameters.to_s
|
127
|
+
|
128
|
+
params.each { |key, value| result.should match(/#{key}=#{value}/) }
|
129
|
+
end
|
130
|
+
|
131
|
+
it "should separate each parameter with the conventional request parameter delimiter" do
|
132
|
+
parameters.to_s.should match(/key\d=value\d\&key\d=value\d\&key\d=value\d/)
|
133
|
+
end
|
134
|
+
|
135
|
+
end
|
136
|
+
|
137
|
+
describe "when empty parameters are provided" do
|
138
|
+
|
139
|
+
let(:params) { {} }
|
140
|
+
|
141
|
+
it "should return an empty string" do
|
142
|
+
parameters.to_s.should eql("")
|
143
|
+
end
|
144
|
+
|
145
|
+
end
|
146
|
+
|
147
|
+
describe "when nil parameters are provided" do
|
148
|
+
|
149
|
+
let(:params) { nil }
|
150
|
+
|
151
|
+
it "should return an empty string" do
|
152
|
+
parameters.to_s.should eql("")
|
153
|
+
end
|
154
|
+
|
155
|
+
end
|
156
|
+
|
157
|
+
end
|
158
|
+
|
159
|
+
end
|
@@ -2,7 +2,9 @@ describe HttpStub::Models::Stub do
|
|
2
2
|
|
3
3
|
let(:stub_uri) { "/a_path" }
|
4
4
|
let(:stub_method) { "get" }
|
5
|
-
let(:stub_parameters)
|
5
|
+
let(:stub_parameters) do
|
6
|
+
{ "param" => "value" }
|
7
|
+
end
|
6
8
|
let(:stub_options) do
|
7
9
|
{
|
8
10
|
"uri" => stub_uri,
|
@@ -15,57 +17,35 @@ describe HttpStub::Models::Stub do
|
|
15
17
|
}
|
16
18
|
end
|
17
19
|
let(:the_stub) { HttpStub::Models::Stub.new(stub_options) }
|
20
|
+
let(:parameters_model) { double(HttpStub::Models::Parameters, match?: true) }
|
21
|
+
|
22
|
+
before(:each) { HttpStub::Models::Parameters.stub!(:new).and_return(parameters_model) }
|
18
23
|
|
19
24
|
describe "#satisfies?" do
|
20
25
|
|
21
26
|
let(:request_uri) { stub_uri }
|
22
27
|
let(:request_method) { stub_method }
|
23
|
-
let(:
|
24
|
-
let(:request) do
|
25
|
-
double("HttpRequest", :path_info => request_uri, :request_method => request_method, :params => request_parameters)
|
26
|
-
end
|
28
|
+
let(:request) { double("HttpRequest", :path_info => request_uri, :request_method => request_method) }
|
27
29
|
|
28
30
|
describe "when the request uri matches" do
|
29
31
|
|
30
32
|
describe "and the request method matches" do
|
31
33
|
|
32
34
|
describe "and a parameter match is configured" do
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
"param2" => "value2",
|
38
|
-
"param3" => "value3"
|
39
|
-
}
|
40
|
-
end
|
41
|
-
|
42
|
-
describe "and the request parameters are identical" do
|
43
|
-
|
44
|
-
it "should return true" do
|
45
|
-
the_stub.satisfies?(request).should be_true
|
46
|
-
end
|
47
|
-
|
48
|
-
end
|
49
|
-
|
50
|
-
describe "and the request parameters match the stub parameters and contain additional parameters" do
|
51
|
-
|
52
|
-
let(:request_parameters) { stub_parameters.merge("param4" => "value4") }
|
35
|
+
|
36
|
+
describe "that matches" do
|
37
|
+
|
38
|
+
before(:each) { parameters_model.stub!(:match?).with(request).and_return(true) }
|
53
39
|
|
54
40
|
it "should return true" do
|
55
41
|
the_stub.satisfies?(request).should be_true
|
56
42
|
end
|
57
|
-
|
43
|
+
|
58
44
|
end
|
45
|
+
|
46
|
+
describe "that does not match" do
|
59
47
|
|
60
|
-
|
61
|
-
|
62
|
-
let(:request_parameters) do
|
63
|
-
{
|
64
|
-
"param1" => "value1",
|
65
|
-
"param2" => "aDifferentValue",
|
66
|
-
"param3" => "value3"
|
67
|
-
}
|
68
|
-
end
|
48
|
+
before(:each) { parameters_model.stub!(:match?).with(request).and_return(false) }
|
69
49
|
|
70
50
|
it "should return false" do
|
71
51
|
the_stub.satisfies?(request).should be_false
|
@@ -73,37 +53,6 @@ describe HttpStub::Models::Stub do
|
|
73
53
|
|
74
54
|
end
|
75
55
|
|
76
|
-
describe "and the request parameters do not contain all stub parameters" do
|
77
|
-
|
78
|
-
let(:request_parameters) do
|
79
|
-
{
|
80
|
-
"param1" => "value1",
|
81
|
-
"param3" => "value3"
|
82
|
-
}
|
83
|
-
end
|
84
|
-
|
85
|
-
it "should be false" do
|
86
|
-
the_stub.satisfies?(request).should be_false
|
87
|
-
end
|
88
|
-
|
89
|
-
end
|
90
|
-
|
91
|
-
end
|
92
|
-
|
93
|
-
describe "and a parameter match is not configured" do
|
94
|
-
|
95
|
-
describe "and parameters are provided in the request" do
|
96
|
-
|
97
|
-
let(:request_parameters) do
|
98
|
-
{ "param1" => "value1" }
|
99
|
-
end
|
100
|
-
|
101
|
-
it "should be true" do
|
102
|
-
the_stub.satisfies?(request).should be_true
|
103
|
-
end
|
104
|
-
|
105
|
-
end
|
106
|
-
|
107
56
|
end
|
108
57
|
|
109
58
|
end
|
@@ -148,6 +97,14 @@ describe HttpStub::Models::Stub do
|
|
148
97
|
|
149
98
|
end
|
150
99
|
|
100
|
+
describe "#parameters" do
|
101
|
+
|
102
|
+
it "should return the parameters model encapsulating the parameters provided in the request body" do
|
103
|
+
the_stub.parameters.should eql(parameters_model)
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
107
|
+
|
151
108
|
describe "#response" do
|
152
109
|
|
153
110
|
it "should expose the provided response status" do
|
@@ -27,6 +27,10 @@ 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 parameters of each alias stub" do
|
31
|
+
(1..3).each { |i| response.body.should match(/param#{i}=value#{i}/) }
|
32
|
+
end
|
33
|
+
|
30
34
|
it "should return a response whose body contains the response status of each alias stub" do
|
31
35
|
(1..3).each { |i| response.body.should match(/20#{i}/) }
|
32
36
|
end
|
@@ -39,6 +43,43 @@ describe HttpStub::Server, "when the server is running" do
|
|
39
43
|
|
40
44
|
end
|
41
45
|
|
46
|
+
describe "GET #stubs" do
|
47
|
+
|
48
|
+
describe "when multiple stubs are configured" do
|
49
|
+
|
50
|
+
before(:all) do
|
51
|
+
(1..3).each { |i| Net::HTTP.get_response("localhost", "/alias#{i}", 8001) }
|
52
|
+
end
|
53
|
+
|
54
|
+
let(:response) { Net::HTTP.get_response("localhost", "/stubs", 8001) }
|
55
|
+
let(:response_document) { Nokogiri::HTML(response.body) }
|
56
|
+
|
57
|
+
it "should return a 200 response code" do
|
58
|
+
response.code.should eql("200")
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should return a response whose body contains the uri of each stub" do
|
62
|
+
(1..3).each { |i| response.body.should match(/#{escape_html("/path#{i}")}/) }
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should return a response whose body contains the parameters of each alias stub" do
|
66
|
+
(1..3).each { |i| response.body.should match(/param#{i}=value#{i}/) }
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should return a response whose body contains the response status of each alias stub" do
|
70
|
+
(1..3).each { |i| response.body.should match(/20#{i}/) }
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should return a response whose body contains the response body of each alias stub" do
|
74
|
+
response.body.should match(/Plain text body/)
|
75
|
+
response.body.should match(/#{escape_html({ "key" => "JSON body" }.to_json)}/)
|
76
|
+
response.body.should match(/#{escape_html("<html><body>HTML body</body></html>")}/)
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
42
83
|
end
|
43
84
|
|
44
85
|
end
|
data/spec/spec_helper.rb
CHANGED
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.3.
|
4
|
+
version: 0.3.1
|
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-02-
|
13
|
+
date: 2013-02-28 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: sinatra
|
@@ -199,6 +199,7 @@ files:
|
|
199
199
|
- ./lib/http_stub/controllers/alias_controller.rb
|
200
200
|
- ./lib/http_stub/controllers/stub_controller.rb
|
201
201
|
- ./lib/http_stub/models/alias.rb
|
202
|
+
- ./lib/http_stub/models/parameters.rb
|
202
203
|
- ./lib/http_stub/models/registry.rb
|
203
204
|
- ./lib/http_stub/models/stub.rb
|
204
205
|
- ./lib/http_stub/response.rb
|
@@ -208,12 +209,15 @@ files:
|
|
208
209
|
- ./lib/http_stub/views/_stub.haml
|
209
210
|
- ./lib/http_stub/views/aliases.haml
|
210
211
|
- ./lib/http_stub/views/application.sass
|
212
|
+
- ./lib/http_stub/views/layout.haml
|
213
|
+
- ./lib/http_stub/views/stubs.haml
|
211
214
|
- ./lib/http_stub.rb
|
212
215
|
- ./spec/curl_samples.txt
|
213
216
|
- ./spec/lib/http_stub/configurer_integration_spec.rb
|
214
217
|
- ./spec/lib/http_stub/controllers/alias_controller_spec.rb
|
215
218
|
- ./spec/lib/http_stub/controllers/stub_controller_spec.rb
|
216
219
|
- ./spec/lib/http_stub/models/alias_spec.rb
|
220
|
+
- ./spec/lib/http_stub/models/parameters_spec.rb
|
217
221
|
- ./spec/lib/http_stub/models/registry_spec.rb
|
218
222
|
- ./spec/lib/http_stub/models/stub_spec.rb
|
219
223
|
- ./spec/lib/http_stub/response_spec.rb
|
@@ -243,7 +247,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
243
247
|
version: '0'
|
244
248
|
segments:
|
245
249
|
- 0
|
246
|
-
hash:
|
250
|
+
hash: -3825897115098639918
|
247
251
|
requirements: []
|
248
252
|
rubyforge_project: http_stub
|
249
253
|
rubygems_version: 1.8.25
|
@@ -256,6 +260,7 @@ test_files:
|
|
256
260
|
- ./spec/lib/http_stub/controllers/alias_controller_spec.rb
|
257
261
|
- ./spec/lib/http_stub/controllers/stub_controller_spec.rb
|
258
262
|
- ./spec/lib/http_stub/models/alias_spec.rb
|
263
|
+
- ./spec/lib/http_stub/models/parameters_spec.rb
|
259
264
|
- ./spec/lib/http_stub/models/registry_spec.rb
|
260
265
|
- ./spec/lib/http_stub/models/stub_spec.rb
|
261
266
|
- ./spec/lib/http_stub/response_spec.rb
|