http_stub 0.8.0 → 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -13,9 +13,9 @@ module HttpStub
13
13
  "headers" => HttpStub::Configurer::Request::Regexpable.format(options[:headers] || {}),
14
14
  "parameters" => HttpStub::Configurer::Request::Regexpable.format(options[:parameters] || {}),
15
15
  "response" => {
16
- "status" => options[:response][:status] || 200,
16
+ "status" => options[:response][:status] || "",
17
17
  "body" => options[:response][:body],
18
- "delay_in_seconds" => options[:response][:delay_in_seconds]
18
+ "delay_in_seconds" => options[:response][:delay_in_seconds] || ""
19
19
  }
20
20
  }.to_json
21
21
  end
@@ -1,10 +1,13 @@
1
1
  module HttpStub
2
2
  module Models
3
+
3
4
  class RequestPipeline
4
5
 
5
6
  def self.before_halt(response)
6
- sleep response.delay_in_seconds if response.delay_in_seconds
7
+ sleep response.delay_in_seconds
7
8
  end
9
+
8
10
  end
11
+
9
12
  end
10
- end
13
+ end
@@ -3,8 +3,20 @@ module HttpStub
3
3
 
4
4
  class Response
5
5
 
6
+ private
7
+
8
+ DEFAULT_OPTIONS = { "status" => 200, "delay_in_seconds" => 0 }
9
+
10
+ def establish_defaults_in(options)
11
+ DEFAULT_OPTIONS.each { |key, value| options[key] = value if !options[key] || options[key] == "" }
12
+ end
13
+
14
+ public
15
+
6
16
  def initialize(options = {})
7
- @response_options = options || {}
17
+ @original_options = options
18
+ @response_options = options.clone
19
+ establish_defaults_in(@response_options)
8
20
  end
9
21
 
10
22
  SUCCESS = HttpStub::Models::Response.new("status" => 200, "body" => "OK")
@@ -24,8 +36,9 @@ module HttpStub
24
36
  end
25
37
 
26
38
  def empty?
27
- @response_options.empty?
39
+ @original_options.empty?
28
40
  end
41
+
29
42
  end
30
43
 
31
44
  end
@@ -1,3 +1,3 @@
1
1
  module HttpStub
2
- VERSION = "0.8.0"
2
+ VERSION = "0.8.1"
3
3
  end
@@ -2,7 +2,7 @@ describe HttpStub::Configurer::Request::Stub do
2
2
 
3
3
  describe "#initialize" do
4
4
 
5
- describe "when provided a uri and stub options" do
5
+ context "when provided a uri and stub options" do
6
6
 
7
7
  let(:uri) { "/some/uri" }
8
8
  let(:stub_method) { "Some Method" }
@@ -10,6 +10,7 @@ describe HttpStub::Configurer::Request::Stub do
10
10
  let(:parameters) { { "parameter_name" => "value" } }
11
11
  let(:response_status) { 500 }
12
12
  let(:response_body) { "Some body" }
13
+ let(:response_delay_in_seconds) { 7 }
13
14
 
14
15
  let(:stub_options) do
15
16
  {
@@ -18,7 +19,8 @@ describe HttpStub::Configurer::Request::Stub do
18
19
  parameters: parameters,
19
20
  response: {
20
21
  status: response_status,
21
- body: response_body
22
+ body: response_body,
23
+ delay_in_seconds: response_delay_in_seconds
22
24
  }
23
25
  }
24
26
  end
@@ -40,7 +42,7 @@ describe HttpStub::Configurer::Request::Stub do
40
42
  request.content_type.should eql("application/json")
41
43
  end
42
44
 
43
- describe "when a header option is provided" do
45
+ context "when a header option is provided" do
44
46
 
45
47
  it "should format the headers with regexp support" do
46
48
  HttpStub::Configurer::Request::Regexpable.should_receive(:format).with(headers)
@@ -50,7 +52,7 @@ describe HttpStub::Configurer::Request::Stub do
50
52
 
51
53
  end
52
54
 
53
- describe "when no header is provided" do
55
+ context "when no header is provided" do
54
56
 
55
57
  let(:headers) { nil }
56
58
 
@@ -62,7 +64,7 @@ describe HttpStub::Configurer::Request::Stub do
62
64
 
63
65
  end
64
66
 
65
- describe "when a parameter option is provided" do
67
+ context "when a parameter option is provided" do
66
68
 
