mimic 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/LICENSE +20 -0
  2. data/README.md +43 -0
  3. data/Rakefile +83 -0
  4. data/lib/mimic.rb +75 -0
  5. data/lib/mimic/fake_host.rb +90 -0
  6. metadata +131 -0
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Luke Redpath
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # What is Mimic?
2
+ Mimic is a testing tool that lets you set create a fake stand-in for an external web service to be used when writing integration/end-to-end tests for applications or libraries that access these services.
3
+
4
+ Note: this is still very early in its development; don't let the existence of this README fool you into thinking its ready for prime-time!
5
+
6
+ ## Why not stub?
7
+ There are already some good tools, like [FakeWeb](http://fakeweb.rubyforge.org/) which let you stub requests at a low-level which is fine for unit and functional tests but when exercising our code through integration or end-to-end tests we want to exercise as much of the stack as possible.
8
+
9
+ Mimic aims to make it possible to test your networking code without actually hitting the real services by starting up a real web server and responding to HTTP requests. This lets you test your application against canned responses in an as-close-to-the-real-thing-as-possible way.
10
+
11
+ Also, because Mimic starts responds to real HTTP requests, it can be used when testing non-Ruby applications too (like an iPhone application).
12
+
13
+ ## How does it work?
14
+
15
+ Mimic's API is designed to be simple yet expressive. You simply register the host that you want to fake and then register any number of requests and responses. Mimic will then start an HTTP server (using Rack and WEBRick) on the specified port and add an entry to your hosts file (OSX and Linux only) for that host.
16
+
17
+ ## Examples
18
+
19
+ Registering to a single request stub:
20
+
21
+ Mimic.mimic("www.example.com", 10090).get("/some/path").returning("hello world")
22
+
23
+ And the result, using RestClient:
24
+
25
+ $ RestClient.get("http://www.example.com:10090/some/path") # => 200 | hello world
26
+
27
+ Registering multiple request stubs; note that you can stub the same path with different HTTP methods separately.
28
+
29
+ Mimic.mimic("www.example.com", 10090) do
30
+ get("/some/path").returning("Hello World", 200)
31
+ get("/some/other/path").returning("Redirecting...", 301, {"Location" => "somewhere else"})
32
+ post("/some/path").returning("Created!", 201)
33
+ end
34
+
35
+ ## Caveats
36
+
37
+ * Because the server is not started on port 80, you need a way of configuring your application under test to use a different port at runtime.
38
+
39
+ * Mimic uses the Ghost gem to modify the hosts file and is therefore dependent on support for this gem. In addition, Ghost currently requires sudo privileges to run which means entering your password. I'm happy to hear any suggestions for working around this.
40
+
41
+ ## License
42
+
43
+ As usual, the code is released under the MIT license which is included in the repository.
data/Rakefile ADDED
@@ -0,0 +1,83 @@
1
+ require 'rubygems'
2
+ require 'cucumber'
3
+ require 'cucumber/rake/task'
4
+ require 'spec/rake/spectask'
5
+
6
+ desc "Run all Cucumber features"
7
+ Cucumber::Rake::Task.new(:features) do |t|
8
+ t.cucumber_opts = "features --format pretty"
9
+ end
10
+
11
+ desc "Run all specs"
12
+ Spec::Rake::SpecTask.new('specs') do |t|
13
+ t.spec_files = FileList['spec/**/*.rb']
14
+ end
15
+
16
+ task :default => :specs
17
+
18
+ require "rake/gempackagetask"
19
+ require "rake/rdoctask"
20
+
21
+ # This builds the actual gem. For details of what all these options
22
+ # mean, and other ones you can add, check the documentation here:
23
+ #
24
+ # http://rubygems.org/read/chapter/20
25
+ #
26
+ spec = Gem::Specification.new do |s|
27
+
28
+ # Change these as appropriate
29
+ s.name = "mimic"
30
+ s.version = "0.1.0"
31
+ s.summary = "A Ruby gem for faking external web services for testing"
32
+ s.author = "Luke Redpath"
33
+ s.email = "luke@lukeredpath.co.uk"
34
+ s.homepage = "http://lukeredpath.co.uk"
35
+
36
+ s.has_rdoc = true
37
+ s.extra_rdoc_files = %w(README.md)
38
+ s.rdoc_options = %w(--main README.md)
39
+
40
+ # Add any extra files to include in the gem
41
+ s.files = %w(LICENSE Rakefile README.md) + Dir.glob("{spec,lib/**/*}")
42
+ s.require_paths = ["lib"]
43
+
44
+ # If you want to depend on other gems, add them here, along with any
45
+ # relevant versions
46
+ s.add_dependency("ghost", "~> 0.2.5")
47
+ s.add_dependency("rack", "~> 1.2.1")
48
+
49
+ # If your tests use any gems, include them here
50
+ s.add_development_dependency("rspec")
51
+ s.add_development_dependency("cucumber")
52
+ end
53
+
54
+ # This task actually builds the gem. We also regenerate a static
55
+ # .gemspec file, which is useful if something (i.e. GitHub) will
56
+ # be automatically building a gem for this project. If you're not
57
+ # using GitHub, edit as appropriate.
58
+ #
59
+ # To publish your gem online, install the 'gemcutter' gem; Read more
60
+ # about that here: http://gemcutter.org/pages/gem_docs
61
+ Rake::GemPackageTask.new(spec) do |pkg|
62
+ pkg.gem_spec = spec
63
+ end
64
+
65
+ desc "Build the gemspec file #{spec.name}.gemspec"
66
+ task :gemspec do
67
+ file = File.dirname(__FILE__) + "/#{spec.name}.gemspec"
68
+ File.open(file, "w") {|f| f << spec.to_ruby }
69
+ end
70
+
71
+ task :package => :gemspec
72
+
73
+ # Generate documentation
74
+ Rake::RDocTask.new do |rd|
75
+ rd.main = "README.md"
76
+ rd.rdoc_files.include("README.md", "lib/**/*.rb")
77
+ rd.rdoc_dir = "rdoc"
78
+ end
79
+
80
+ desc 'Clear out RDoc and generated packages'
81
+ task :clean => [:clobber_rdoc, :clobber_package] do
82
+ rm "#{spec.name}.gemspec"
83
+ end
data/lib/mimic.rb ADDED
@@ -0,0 +1,75 @@
1
+ require 'mimic/fake_host'
2
+ require 'singleton'
3
+ require 'rack'
4
+ require 'ghost'
5
+ require 'logger'
6
+
7
+ module Mimic
8
+ MIMIC_DEFAULT_PORT = 11988
9
+
10
+ def self.mimic(hostname, port = MIMIC_DEFAULT_PORT, &block)
11
+ FakeHost.new(hostname).tap do |host|
12
+ registry.register_host(host)
13
+ host.instance_eval(&block) if block_given?
14
+ Server.instance.serve(host, port)
15
+ end
16
+ end
17
+
18
+ def self.cleanup!
19
+ Mimic::Server.instance.shutdown
20
+ registry.unregister_all_hosts
21
+ end
22
+
23
+ private
24
+
25
+ def self.registry
26
+ @registry ||= Registry.new
27
+ end
28
+
29
+ class Server
30
+ include Singleton
31
+
32
+ def logger
33
+ @logger ||= Logger.new(StringIO.new)
34
+ end
35
+
36
+ def serve(host_app, port)
37
+ webrick_logger = logger
38
+ @thread = Thread.fork do
39
+ Rack::Handler::WEBrick.run(host_app,
40
+ :Port => port,
41
+ :Logger => webrick_logger,
42
+ :AccessLog => webrick_logger
43
+
44
+ ) { |server| @server = server }
45
+ end
46
+ end
47
+
48
+ def shutdown
49
+ if @thread
50
+ Thread.kill(@thread)
51
+ @server.shutdown
52
+ end
53
+ end
54
+ end
55
+
56
+ class Registry
57
+ def initialize
58
+ @hosts = []
59
+ end
60
+
61
+ def register_host(host)
62
+ @hosts << host
63
+ Host.add(host.hostname)
64
+ end
65
+
66
+ def unregister_host(host)
67
+ @hosts.delete(host)
68
+ Host.delete(host.hostname)
69
+ end
70
+
71
+ def unregister_all_hosts
72
+ @hosts.each { |host| unregister_host(host) }
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,90 @@
1
+ module Mimic
2
+ class FakeHost
3
+ attr_reader :hostname
4
+
5
+ def initialize(hostname, unhandled_response_strategy = NotFoundResponseStrategy.new)
6
+ @hostname = hostname
7
+ @stubs = {}
8
+ @unhandled_response_strategy = unhandled_response_strategy
9
+ end
10
+
11
+ def get(path)
12
+ request("GET", path)
13
+ end
14
+
15
+ def post(path)
16
+ request("POST", path)
17
+ end
18
+
19
+ def put(path)
20
+ request("PUT", path)
21
+ end
22
+
23
+ def delete(path)
24
+ request("DELETE", path)
25
+ end
26
+
27
+ def head(path)
28
+ request("HEAD", path)
29
+ end
30
+
31
+ def call(env)
32
+ handler_for_call(env).call(env)
33
+ end
34
+
35
+ def terminate
36
+ Mimic.terminate(@hostname)
37
+ end
38
+
39
+ private
40
+
41
+ def request(method, path)
42
+ stubbed_request = StubbedRequest.new(method, path)
43
+ @stubs[stubbed_request.key] = stubbed_request
44
+ end
45
+
46
+ def handler_for_call(env)
47
+ (@stubs[request_key(env)] || @unhandled_response_strategy)
48
+ end
49
+
50
+ def request_key(env)
51
+ request = Rack::Request.new(env)
52
+ StubbedRequest.key(request.request_method, request.path)
53
+ end
54
+
55
+ class StubbedRequest
56
+ def initialize(method, path)
57
+ @method, @path = method, path
58
+ @code = 200
59
+ @headers = {}
60
+ @body = ""
61
+ end
62
+
63
+ def returning(body, code = 200, headers = {})
64
+ tap do
65
+ @body = body
66
+ @code = code
67
+ @headers = headers
68
+ end
69
+ end
70
+
71
+ def call(env)
72
+ [@code, @headers, @body]
73
+ end
74
+
75
+ def key
76
+ self.class.key(@method, @path)
77
+ end
78
+
79
+ def self.key(method, path)
80
+ "#{method} #{path}"
81
+ end
82
+ end
83
+
84
+ class NotFoundResponseStrategy
85
+ def call(env)
86
+ [404, {}, ""]
87
+ end
88
+ end
89
+ end
90
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mimic
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Luke Redpath
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-08-19 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: ghost
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 29
30
+ segments:
31
+ - 0
32
+ - 2
33
+ - 5
34
+ version: 0.2.5
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: rack
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ hash: 29
46
+ segments:
47
+ - 1
48
+ - 2
49
+ - 1
50
+ version: 1.2.1
51
+ type: :runtime
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: rspec
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 3
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ type: :development
66
+ version_requirements: *id003
67
+ - !ruby/object:Gem::Dependency
68
+ name: cucumber
69
+ prerelease: false
70
+ requirement: &id004 !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ hash: 3
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ type: :development
80
+ version_requirements: *id004
81
+ description:
82
+ email: luke@lukeredpath.co.uk
83
+ executables: []
84
+
85
+ extensions: []
86
+
87
+ extra_rdoc_files:
88
+ - README.md
89
+ files:
90
+ - LICENSE
91
+ - Rakefile
92
+ - README.md
93
+ - lib/mimic/fake_host.rb
94
+ - lib/mimic.rb
95
+ has_rdoc: true
96
+ homepage: http://lukeredpath.co.uk
97
+ licenses: []
98
+
99
+ post_install_message:
100
+ rdoc_options:
101
+ - --main
102
+ - README.md
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ hash: 3
111
+ segments:
112
+ - 0
113
+ version: "0"
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ none: false
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ hash: 3
120
+ segments:
121
+ - 0
122
+ version: "0"
123
+ requirements: []
124
+
125
+ rubyforge_project:
126
+ rubygems_version: 1.3.7
127
+ signing_key:
128
+ specification_version: 3
129
+ summary: A Ruby gem for faking external web services for testing
130
+ test_files: []
131
+