http_stub 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,45 @@
1
+ module Http
2
+ module Stub
3
+ module Client
4
+
5
+ def self.included(mod)
6
+ mod.extend(Http::Stub::Client::ClassMethods)
7
+ mod.send(:include, Http::Stub::Client::InstanceMethods)
8
+ end
9
+
10
+ module ClassMethods
11
+
12
+ attr_reader :host_value, :port_value
13
+
14
+ def host(host)
15
+ @host_value = host
16
+ end
17
+
18
+ def port(port)
19
+ @port_value = port
20
+ end
21
+
22
+ end
23
+
24
+ module InstanceMethods
25
+
26
+ def stub_response!(uri, response_options)
27
+ request = Net::HTTP::Post.new("/stub")
28
+ request.content_type = "application/json"
29
+ request.body = {
30
+ "uri" => uri,
31
+ "method" => response_options[:method],
32
+ "response" => {
33
+ "status" => response_options[:status] || "200",
34
+ "body" => response_options[:body]
35
+ }
36
+ }.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"
39
+ end
40
+
41
+ end
42
+
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,22 @@
1
+ require File.expand_path('../../../http_stub', __FILE__)
2
+ require 'rake/tasklib' unless defined?(::Rake::TaskLib)
3
+
4
+ module Http
5
+ module Stub
6
+
7
+ class RakeTask < ::Rake::TaskLib
8
+
9
+ def initialize(server_name, server_port)
10
+ desc "Starts stub #{server_name} server"
11
+ task "start_#{server_name}_server" do
12
+ Http::Stub::Server.instance_eval do
13
+ set :port, server_port
14
+ run!
15
+ end
16
+ end
17
+ end
18
+
19
+ end
20
+
21
+ end
22
+ end
@@ -1,8 +1,3 @@
1
- require 'rubygems'
2
- require 'bundler/setup'
3
- Bundler.require(:default)
4
- require "json"
5
-
6
1
  module Http
7
2
  module Stub
8
3
 
@@ -50,8 +45,7 @@ module Http
50
45
  stub_data = @response_register[request.path_info]
51
46
  if stub_data && stub_data["method"].downcase == request.request_method.downcase
52
47
  response_data = stub_data["response"]
53
- halt response_data["status"] unless response_data["status"] == "200"
54
- response_data["body"]
48
+ halt response_data["status"].to_i, response_data["body"]
55
49
  else
56
50
  halt 404
57
51
  end
@@ -1,5 +1,5 @@
1
1
  module Http
2
2
  module Stub
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
data/lib/http_stub.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ Bundler.require(:default)
4
+
5
+ require 'sinatra'
6
+ require 'net/http'
7
+ require 'json'
8
+
9
+ require File.expand_path('../http/stub/server', __FILE__)
10
+ require File.expand_path('../http/stub/client', __FILE__)
data/spec/curl_sample.txt CHANGED
@@ -1 +1 @@
1
- curl -v -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"uri": "/test", "method": "get", "response": {"status":"200", "body":"Foo"}}' http://localhost:8000/register_stub
1
+ curl -v -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"uri": "/test", "method": "get", "response": {"status":"200", "body":"Foo"}}' http://localhost:8001/stub
@@ -0,0 +1,32 @@
1
+ describe Http::Stub::Client do
2
+ include_context "server integration"
3
+
4
+ class TestClient
5
+ include Http::Stub::Client
6
+
7
+ host "localhost"
8
+ port 8001
9
+ end
10
+
11
+ let(:client) { TestClient.new }
12
+
13
+ describe "when a response for a request is stubbed" do
14
+
15
+ before(:each) do
16
+ client.stub_response!("/a_path", method: :get, status: 200, body: "Some body")
17
+ end
18
+
19
+ describe "and that request is made" do
20
+
21
+ let(:response) { Net::HTTP.get_response("localhost", "/a_path", 8001) }
22
+
23
+ it "should replay the stubbed response" do
24
+ response.code.should eql("200")
25
+ response.body.should eql("Some body")
26
+ end
27
+
28
+ end
29
+
30
+ end
31
+
32
+ end
@@ -0,0 +1,17 @@
1
+ describe Http::Stub::RakeTask do
2
+ include_context "server integration"
3
+
4
+ describe("when the generated task is invoked") do
5
+
6
+ it "should start a stub server that responds to stub requests" do
7
+ request = Net::HTTP::Post.new("/stub")
8
+ request.body = "{}"
9
+
10
+ response = Net::HTTP.new("localhost", 8001).start { |http| http.request(request) }
11
+
12
+ response.code.should eql("200")
13
+ end
14
+
15
+ end
16
+
17
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  require 'cover_me'
2
2
  require 'rack/test'
