http_stub 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -23,19 +23,34 @@ module Http
23
23
 
24
24
  module InstanceMethods
25
25
 
26
- def stub_response!(uri, response_options)
26
+ def stub!(uri, options)
27
+ response_options = options[:response]
27
28
  request = Net::HTTP::Post.new("/stub")
28
29
  request.content_type = "application/json"
29
30
  request.body = {
30
31
  "uri" => uri,
31
- "method" => response_options[:method],
32
+ "method" => options[:method],
32
33
  "response" => {
33
34
  "status" => response_options[:status] || "200",
34
35
  "body" => response_options[:body]
35
36
  }
36
37
  }.to_json
37
- response = Net::HTTP.new(self.class.host_value, self.class.port_value).start { |http| http.request(request) }
38
- raise "Unable to stub request, stub responded with: #{response.message}" unless response.code == "200"
38
+ response = submit(request)
39
+ raise "Unable to stub request: #{response.message}" unless response.code == "200"
40
+ end
41
+
42
+ alias_method :stub_response!, :stub!
43
+
44
+ def clear!
45
+ request = Net::HTTP::Delete.new("/stubs")
46
+ response = submit(request)
47
+ raise "Unable to clear stubs: #{response.message}" unless response.code == "200"
48
+ end
49
+
50
+ private
51
+
52
+ def submit(request)
53
+ Net::HTTP.new(self.class.host_value, self.class.port_value).start { |http| http.request(request) }
39
54
  end
40
55
 
41
56
  end
@@ -17,6 +17,11 @@ module Http
17
17
  @stubs.find { |stub| stub.stubs?(request) }
18
18
  end
19
19
 
20
+ def clear(request)
21
+ request.logger.info "Clearing stubs"
22
+ @stubs.clear
23
+ end
24
+
20
25
  end
21
26
 
22
27
  end
@@ -38,6 +38,11 @@ module Http
38
38
  halt 200
39
39
  end
40
40
 
