mimic2 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e90ca1fced2c9d223f2208fbd0aebca0aee0838c
4
+ data.tar.gz: 4d8d6ffe8e8e788b39cb5aaeb7444d43052af5ef
5
+ SHA512:
6
+ metadata.gz: 4b806edf1ee478b42be8aa49fee1f479216906928a9c87ad574e604b2e7c9e0a72ba25d7c7ea575d30ac7a8cae19136c7e72cde28ecc74ef2583f683412fb587
7
+ data.tar.gz: dc3f1bf7023bce28245dae4017aabfa8d68c045e1d6b35dc5fe122f791aa387350cb06c80c66e677ee332c999c83b93860f25788f0f727927bbcd1195e8559df
data/CHANGES ADDED
@@ -0,0 +1,31 @@
1
+ ## 0.4.3
2
+
3
+ * Added request logging support.
4
+
5
+ ## 0.4.2
6
+
7
+ * Remove unnecessary signal traps.
8
+
9
+ ## 0.4.1
10
+
11
+ * Fixed bug where stubs wouldn't actually be cleared correctly.
12
+
13
+ ## 0.4.0
14
+
15
+ * Mimic can now be run in it's own process and configured externally using a REST API.
16
+ * Mimic can be run using the Daemons gem safely (pass :fork => false to disable built-in forking).
17
+ * All existing stubs can be cleared.
18
+
19
+ ## 0.3.0
20
+
21
+ * All verb methods (get, post etc.) can take blocks
22
+
23
+ ## 0.2.0
24
+
25
+ * Added support for using Rack middleware
26
+ * Removed host file modification feature
27
+ * Refactor code top build on top of Sinatra::Base
28
+
29
+ ## 0.1.0
30
+
31
+ * Initial release
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,107 @@
1
+ # Mimic, simple web service stubs for testing [![Build Status](https://secure.travis-ci.org/lukeredpath/mimic.png)](https://secure.travis-ci.org/lukeredpath/mimic)
2
+
3
+ ## What is Mimic?
4
+ 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.
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 responds to real HTTP requests, it can be used when testing non-Ruby applications too.
12
+
13
+ ## Examples
14
+ Registering to a single request stub:
15
+ ```
16
+ Mimic.mimic.get("/some/path").returning("hello world")
17
+ ```
18
+ And the result, using RestClient:
19
+ ```
20
+ $ RestClient.get("http://www.example.com:11988/some/path") # => 200 | hello world
21
+ ```
22
+ Registering multiple request stubs; note that you can stub the same path with different HTTP methods separately.
23
+ ```
24
+ Mimic.mimic do
25
+ get("/some/path").returning("Hello World", 200)
26
+ get("/some/other/path").returning("Redirecting...", 301, {"Location" => "somewhere else"})
27
+ post("/some/path").returning("Created!", 201)
28
+ end
29
+ ```
30
+ You can even use Rack middlewares, e.g. to handle common testing scenarios such as authentication:
31
+ ```
32
+ Mimic.mimic do
33
+ use Rack::Auth::Basic do |user, pass|
34
+ user == 'theuser' and pass == 'thepass'
35
+ end
36
+
37
+ get("/some/path")
38
+ end
39
+ ```
40
+ Finally, because Mimic is built on top of Sinatra for the core request handling, you can create your stubbed requests like you would in any Sinatra app:
41
+ ```
42
+ Mimic.mimic do
43
+ get "/some/path" do
44
+ [200, {}, "hello world"]
45
+ end
46
+ end
47
+ ```
48
+
49
+ ## Using Mimic with non-Ruby processes
50
+ Mimic has a built-in REST API that lets you configure your request stubs over HTTP. This makes it possible to use Mimic from other processes that can perform HTTP requests.
51
+
52
+ First of all, you'll need to run Mimic as a daemon. You can do this with a simple Ruby script and the [daemons](http://daemons.rubyforge.org/) gem:
53
+ ```
54
+ #!/usr/bin/env ruby
55
+ require 'mimic'
56
+ require 'daemons'
57
+
58
+ Daemons.run_proc("mimic") do
59
+ Mimic.mimic(:port => 11988, :fork => false, :remote_configuration_path => '/api') do
60
+ # configure your stubs here
61
+ end
62
+ end
63
+ ```
64
+
65
+ Give the script executable permissions and then start it:
66
+ ```
67
+ $ your_mimic_script.rb start (or run)
68
+ ```
69
+ The remote configuration path is where the API endpoints will be mounted - this is configurable as you will not be able this path or any paths below it in your stubs, so choose one that doesn't conflict with the paths you need to stub.
70
+
71
+ The API supports both JSON and Plist payloads, defaulting to JSON. Set the request Content-Type header to application/plist for Plist requests.
72
+
73
+ For the following Mimic configuration (using the Ruby DSL):
74
+ ```
75
+ Mimic.mimic.get("/some/path").returning("hello world")
76
+ ```
77
+
78
+ The equivalent stub can be configured using the REST API as follows:
79
+ ```
80
+ $ curl -d'{"path":"/some/path", "body":"hello world"}' http://localhost:11988/api/get
81
+ ```
82
+
83
+ Likewise, a POST request to the same path could be stubbed like so:
84
+ ```
85
+ $ curl -d'{"path":"/some/path", "body":"hello world"}' http://localhost:11988/api/post
86
+ ```
87
+
88
+ The end-point of the API is the HTTP verb you are stubbing, the path, response body, code and headers are specified in the POST data (a hash in JSON or Plist format). See the HTTP API Cucumber features for more examples.
89
+
90
+ An [Objective-C wrapper](http://github.com/lukeredpath/LRMimic) for the REST API is available, allowing you to use mimic for your OSX and iOS apps.
91
+
92
+ ## Binding mimic to all ip addresses
93
+ For security mimic only binds to localhost, should you require access from another computer you can tell mimic to bind to a particular or all ip addresses.
94
+ ```
95
+ Mimic.mimic(:Host => '0.0.0.0', :port => 11988) do
96
+ # configure your stubs here
97
+ end
98
+ ```
99
+
100
+ ## Contributors
101
+
102
+ * [James Fairbairn](http://github.com/jfairbairn)
103
+ * [Marcello Barnaba](https://github.com/vjt)
104
+
105
+ ## License
106
+
107
+ As usual, the code is released under the MIT license which is included in the repository.
data/Rakefile ADDED
@@ -0,0 +1,100 @@
1
+ require 'rubygems'
2
+ require 'cucumber'
3
+ require 'cucumber/rake/task'
4
+ require 'rspec/core/rake_task'
5
+
6
+ ENV["MIMIC_TEST_PROXY"] = nil
7
+
8
+ desc "Run all Cucumber features"
9
+ Cucumber::Rake::Task.new(:features) do |t|
10
+ t.cucumber_opts = "features --format pretty"
11
+ end
12
+
13
+ desc "Run all specs"
14
+ RSpec::Core::RakeTask.new(:spec) do |t|
15
+ end
16
+
17
+ task :default => :spec
18
+ task :all => [:spec, :features]
19
+
20
+ require "rubygems/package_task"
21
+ require "rdoc/task"
22
+
23
+ # This builds the actual gem. For details of what all these options
24
+ # mean, and other ones you can add, check the documentation here:
25
+ #
26
+ # http://rubygems.org/read/chapter/20
27
+ #
28
+ spec = Gem::Specification.new do |s|
29
+
30
+ # Change these as appropriate
31
+ s.name = "mimic2"
32
+ s.version = "2.0.0"
33
+ s.summary = "A Ruby gem for faking external web services for testing, forked and updated version of Luke Redpath's mimic"
34
+ s.authors = "Luke Redpath, Nic Jackson"
35
+ s.email = "jackson.nic@gmail.com, luke@lukeredpath.co.uk"
36
+ s.homepage = "https://github.com/nicholasjackson/mimic"
37
+
38
+ s.has_rdoc = true
39
+ s.extra_rdoc_files = %w(README.md)
40
+ s.rdoc_options = %w(--main README.md)
41
+
42
+ # Add any extra files to include in the gem
43
+ s.files = %w(LICENSE CHANGES Rakefile README.md) + Dir.glob("{spec,lib/**/*}")
44
+ s.require_paths = ["lib"]
45
+
46
+ # If you want to depend on other gems, add them here, along with any
47
+ # relevant versions
48
+ s.add_dependency("rack")
49
+ s.add_dependency("sinatra")
50
+ s.add_dependency("thin")
51
+ s.add_dependency("json")
52
+ s.add_dependency("plist", "~> 3.1.0")
53
+
54
+ # If your tests use any gems, include them here
55
+ s.add_development_dependency("rspec", "~> 3.3.0")
56
+ s.add_development_dependency("cucumber", "~> 2.0.2")
57
+ s.add_development_dependency("mocha")
58
+ s.add_development_dependency("rest-client")
59
+ end
60
+
61
+ # This task actually builds the gem. We also regenerate a static
62
+ # .gemspec file, which is useful if something (i.e. GitHub) will
63
+ # be automatically building a gem for this project. If you're not
64
+ # using GitHub, edit as appropriate.
65
+ #
66
+ # To publish your gem online, install the 'gemcutter' gem; Read more
67
+ # about that here: http://gemcutter.org/pages/gem_docs
68
+ Gem::PackageTask.new(spec) do |pkg|
69
+ pkg.gem_spec = spec
70
+ end
71
+
72
+ desc "Build the gemspec file #{spec.name}.gemspec"
73
+ task :gemspec do
74
+ file = File.dirname(__FILE__) + "/#{spec.name}.gemspec"
75
+ File.open(file, "w") {|f| f << spec.to_ruby }
76
+ end
77
+
78
+ task :package => :gemspec
79
+
80
+ # Generate documentation
81
+ RDoc::Task.new do |rd|
82
+ rd.main = "README.md"
83
+ rd.rdoc_files.include("README.md", "lib/**/*.rb")
84
+ rd.rdoc_dir = "rdoc"
85
+ end
86
+
87
+ desc 'Clear out RDoc and generated packages'
88
+ task :clean => [:clobber_rdoc, :clobber_package] do
89
+ rm "#{spec.name}.gemspec" if File.exists? "#{spec.name}.gemspec"
90
+ end
91
+
92
+ task 'Release if all specs pass'
93
+ task :release => [:clean, :bundle, :spec, :features, :package] do
94
+ system("gem push pkg/#{spec.name}-#{spec.version}.gem")
95
+ end
96
+
97
+ desc 'Install all gem dependencies'
98
+ task :bundle => :gemspec do
99
+ system("bundle")
100
+ end
@@ -0,0 +1,7 @@
1
+ require 'helpers/request_echo'
2
+
3
+ module Helpers
4
+ def echo_request!(format)
5
+ RequestEcho.new(request).response_as(format)
6
+ end
7
+ end
@@ -0,0 +1,42 @@
1
+ module Helpers
2
+ class RequestEcho
3
+ def initialize(request)
4
+ @request = request
5
+ end
6
+
7
+ def response_as(format)
8
+ content_type = case format
9
+ when :json, :plist
10
+ "application/#{format.to_s.downcase}"
11
+ else
12
+ "text/plain"
13
+ end
14
+ [200, {"Content-Type" => content_type}, to_s(format)]
15
+ end
16
+
17
+ def to_s(format)
18
+ case format
19
+ when :json
20
+ to_hash.to_json
21
+ when :plist
22
+ to_hash.to_plist
23
+ when :text
24
+ to_hash.inspect
25
+ end
26
+ end
27
+
28
+ def to_hash
29
+ {"echo" => {
30
+ "params" => @request.params,
31
+ "env" => env_without_rack_and_async_env,
32
+ "body" => @request.body.read
33
+ }}
34
+ end
35
+
36
+ private
37
+
38
+ def env_without_rack_and_async_env
39
+ Hash[*@request.env.select { |key, value| key !~ /^(rack|async)/i }.flatten]
40
+ end
41
+ end
42
+ end
data/lib/mimic.rb ADDED
@@ -0,0 +1,40 @@
1
+ require 'mimic/fake_host'
2
+ require 'singleton'
3
+ require 'rack'
4
+ require 'logger'
5
+
6
+ module Mimic
7
+ MIMIC_DEFAULT_PORT = 11988
8
+
9
+ MIMIC_DEFAULT_OPTIONS = {
10
+ :hostname => 'localhost',
11
+ :port => MIMIC_DEFAULT_PORT,
12
+ :remote_configuration_path => nil,
13
+ :fork => true,
14
+ :log => nil
15
+ }
16
+
17
+ def self.mimic(options = {}, &block)
18
+ options = MIMIC_DEFAULT_OPTIONS.merge(options)
19
+ host = FakeHost.new(options)
20
+ host.instance_eval(&block) if block_given?
21
+
22
+ Server.instance.serve(host, options)
23
+
24
+ add_host(host)
25
+ end
26
+
27
+ def self.cleanup!
28
+ Mimic::Server.instance.shutdown
29
+ end
30
+
31
+ def self.reset_all!
32
+ @hosts.each { |h| h.clear }
33
+ end
34
+
35
+ private
36
+
37
+ def self.add_host(host)
38
+ host.tap { |h| (@hosts ||= []) << h }
39
+ end
40
+ end
data/lib/mimic/api.rb ADDED
@@ -0,0 +1,65 @@
1
+ require 'json'
2
+ require 'plist'
3
+ require 'mimic/api_request'
4
+
5
+ module Mimic
6
+ class API < Sinatra::Base
7
+ class << self
8
+ attr_accessor :host
9
+ end
10
+
11
+ def host
12
+ self.class.host
13
+ end
14
+
15
+ post "/get" do
16
+ process_request request, 'get'
17
+ end
18
+
19
+ post "/post" do
20
+ process_request request, 'post'
21
+ end
22
+
23
+ post "/put" do
24
+ process_request request, 'put'
25
+ end
26
+
27
+ post "/delete" do
28
+ process_request request, 'delete'
29
+ end
30
+
31
+ post "/head" do
32
+ process_request request, 'head'
33
+ end
34
+
35
+ post "/multi" do
36
+ api_request = APIRequest.from_request(request)
37
+ api_request.setup_stubs_on(host)
38
+ [201, {"Content-Type" => api_request.request_content_type}, api_request.response]
39
+ end
40
+
41
+ post "/clear" do
42
+ response_body = self.host.inspect
43
+ self.host.clear
44
+ [200, {}, "Cleared stubs: #{response_body}"]
45
+ end
46
+
47
+ get "/ping" do
48
+ [200, {}, "OK"]
49
+ end
50
+
51
+ get "/debug" do
52
+ [200, {}, self.host.inspect]
53
+ end
54
+
55
+ get "/requests" do
56
+ [200, {"Content-Type" => "application/json"}, {"requests" => host.received_requests.map(&:to_hash)}.to_json]
57
+ end
58
+
59
+ def process_request request, verb
60
+ api_request = APIRequest.from_request(request, verb)
61
+ api_request.setup_stubs_on(host)
62
+ [201, {"Content-Type" => api_request.request_content_type}, api_request.response]
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,51 @@
1
+ require 'mimic/stub'
2
+
3
+ module Mimic
4
+ class APIRequest
5
+ attr_reader :request_content_type
6
+
7
+ def initialize(data, method = nil, request_content_type = '')
8
+ @data = data
9
+ @method = (method || "GET")
10
+ @stubs = []
11
+ @request_content_type = request_content_type
12
+ end
13
+
14
+ def to_s
15
+ @data.inspect
16
+ end
17
+
18
+ def response
19
+ response = {"stubs" => @stubs.map(&:to_hash)}
20
+
21
+ case request_content_type
22
+ when /json/
23
+ response.to_json
24
+ when /plist/
25
+ response.to_plist
26
+ else
27
+ response.to_json
28
+ end
29
+ end
30
+
31
+ def self.from_request(request, method = nil)
32
+ case request.content_type
33
+ when /json/
34
+ data = JSON.parse(request.body.string)
35
+ when /plist/
36
+ data = Plist.parse_xml(request.body.string)
37
+ else
38
+ data = request.body.string && request.body.string.length >= 2 ? JSON.parse(request.body.string) : {}
39
+ end
40
+
41
+ new(data, method, request.content_type)
42
+ end
43
+
44
+ def setup_stubs_on(host)
45
+ (@data["stubs"] || [@data]).each do |stub_data|
46
+ @stubs << Stub.new(stub_data, stub_data['method'] || @method).on(host)
47
+ end
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,105 @@
1
+ require 'sinatra/base'
2
+ require 'mimic/api'
3
+ require 'mimic/server'
4
+ require 'mimic/stubbed_request'
5
+ require 'helpers/helpers'
6
+
7
+ module Mimic
8
+ class FakeHost
9
+ attr_reader :hostname, :url_map
10
+ attr_accessor :log
11
+
12
+ def initialize(options = {})
13
+ @hostname = options[:hostname]
14
+ @remote_configuration_path = options[:remote_configuration_path]
15
+ @log = options[:log]
16
+ @imports = []
17
+ setup_sinatra
18
+ build_url_map!
19
+ end
20
+
21
+ def received_requests
22
+ @stubs.select { |s| s.received }
23
+ end
24
+
25
+ def get(path, &block)
26
+ setup_request_stub("GET", path, &block)
27
+ end
28
+
29
+ def post(path, &block)
30
+ setup_request_stub("POST", path, &block)
31
+ end
32
+
33
+ def put(path, &block)
34
+ setup_request_stub("PUT", path, &block)
35
+ end
36
+
37
+ def delete(path, &block)
38
+ setup_request_stub("DELETE", path, &block)
39
+ end
40
+
41
+ def head(path, &block)
42
+ setup_request_stub("HEAD", path, &block)
43
+ end
44
+
45
+ def import(path)
46
+ if File.exists?(path)
47
+ @imports << path unless @imports.include?(path)
48
+ instance_eval(File.read(path))
49
+ else
50
+ raise "Could not locate file for stub import: #{path}"
51
+ end
52
+ end
53
+
54
+ def call(env)
55
+ @stubs.each(&:build)
56
+ @app.call(env) # call next middleware in chain
57
+ end
58
+
59
+ def setup_sinatra
60
+ @stubs = []
61
+ @app = Sinatra.new
62
+ @app.use Rack::CommonLogger, self.log if self.log
63
+ @app.not_found do
64
+ [404, {}, ""]
65
+ end
66
+ @app.helpers do
67
+ include Helpers
68
+ end
69
+ @imports.each { |file| import(file) }
70
+ end
71
+
72
+ def clear
73
+ setup_sinatra
74
+ end
75
+
76
+ def inspect
77
+ @stubs.inspect
78
+ end
79
+
80
+ private
81
+ def method_missing(method, *args, &block)
82
+ @app.send(method, *args, &block)
83
+ end
84
+
85
+ def setup_request_stub(method, path, &block)
86
+ if block_given?
87
+ @app.send(method.downcase, path, &block)
88
+ else
89
+ @stubs << StubbedRequest.new(@app, method, path)
90
+ @stubs.last
91
+ end
92
+ end
93
+
94
+ def build_url_map!
95
+ routes = {'/' => self}
96
+
97
+ if @remote_configuration_path
98
+ API.host = self
99
+ routes[@remote_configuration_path] = API
100
+ end
101
+
102
+ @url_map = Rack::URLMap.new(routes)
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,61 @@
1
+ require 'singleton'
2
+
3
+ module Mimic
4
+ class Server
5
+ include Singleton
6
+
7
+ def logger
8
+ @logger ||= Logger.new(StringIO.new)
9
+ end
10
+
11
+ def serve(app, options)
12
+ if options[:fork]
13
+ @thread = Thread.fork do
14
+ start_service(app, options)
15
+ end
16
+
17
+ wait_for_service(app.hostname, options[:port])
18
+
19
+ else
20
+ start_service(app, options)
21
+ end
22
+ end
23
+
24
+ def start_service(app, options)
25
+ Rack::Handler::Thin.run(app.url_map, {
26
+ :Host => options[:Host] || 'localhost',
27
+ :Port => options[:port],
28
+ :Logger => logger,
29
+ :AccessLog => logger
30
+ })
31
+ end
32
+
33
+ def shutdown
34
+ Thread.kill(@thread) if @thread
35
+ end
36
+
37
+ # courtesy of http://is.gd/eoYho
38
+
39
+ def listening?(host, port)
40
+ begin
41
+ socket = TCPSocket.new(host, port)
42
+ socket.close unless socket.nil?
43
+ true
44
+ rescue Errno::ECONNREFUSED, SocketError,
45
+ Errno::EBADF, # Windows
46
+ Errno::EADDRNOTAVAIL # Windows
47
+ false
48
+ end
49
+ end
50
+
51
+ def wait_for_service(host, port, timeout = 5)
52
+ start_time = Time.now
53
+
54
+ until listening?(host, port)
55
+ if timeout && (Time.now > (start_time + timeout))
56
+ raise SocketError.new("Socket did not open within #{timeout} seconds")
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
data/lib/mimic/stub.rb ADDED
@@ -0,0 +1,44 @@
1
+ module Mimic
2
+ class Stub
3
+ def initialize(data, method = nil)
4
+ @data = data
5
+ @method = method
6
+ end
7
+
8
+ def on(host)
9
+ get_stub(host).tap do |stub|
10
+ stub.returning(body, code, headers)
11
+ stub.with_query_parameters(params)
12
+ stub.echo_request!(echo_format)
13
+ end
14
+ end
15
+
16
+ def get_stub(host)
17
+ host.send(@method.downcase.to_sym, path)
18
+ end
19
+
20
+ def echo_format
21
+ @data['echo'].to_sym rescue nil
22
+ end
23
+
24
+ def path
25
+ @data['path'] || '/'
26
+ end
27
+
28
+ def body
29
+ @data['body'] || ''
30
+ end
31
+
32
+ def code
33
+ @data['code'] || 200
34
+ end
35
+
36
+ def headers
37
+ @data['headers'] || {}
38
+ end
39
+
40
+ def params
41
+ @data['params'] || {}
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,73 @@
1
+ require 'helpers/request_echo'
2
+
3
+ module Mimic
4
+ class StubbedRequest
5
+ attr_accessor :received
6
+
7
+ def initialize(app, method, path)
8
+ @method, @path = method, path
9
+ @code = 200
10
+ @headers = {}
11
+ @params = {}
12
+ @body = ""
13
+ @app = app
14
+ @received = false
15
+ end
16
+
17
+ def to_hash
18
+ token = "#{@method} #{@path}"
19
+ Digest::MD5.hexdigest(token)
20
+ end
21
+
22
+ def returning(body, code = 200, headers = {})
23
+ tap do
24
+ @body = body
25
+ @code = code
26
+ @headers = headers
27
+ end
28
+ end
29
+
30
+ def with_query_parameters(params)
31
+ tap do
32
+ @params = params
33
+ end
34
+ end
35
+
36
+ def echo_request!(format = :json)
37
+ @echo_request_format = format
38
+ end
39
+
40
+ def matches?(request)
41
+ if @params.any?
42
+ request.params == @params
43
+ else
44
+ true
45
+ end
46
+ end
47
+
48
+ def matched_response
49
+ [@code, @headers, @body]
50
+ end
51
+
52
+ def unmatched_response
53
+ [404, {}, ""]
54
+ end
55
+
56
+ def response_for_request(request)
57
+ if @echo_request_format
58
+ @body = Helpers::RequestEcho.new(request).to_s(@echo_request_format)
59
+ end
60
+
61
+ matches?(request) ? matched_response : unmatched_response
62
+ end
63
+
64
+ def build
65
+ stub = self
66
+
67
+ @app.send(@method.downcase, @path) do
68
+ stub.received = true
69
+ stub.response_for_request(request)
70
+ end
71
+ end
72
+ end
73
+ end
metadata ADDED
@@ -0,0 +1,185 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mimic2
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Luke Redpath, Nic Jackson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sinatra
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: thin
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: json
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: plist
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 3.1.0
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 3.1.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 3.3.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 3.3.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: cucumber
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 2.0.2
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 2.0.2
111
+ - !ruby/object:Gem::Dependency
112
+ name: mocha
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rest-client
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description:
140
+ email: jackson.nic@gmail.com, luke@lukeredpath.co.uk
141
+ executables: []
142
+ extensions: []
143
+ extra_rdoc_files:
144
+ - README.md
145
+ files:
146
+ - CHANGES
147
+ - LICENSE
148
+ - README.md
149
+ - Rakefile
150
+ - lib/helpers/helpers.rb
151
+ - lib/helpers/request_echo.rb
152
+ - lib/mimic.rb
153
+ - lib/mimic/api.rb
154
+ - lib/mimic/api_request.rb
155
+ - lib/mimic/fake_host.rb
156
+ - lib/mimic/server.rb
157
+ - lib/mimic/stub.rb
158
+ - lib/mimic/stubbed_request.rb
159
+ homepage: https://github.com/nicholasjackson/mimic
160
+ licenses: []
161
+ metadata: {}
162
+ post_install_message:
163
+ rdoc_options:
164
+ - "--main"
165
+ - README.md
166
+ require_paths:
167
+ - lib
168
+ required_ruby_version: !ruby/object:Gem::Requirement
169
+ requirements:
170
+ - - ">="
171
+ - !ruby/object:Gem::Version
172
+ version: '0'
173
+ required_rubygems_version: !ruby/object:Gem::Requirement
174
+ requirements:
175
+ - - ">="
176
+ - !ruby/object:Gem::Version
177
+ version: '0'
178
+ requirements: []
179
+ rubyforge_project:
180
+ rubygems_version: 2.2.2
181
+ signing_key:
182
+ specification_version: 4
183
+ summary: A Ruby gem for faking external web services for testing, forked and updated
184
+ version of Luke Redpath's mimic
185
+ test_files: []