3
3
 
4
- require File.expand_path('../../lib/http/stub/server', __FILE__)
4
+ require File.expand_path('../../lib/http/stub/rake_task', __FILE__)
5
+ require File.expand_path('../../lib/http_stub', __FILE__)
5
6
 
7
+ Dir[File.expand_path('../support/**/*.rb', __FILE__)].each { |file| require file }
@@ -0,0 +1,28 @@
1
+ share_examples_for "server integration" do
2
+
3
+ FIVE_SECONDS = 5
4
+
5
+ before(:all) do
6
+ @pid = Process.spawn("rake start_sample_server --trace")
7
+ wait_until("http stub server started") { Net::HTTP.get_response("localhost", "/", 8001) }
8
+ end
9
+
10
+ after(:all) do
11
+ Process.kill(9, @pid)
12
+ end
13
+
14
+ def wait_until(description, &block)
15
+ start_time = Time.now
16
+ while true
17
+ begin
18
+ block.call
19
+ return
20
+ rescue => exc
21
+ if Time.now - start_time > FIVE_SECONDS
22
+ raise "Timed out waiting until #{description}"
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ end
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.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,8 +10,24 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-02-08 00:00:00.000000000 Z
14
- dependencies: []
13
+ date: 2013-02-10 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: sinatra
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 1.3.4
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ version: 1.3.4
15
31
  description: Configure server responses via requests to /stub. Intended as an acceptance
16
32
  / integration testing tool.
17
33
  email: matthew.ueckerman@myob.com
@@ -19,11 +35,17 @@ executables: []
19
35
  extensions: []
20
36
  extra_rdoc_files: []
21
37
  files:
38
+ - ./lib/http/stub/client.rb
39
+ - ./lib/http/stub/rake_task.rb
22
40
  - ./lib/http/stub/server.rb
23
41
  - ./lib/http/stub/version.rb
42
+ - ./lib/http_stub.rb
24
43
  - ./spec/curl_sample.txt
44
+ - ./spec/lib/http/client_integration_spec.rb
45
+ - ./spec/lib/http/rake_task_integration_spec.rb
25
46
  - ./spec/lib/http/server_spec.rb
26
47
  - ./spec/spec_helper.rb
48
+ - ./spec/support/server_integration.rb
27
49
  homepage: http://github.com/MYOB-Technology/http_stub
28
50
  licenses:
29
51
  - MIT
@@ -43,6 +65,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
43
65
  - - ! '>='
44
66
  - !ruby/object:Gem::Version
45
67
  version: '0'
68
+ segments:
69
+ - 0
70
+ hash: -1339898744489492428
46
71
  requirements: []
47
72
  rubyforge_project: http_stub
48
73
  rubygems_version: 1.8.25
@@ -51,5 +76,8 @@ specification_version: 3
51
76
  summary: A Http Server replaying configured stub responses
52
77
  test_files:
53
78
  - ./spec/curl_sample.txt
79
+ - ./spec/lib/http/client_integration_spec.rb
80
+ - ./spec/lib/http/rake_task_integration_spec.rb
54
81
  - ./spec/lib/http/server_spec.rb
55
82
  - ./spec/spec_helper.rb
83
+ - ./spec/support/server_integration.rb