derfred-httpstub 0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +18 -0
- data/lib/httpstub.rb +58 -0
- data/lib/httpstubserver.rb +20 -0
- data/lib/httpstubservlet.rb +59 -0
- metadata +57 -0
data/README
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
HTTPStub
|
2
|
+
========
|
3
|
+
|
4
|
+
HTTPStub is a very simple HTTP server for use in automated integration tests. For example if you're relying on an ActiveResource in your Rails application and you want to use a live integration framework this will make it easier to run the tests in a continuous integration setup.
|
5
|
+
|
6
|
+
Example
|
7
|
+
-------
|
8
|
+
|
9
|
+
The following will stub out a success response for Person.create. (Note that you need to declare which ports to listen on beforehand):
|
10
|
+
HTTPStub.listen_on(3001)
|
11
|
+
HTTPStub.post "http://localhost:3001/people/", { :status => 201, :location => "http://localhost:3001/people/1.xml" }, <<-EOF
|
12
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
13
|
+
<person>
|
14
|
+
<id>1</id>
|
15
|
+
<name>Joe Doe</name>
|
16
|
+
</person>
|
17
|
+
EOF
|
18
|
+
|
data/lib/httpstub.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'webrick'
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + "/webrick_monkey_patch")
|
3
|
+
require File.expand_path(File.dirname(__FILE__) + "/httpstubservlet")
|
4
|
+
require File.expand_path(File.dirname(__FILE__) + "/httpstubserver")
|
5
|
+
|
6
|
+
class HTTPStub
|
7
|
+
|
8
|
+
@@root_server = nil
|
9
|
+
@@thread = nil
|
10
|
+
|
11
|
+
def self.listen_on(ports)
|
12
|
+
@@root_server = WEBrick::HTTPServer.new :Port => 10000, :DoNotListen => true
|
13
|
+
[ports].flatten.each do |port|
|
14
|
+
@@root_server.virtual_host HTTPStubServer.new(port)
|
15
|
+
@@root_server.listen "0.0.0.0", port
|
16
|
+
end
|
17
|
+
@@thread = Thread.new(@@root_server) { |server| server.start }
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.initialize_server(port)
|
21
|
+
@@root_server.server_for_port(port)
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.stop_server
|
25
|
+
if @@thread
|
26
|
+
@@root_server.virtual_hosts.each { |h| h.servlet.clear_responses }
|
27
|
+
@@root_server.shutdown
|
28
|
+
@@thread.join
|
29
|
+
@@thread = nil
|
30
|
+
@@root_server = nil
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
def self.get(url, metadata, body)
|
36
|
+
stub(:get, url, metadata, body)
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.post(url, metadata, body)
|
40
|
+
stub(:post, url, metadata, body)
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.put(url, metadata, body)
|
44
|
+
stub(:put, url, metadata, body)
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.delete(url, metadata, body)
|
48
|
+
stub(:delete, url, metadata, body)
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
def self.stub(method, url, metadata, body)
|
53
|
+
parsed_url = URI.parse(url)
|
54
|
+
server = initialize_server parsed_url.port
|
55
|
+
server.servlet.send("stub_#{method}", "#{parsed_url.path}?#{parsed_url.query}", metadata, body)
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + "/../lib/httpstub")
|
2
|
+
|
3
|
+
class HTTPStubServer < WEBrick::HTTPServer
|
4
|
+
|
5
|
+
attr_reader :servlet
|
6
|
+
|
7
|
+
def initialize(port)
|
8
|
+
super :Port => port, :DoNotListen => true, :BindAddress => nil, :ServerName => nil, :ServerAlias => nil
|
9
|
+
@servlet = HTTPStubServlet.get_instance self, port
|
10
|
+
end
|
11
|
+
|
12
|
+
def hier
|
13
|
+
puts "hier"
|
14
|
+
end
|
15
|
+
|
16
|
+
def service(req, res)
|
17
|
+
@servlet.service(req, res)
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
class HTTPStubServlet < WEBrick::HTTPServlet::AbstractServlet
|
2
|
+
|
3
|
+
@@instances = {}
|
4
|
+
def self.get_instance(server, port)
|
5
|
+
@@instances[port] ||= self.new(server)
|
6
|
+
end
|
7
|
+
|
8
|
+
def initialize(server)
|
9
|
+
super
|
10
|
+
clear_responses
|
11
|
+
end
|
12
|
+
|
13
|
+
[:get, :post, :put, :delete].each do |method|
|
14
|
+
define_method "stub_#{method}" do |url, headers, body|
|
15
|
+
@responses[url] ||= {}
|
16
|
+
@responses[url][method] = [headers, body]
|
17
|
+
end
|
18
|
+
|
19
|
+
define_method "do_#{method.to_s.upcase}" do |request, response|
|
20
|
+
if canned_response = get_response(request.unparsed_uri, method)
|
21
|
+
fill_response(response, canned_response)
|
22
|
+
else
|
23
|
+
respond_with_404(response)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
def clear_responses
|
30
|
+
@responses = {}
|
31
|
+
end
|
32
|
+
|
33
|
+
def get_response(path, method)
|
34
|
+
(@responses[path] || {})[method]
|
35
|
+
end
|
36
|
+
|
37
|
+
def fill_response(response, canned_response)
|
38
|
+
headers = canned_response.first || {}
|
39
|
+
headers.each do |k, v|
|
40
|
+
next if k == :status
|
41
|
+
response[convert_header_name(k)] = v
|
42
|
+
end
|
43
|
+
|
44
|
+
response.status = headers[:status] || 200
|
45
|
+
response.body = canned_response.last
|
46
|
+
end
|
47
|
+
|
48
|
+
def respond_with_404(response)
|
49
|
+
response.status = 404
|
50
|
+
response.body = "not found"
|
51
|
+
end
|
52
|
+
|
53
|
+
def convert_header_name(key)
|
54
|
+
{
|
55
|
+
:content_type => "Content-Type"
|
56
|
+
}[key] || key
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: derfred-httpstub
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "0.1"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Frederik Fix
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-04-15 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: a very simple http server for use in automated integration tests
|
17
|
+
email: fred@stemcel.co.uk
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- README
|
26
|
+
- lib/httpstub.rb
|
27
|
+
- lib/httpstubservlet.rb
|
28
|
+
- lib/httpstubserver.rb
|
29
|
+
has_rdoc: true
|
30
|
+
homepage: http://github.com/derfred/httpstub
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options:
|
33
|
+
- --inline-source
|
34
|
+
- --charset=UTF-8
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: "0"
|
42
|
+
version:
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: "0"
|
48
|
+
version:
|
49
|
+
requirements: []
|
50
|
+
|
51
|
+
rubyforge_project:
|
52
|
+
rubygems_version: 1.2.0
|
53
|
+
signing_key:
|
54
|
+
specification_version: 2
|
55
|
+
summary: a very simple http server for use in automated integration tests
|
56
|
+
test_files: []
|
57
|
+
|