http_stub 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,63 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ Bundler.require(:default)
4
+ require "json"
5
+
6
+ module Http
7
+ module Stub
8
+
9
+ class Server < ::Sinatra::Base
10
+
11
+ SUPPORTED_REQUEST_TYPES = [:get, :post, :put, :delete, :patch, :options].freeze
12
+
13
+ enable :dump_errors, :logging
14
+
15
+ def initialize()
16
+ super()
17
+ @response_register = {}
18
+ end
19
+
20
+ private
21
+
22
+ def self.any_request_type(path, opts={}, &block)
23
+ SUPPORTED_REQUEST_TYPES.each { |type| self.send(type, path, opts, &block) }
24
+ end
25
+
26
+ public
27
+
28
+ # Sample request body:
29
+ # {
30
+ # "uri": "/some/path",
31
+ # "method": "get",
32
+ # "response": {
33
+ # "status": "200",
34
+ # "body": "Hello World"
35
+ # }
36
+ # }
37
+ post "/stub" do
38
+ data = JSON.parse(request.body.read)
39
+ logger.info "Stub registered: #{data}"
40
+ @response_register[data["uri"]] = data
41
+ halt 200
42
+ end
43
+
44
+ any_request_type(//) { handle_stub_request }
45
+
46
+ private
47
+
48
+ def handle_stub_request
49
+ logger.info "Stub response requested: #{request}"
50
+ stub_data = @response_register[request.path_info]
51
+ if stub_data && stub_data["method"].downcase == request.request_method.downcase
52
+ response_data = stub_data["response"]
53
+ halt response_data["status"] unless response_data["status"] == "200"
54
+ response_data["body"]
55
+ else
56
+ halt 404
57
+ end
58
+ end
59
+
60
+ end
61
+
62
+ end
63
+ end
@@ -0,0 +1,5 @@
1
+ module Http
2
+ module Stub
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +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
@@ -0,0 +1,65 @@
1
+ describe Http::Stub::Server do
2
+ include Rack::Test::Methods
3
+
4
+ let(:app) { Http::Stub::Server }
5
+ let(:response) { last_response }
6
+ let(:response_body) { response.body.to_s }
7
+
8
+ shared_examples "a server that stubs a response" do |options|
9
+
10
+ let(:request_type) { options[:request_type] }
11
+ let(:different_request_type) { options[:different_request_type] }
12
+ let(:test_url) { "/test_#{request_type}" }
13
+
14
+ before(:each) do
15
+ post "/stub", '{"uri": "' + test_url + '", "method": "' + request_type.to_s + '", "response": {"status":"200", "body":"Foo"}}'
16
+ end
17
+
18
+ describe "when a #{options[:request_type]} request is made" do
19
+
20
+ before(:each) { self.send(request_type, test_url) }
21
+
22
+ it "should respond with the stubbed response code" do
23
+ response.status.should eql(200)
24
+ end
25
+
26
+ it "should respond with the stubbed body" do
27
+ response_body.should eql("Foo")
28
+ end
29
+
30
+ end
31
+
32
+ describe "and a request of type '#{options[:different_request_type]}' is made" do
33
+
34
+ before(:each) { self.send(different_request_type, test_url) }
35
+
36
+ it "should respond with a 404 response code" do
37
+ response.status.should eql(404)
38
+ end
39
+
40
+ end
41
+
42
+ end
43
+
44
+ all_request_types = [:get, :post, :put, :delete, :patch, :options]
45
+ all_request_types.each_with_index do |request_type, i|
46
+
47
+ describe "when a #{request_type} request is stubbed" do
48
+
49
+ it_should_behave_like "a server that stubs a response", request_type: request_type, different_request_type: all_request_types[i - 1]
50
+
51
+ end
52
+
53
+ end
54
+
55
+ describe "when a request is made that is not stubbed" do
56
+
57
+ before(:each) { get "/not_stubbed" }
58
+
59
+ it "should respond with 404 response code" do
60
+ response.status.should eql(404)
61
+ end
62
+
63
+ end
64
+
65
+ end
@@ -0,0 +1,5 @@
1
+ require 'cover_me'
2
+ require 'rack/test'
3
+
4
+ require File.expand_path('../../lib/http/stub/server', __FILE__)
5
+
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: http_stub
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Matthew Ueckerman
9
+ - Russell Van Bert
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-02-08 00:00:00.000000000 Z
14
+ dependencies: []
15
+ description: Configure server responses via requests to /stub. Intended as an acceptance
16
+ / integration testing tool.
17
+ email: matthew.ueckerman@myob.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - ./lib/http/stub/server.rb
23
+ - ./lib/http/stub/version.rb
24
+ - ./spec/curl_sample.txt
25
+ - ./spec/lib/http/server_spec.rb
26
+ - ./spec/spec_helper.rb
27
+ homepage: http://github.com/MYOB-Technology/http_stub
28
+ licenses:
29
+ - MIT
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: 1.9.3
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubyforge_project: http_stub
48
+ rubygems_version: 1.8.25
49
+ signing_key:
50
+ specification_version: 3
51
+ summary: A Http Server replaying configured stub responses
52
+ test_files:
53
+ - ./spec/curl_sample.txt
54
+ - ./spec/lib/http/server_spec.rb
55
+ - ./spec/spec_helper.rb