67
69
  it "should format the parameters with regexp support" do
68
70
  HttpStub::Configurer::Request::Regexpable.should_receive(:format).with(parameters)
@@ -72,7 +74,7 @@ describe HttpStub::Configurer::Request::Stub do
72
74
 
73
75
  end
74
76
 
75
- describe "when no parameter option is provided" do
77
+ context "when no parameter option is provided" do
76
78
 
77
79
  let(:parameters) { nil }
78
80
 
@@ -84,52 +86,70 @@ describe HttpStub::Configurer::Request::Stub do
84
86
 
85
87
  end
86
88
 
87
- describe "generates a JSON body which" do
89
+ context "generates a JSON body which" do
88
90
 
89
91
  it "should have an entry containing the regexpable representation of the uri" do
90
92
  HttpStub::Configurer::Request::Regexpable.should_receive(:format).with(uri).and_return("uri as a string")
91
93
 
92
- request_body.should include({ "uri" => "uri as a string" })
94
+ request_body.should include("uri" => "uri as a string")
93
95
  end
94
96
 
95
97
  it "should have an entry for the method option" do
96
- request_body.should include({ "method" => stub_method })
98
+ request_body.should include("method" => stub_method)
97
99
  end
98
100
 
99
101
  it "should have an entry containing the string representation of the headers" do
100
102
  HttpStub::Configurer::Request::Regexpable.should_receive(:format)
101
103
  .with(headers).and_return("headers as string")
102
104
 
103
- request_body.should include({ "headers" => "headers as string" })
105
+ request_body.should include("headers" => "headers as string")
104
106
  end
105
107
 
106
108
  it "should have an entry containing the string representation of the parameters" do
107
109
  HttpStub::Configurer::Request::Regexpable.should_receive(:format)
108
110
  .with(parameters).and_return("parameters as string")
109
111
 
110
- request_body.should include({ "parameters" => "parameters as string" })
112
+ request_body.should include("parameters" => "parameters as string")
111
113
  end
112
114
 
113
- describe "when a status response option is provided" do
115
+ context "when a status response option is provided" do
114
116
 
115
117
  it "should have a response entry for the option" do
116
- request_body["response"].should include({ "status" => response_status })
118
+ request_body["response"].should include("status" => response_status)
117
119
  end
118
120
 
119
121
  end
120
122
 
121
- describe "when no status response option is provided" do
123
+ context "when no status response option is provided" do
122
124
 
123
125
  let(:response_status) { nil }
124
126
 
125
- it "should have a response entry with status code of 200" do
126
- request_body["response"].should include({ "status" => 200 })
127
+ it "should have a response entry with an empty status code" do
128
+ request_body["response"].should include("status" => "")
127
129
  end
128
130
 
129
131
  end
130
132
 
131
133
  it "should have an entry for the response body option" do
132
- request_body["response"].should include({ "body" => response_body })
134
+ request_body["response"].should include("body" => response_body)
135
+ end
136
+
137
+ context "when a delay option is provided" do
138
+
139
+ it "should have a response entry for the option" do
140
+ request_body["response"].should include("delay_in_seconds" => response_delay_in_seconds)
141
+ end
142
+
143
+ end
144
+
145
+ context "when a delay option is not provided" do
146
+
147
+ let(:response_delay_in_seconds) { nil }
148
+
149
+ it "should have a response entry with an empty delay" do
150
+ request_body["response"].should include("delay_in_seconds" => "")
151
+ end
152
+
133
153
  end
134
154
 
135
155
  end
@@ -1,24 +1,17 @@
1
1
  describe HttpStub::Models::RequestPipeline do
2
2
 
3
- describe '.before_halt' do
3
+ let(:request_pipeline) { HttpStub::Models::RequestPipeline }
4
4
 
5
- let(:response) { double(HttpStub::Models::Response) }
6
- let(:request_pipeline) { HttpStub::Models::RequestPipeline }
5
+ describe ".before_halt" do
7
6
 
8
- before(:each) do
9
- request_pipeline.stub(:sleep)
10
- response.stub(:delay_in_seconds).and_return(5)
11
- end
7
+ let(:response) { double(HttpStub::Models::Response, delay_in_seconds: 5) }
12
8
 
13
- it 'should sleep for specified duration' do
9
+ it "should sleep for specified duration" do
14
10
  request_pipeline.should_receive(:sleep).with(5)