41
+ delete "/stubs" do
42
+ @registry.clear(request)
43
+ halt 200
44
+ end
45
+
41
46
  any_request_type(//) { handle_stub_request }
42
47
 
43
48
  private
@@ -4,13 +4,13 @@ require 'rake/tasklib' unless defined?(::Rake::TaskLib)
4
4
  module Http
5
5
  module Stub
6
6
 
7
- class RakeTask < ::Rake::TaskLib
7
+ class StartServerRakeTask < ::Rake::TaskLib
8
8
 
9
- def initialize(server_name, server_port)
10
- desc "Starts stub #{server_name} server"
11
- task "start_#{server_name}_server" do
9
+ def initialize(options)
10
+ desc "Starts stub #{options[:name]}"
11
+ task "start_#{options[:name]}" do
12
12
  Http::Stub::Server.instance_eval do
13
- set :port, server_port
13
+ set :port, options[:port]
14
14
  run!
15
15
  end
16
16
  end
@@ -1,5 +1,5 @@
1
1
  module Http
2
2
  module Stub
3
- VERSION = "0.0.3"
3
+ VERSION = "0.0.4"
4
4
  end
5
5
  end
@@ -12,7 +12,7 @@ describe Http::Stub::Client do
12
12
 
13
13
  describe "when a response for a request is stubbed" do
14
14
 
15
- before(:each) { client.stub_response!("/a_path", method: :get, status: 200, body: "Some body") }
15
+ before(:each) { client.stub_response!("/a_path", method: :get, response: { status: 200, body: "Some body" }) }
16
16
 
17
17
  describe "and that request is made" do
18
18
 
@@ -25,6 +25,22 @@ describe Http::Stub::Client do
25
25
 
26
26
  end
27
27
 
28
+ describe "and the stub is cleared" do
29
+
30
+ before(:each) { client.clear! }
31
+
32
+ describe "and the original request is made" do
33
+
34
+ let(:response) { Net::HTTP.get_response("localhost", "/a_path", 8001) }
35
+
36
+ it "should respond with a 404 status code" do
37
+ response.code.should eql("404")
38
+ end
39
+
40
+ end
41
+
42
+ end
43
+
28
44
  end
29
45
 
30
46
  end
@@ -36,6 +36,16 @@ describe Http::Stub::Registry do
36
36
  registry.find_for(request).should eql(stubs[1])
37
37
  end
38
38
 
39
+ describe "and the registry is subsequently cleared" do
40
+
41
+ before(:each) { registry.clear(request) }
42
+
43
+ it "should return nil" do
44
+ registry.find_for(request).should be_nil
45
+ end
46
+
47
+ end
48
+
39
49
  end
40
50
 
41
51
  describe "and multiple registered stubs match the request" do
@@ -76,4 +86,14 @@ describe Http::Stub::Registry do
76
86
 
77
87
  end
78
88
 
89
+ describe "#clear" do
90
+
91
+ it "should log that the stubs are being cleared" do
92
+ logger.should_receive(:info).with(/clearing stubs/i)
93
+
94
+ registry.clear(request)
95
+ end
96
+
97
+ end
98
+
79
99
  end
@@ -21,7 +21,7 @@ describe Http::Stub::Server do
21
21
 
22
22
  end
23
23
 
24
- describe "when a playback request is received" do
24
+ describe "when a replay request is received" do
25
25
 
26
26
  describe "and the request has been stubbed" do
27
27
 
@@ -50,7 +50,7 @@ describe Http::Stub::Server do
50
50
  registry.stub!(:find_for).and_return(nil)
51
51
  end
52
52
 
53
- it "should respond with a 404" do
53
+ it "should respond with a 404 status code" do
54
54
  get "/a_path"
55
55
 
56
56
  response.status.should eql(404)
@@ -60,6 +60,22 @@ describe Http::Stub::Server do
60
60
 
61
61
  end
62
62
 
63
+ describe "when a request to clear the stub has been received" do
64
+
65
+ it "should clear the contents of the registry" do
66
+ registry.should_receive(:clear)
67
+
68
+ delete "/stubs"
69
+ end
70
+
71
+ it "should respond with a 200 status code" do
72
+ delete "/stubs"
73
+
74
+ response.status.should eql(200)
75
+ end
76
+
77
+ end
78
+
63
79
  def issue_stub_request
64
80
  post "/stub", {
65
81
  "uri" => "/a_path",
@@ -1,4 +1,4 @@
1
- describe Http::Stub::RakeTask do
1
+ describe Http::Stub::StartServerRakeTask do
2
2
  include_context "server integration"
3
3
 
4
4
  describe("when the generated task is invoked") do
data/spec/spec_helper.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'cover_me'
2
2
  require 'rack/test'
3
3
 
4
- require File.expand_path('../../lib/http/stub/rake_task', __FILE__)
4
+ require File.expand_path('../../lib/http/stub/start_server_rake_task', __FILE__)
5
5
  require File.expand_path('../../lib/http_stub', __FILE__)
6
6
 
7
7
  Dir[File.expand_path('../support/**/*.rb', __FILE__)].each { |file| require file }
@@ -3,7 +3,7 @@ share_examples_for "server integration" do
3
3
  FIVE_SECONDS = 5
4
4
 
5
5
  before(:all) do
6
- @pid = Process.spawn("rake start_sample_server --trace")
6
+ @pid = Process.spawn("rake start_test_server --trace")
7
7
  wait_until("http stub server started") { Net::HTTP.get_response("localhost", "/", 8001) }
8
8
  end
9
9
 
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.0.3
4
+ version: 0.0.4
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-11 00:00:00.000000000 Z
13
+ date: 2013-02-12 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: sinatra
@@ -52,17 +52,17 @@ extensions: []
52
52
  extra_rdoc_files: []
53
53
  files:
54
54
  - ./lib/http/stub/client.rb
55
- - ./lib/http/stub/rake_task.rb
56
55
  - ./lib/http/stub/registry.rb
57
56
  - ./lib/http/stub/server.rb
57
+ - ./lib/http/stub/start_server_rake_task.rb
58
58
  - ./lib/http/stub/stub.rb
59
59
  - ./lib/http/stub/version.rb
60
60
  - ./lib/http_stub.rb
61
61
  - ./spec/curl_sample.txt
62
62
  - ./spec/lib/http/client_integration_spec.rb
63
- - ./spec/lib/http/rake_task_integration_spec.rb
64
63
  - ./spec/lib/http/registry_spec.rb
65
64
  - ./spec/lib/http/server_spec.rb
65
+ - ./spec/lib/http/start_server_rake_task_integration_spec.rb
66
66
  - ./spec/lib/http/stub_spec.rb
67
67
  - ./spec/spec_helper.rb
68
68
  - ./spec/support/server_integration.rb
@@ -87,7 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
87
87
  version: '0'
88
88
  segments:
89
89
  - 0
90
- hash: 2752646787075504105
90
+ hash: 698355929090701443
91
91
  requirements: []
92
92
  rubyforge_project: http_stub
93
93
  rubygems_version: 1.8.25
@@ -97,9 +97,9 @@ summary: A Http Server replaying configured stub responses
97
97
  test_files:
98
98
  - ./spec/curl_sample.txt
99
99
  - ./spec/lib/http/client_integration_spec.rb
100
- - ./spec/lib/http/rake_task_integration_spec.rb
101
100
  - ./spec/lib/http/registry_spec.rb
102
101
  - ./spec/lib/http/server_spec.rb
102
+ - ./spec/lib/http/start_server_rake_task_integration_spec.rb
103
103
  - ./spec/lib/http/stub_spec.rb
104
104
  - ./spec/spec_helper.rb
105
105
  - ./spec/support/server_integration.rb