15
- request_pipeline.before_halt(response)
16
- end
17
11
 
18
- it 'should skip sleep if not specified' do
19
- request_pipeline.should_not_receive(:sleep)
20
- response.stub(:delay_in_seconds).and_return(nil)
21
12
  request_pipeline.before_halt(response)
22
13
  end
14
+
23
15
  end
24
- end
16
+
17
+ end
@@ -1,6 +1,12 @@
1
1
  describe HttpStub::Models::Response do
2
2
 
3
- let(:response) { HttpStub::Models::Response.new("status" => 202, "body" => "A response body")}
3
+ let(:status) { 202 }
4
+ let(:body) { "A response body" }
5
+ let(:delay_in_seconds) { 18 }
6
+
7
+ let(:response) do
8
+ HttpStub::Models::Response.new("status" => status, "body" => body, "delay_in_seconds" => delay_in_seconds)
9
+ end
4
10
 
5
11
  describe "::SUCCESS" do
6
12
 
@@ -30,24 +36,38 @@ describe HttpStub::Models::Response do
30
36
 
31
37
  end
32
38
 
33
- describe "::EMPTY" do
39
+ describe "#status" do
34
40
 
35
- let(:response) { HttpStub::Models::Response::EMPTY }
41
+ context "when a value is provided in the options" do
36
42
 
37
- it "should have a nil status" do
38
- response.status.should be_nil
39
- end
43
+ context "that is an integer" do
44
+
45
+ it "should return the value provided" do
46
+ response.status.should eql(status)
47
+ end
48
+
49
+ end
50
+
51
+ context "that is an empty string" do
52
+
53
+ let(:status) { "" }
54
+
55
+ it "should return 200" do
56
+ response.status.should eql(200)
57
+ end
58
+
59
+ end
40
60
 
41
- it "should have a nil body" do
42
- response.body.should be_nil
43
61
  end
44
62
 
45
- end
63
+ context "when the status is not provided in the options" do
46
64
 
47
- describe "#status" do
65
+ let(:response) { HttpStub::Models::Response.new("body" => body, "delay_in_seconds" => delay_in_seconds) }
66
+
67
+ it "should return 200" do
68
+ response.status.should eql(200)
69
+ end
48
70
 
49
- it "should return the value provided in the options" do
50
- response.status.should eql(202)
51
71
  end
52
72
 
53
73
  end
@@ -60,6 +80,42 @@ describe HttpStub::Models::Response do
60
80
 
61
81
  end
62
82
 
83
+ describe "#delay_in_seconds" do
84
+
85
+ context "when a value is provided in the options" do
86
+
87
+ context "that is an integer" do
88
+
89
+ it "should return the value" do
90
+ response.delay_in_seconds.should eql(delay_in_seconds)
91
+ end
92
+
93
+ end
94
+
95
+ context "that is an empty string" do
96
+
97
+ let(:delay_in_seconds) { "" }
98
+
99
+ it "should return 0" do
100
+ response.delay_in_seconds.should eql(0)
101
+ end
102
+
103
+ end
104
+
105
+ end
106
+
107
+ context "when a value is not provided in the options" do
108
+
109
+ let(:response) { HttpStub::Models::Response.new("status" => status, "body" => body) }
110
+
111
+ it "should return 0" do
112
+ response.delay_in_seconds.should eql(0)
113
+ end
114
+
115
+ end
116
+
117
+ end
118
+
63
119
  describe "#empty?" do
64
120
 
65
121
  describe "when the response is EMPTY" do
data/spec/spec_helper.rb CHANGED
@@ -2,7 +2,7 @@ require 'simplecov'
2
2
  SimpleCov.start do
3
3
  add_filter "/spec/"
4
4
  add_filter "/vendor/"
5
- minimum_coverage 98.28
5
+ minimum_coverage 98.3
6
6
  refuse_coverage_drop
7
7
  end if ENV["coverage"]
8
8
 
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.8.0
4
+ version: 0.8.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-09-16 00:00:00.000000000 Z
13
+ date: 2013-09-17 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: sinatra
@@ -323,7 +323,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
323
323
  version: '0'
324
324
  segments:
325
325
  - 0
326
- hash: -2870927875364426759
326
+ hash: -2719492474227436530
327
327
  requirements: []
328
328
  rubyforge_project: http_stub
329
329
  rubygems_version: 1.8.25