mimic 0.4.3 → 0.4.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8ddf333356aba7b3c5148a2019f6c011b765d688
4
+ data.tar.gz: f9acf8a652c4ee2ace5ad1ffa08fca3f5f7fb4d5
5
+ SHA512:
6
+ metadata.gz: cd9a2f1a1be1af588fab4de66b0f79b04645499af733d061b8490727a74e81286e059219e187f0a2000faa72e36f83f685972fd2a2d9b5d00d22053cf2bdde70
7
+ data.tar.gz: f0afe2d3f65d4dfce306feca21c03ff584105a504e93c550ad5ff3b9d1e0bfa00fc0ca11a2acc53be066dd162cd69eedce2b59bfe497d0328df7f50b47deb4d8
data/README.md CHANGED
@@ -1,4 +1,7 @@
1
- # What is Mimic?
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
+
2
5
  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
6
 
4
7
  ## Why not stub?
@@ -48,10 +51,17 @@ Finally, because Mimic is built on top of Sinatra for the core request handling,
48
51
 
49
52
  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.
50
53
 
51
- First of all, you'll need to run Mimic as a daemon. You can do this with a simple Ruby script:
54
+ 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:
52
55
 
53
56
  #!/usr/bin/env ruby
54
- Mimic.daemonize({:port => 11988, :remote_configuration_path => '/api'})
57
+ require 'mimic'
58
+ require 'daemons'
59
+
60
+ Daemons.run_proc("mimic") do
61
+ Mimic.mimic(:port => 11988, :fork => false, :remote_configuration_path => '/api') do
62
+ # configure your stubs here
63
+ end
64
+ end
55
65
 
56
66
  Give the script executable permissions and then start it:
57
67
 
@@ -75,6 +85,8 @@ Likewise, a POST request to the same path could be stubbed like so:
75
85
 
76
86
  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.
77
87
 
88
+ 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.
89
+
78
90
  ## Contributors
79
91
 
80
92
  * [James Fairbairn](http://github.com/jfairbairn)
data/Rakefile CHANGED
@@ -29,7 +29,7 @@ spec = Gem::Specification.new do |s|
29
29
 
30
30
  # Change these as appropriate
31
31
  s.name = "mimic"
32
- s.version = "0.4.3"
32
+ s.version = "0.4.4"
33
33
  s.summary = "A Ruby gem for faking external web services for testing"
34
34
  s.authors = "Luke Redpath"
35
35
  s.email = "luke@lukeredpath.co.uk"
@@ -47,11 +47,12 @@ spec = Gem::Specification.new do |s|
47
47
  # relevant versions
48
48
  s.add_dependency("rack")
49
49
  s.add_dependency("sinatra")
50
+ s.add_dependency("thin")
50
51
  s.add_dependency("json")
51
- s.add_dependency("plist")
52
+ s.add_dependency("plist", "~> 3.1.0")
52
53
 
53
54
  # If your tests use any gems, include them here
54
- s.add_development_dependency("rspec", "~> 2.4.0")
55
+ s.add_development_dependency("rspec")
55
56
  s.add_development_dependency("cucumber")
56
57
  s.add_development_dependency("mocha")
57
58
  s.add_development_dependency("rest-client")
@@ -2,6 +2,7 @@ require 'mimic/fake_host'
2
2
  require 'singleton'
3
3
  require 'rack'
4
4
  require 'logger'
5
+ require 'socket'
5
6
 
6
7
  module Mimic
7
8
  MIMIC_DEFAULT_PORT = 11988
@@ -11,7 +12,8 @@ module Mimic
11
12
  :port => MIMIC_DEFAULT_PORT,
12
13
  :remote_configuration_path => nil,
13
14
  :fork => true,
14
- :log => nil
15
+ :log => nil,
16
+ :wait_timeout => 5
15
17
  }
16
18
 
17
19
  def self.mimic(options = {}, &block)
@@ -51,7 +53,7 @@ module Mimic
51
53
  start_service(app, options)
52
54
  end
53
55
 
54
- wait_for_service(app.hostname, options[:port])
56
+ wait_for_service(app.hostname, options[:port], options[:wait_timeout])
55
57
 
56
58
  else
57
59
  start_service(app, options)
@@ -59,24 +61,15 @@ module Mimic
59
61
  end
60
62
 
61
63
  def start_service(app, options)
62
- Rack::Handler::WEBrick.run(app.url_map, {
64
+ Rack::Handler::Thin.run(app.url_map, {
63
65
  :Port => options[:port],
64
66
  :Logger => logger,
65
67
  :AccessLog => logger,
66
-
67
- }) do |server|
68
- @server = server
69
-
70
- old = trap('EXIT') do
71
- old.call if old
72
- @server.shutdown
73
- end
74
- end
68
+ })
75
69
  end
76
70
 
77
71
  def shutdown
78
72
  Thread.kill(@thread) if @thread
79
- @server.shutdown if @server
80
73
  end
81
74
 
82
75
  # courtesy of http://is.gd/eoYho
@@ -15,14 +15,14 @@ module Mimic
15
15
  post "/#{verb}" do
16
16
  api_request = APIRequest.from_request(request, verb)
17
17
  api_request.setup_stubs_on(host)
18
- [201, {}, api_request.to_s]
18
+ [201, {"Content-Type" => api_request.request_content_type}, api_request.response]
19
19
  end
20
20
  end
21
21
 
22
22
  post "/multi" do
23
23
  api_request = APIRequest.from_request(request)
24
24
  api_request.setup_stubs_on(host)
25
- [201, {}, api_request.to_s]
25
+ [201, {"Content-Type" => api_request.request_content_type}, api_request.response]
26
26
  end
27
27
 
28
28
  post "/clear" do
@@ -39,18 +39,39 @@ module Mimic
39
39
  [200, {}, self.host.inspect]
40
40
  end
41
41
 
42
+ get "/requests" do
43
+ [200, {"Content-Type" => "application/json"}, {"requests" => host.received_requests.map(&:to_hash)}.to_json]
44
+ end
45
+
42
46
  private
43
47
 
44
48
  class APIRequest
45
- def initialize(data, method = nil)
49
+ attr_reader :request_content_type
50
+
51
+ def initialize(data, method = nil, request_content_type = '')
46
52
  @data = data
47
53
  @method = (method || "GET")
54
+ @stubs = []
55
+ @request_content_type = request_content_type
48
56
  end
49
57
 
50
58
  def to_s
51
59
  @data.inspect
52
60
  end
53
61
 
62
+ def response
63
+ response = {"stubs" => @stubs.map(&:to_hash)}
64
+
65
+ case request_content_type
66
+ when /json/
67
+ response.to_json
68
+ when /plist/
69
+ response.to_plist
70
+ else
71
+ response.to_json
72
+ end
73
+ end
74
+
54
75
  def self.from_request(request, method = nil)
55
76
  case request.content_type
56
77
  when /json/
@@ -60,12 +81,12 @@ module Mimic
60
81
  else
61
82
  data = JSON.parse(request.body.string)
62
83
  end
63
- new(data, method)
84
+ new(data, method, request.content_type)
64
85
  end
65
86
 
66
87
  def setup_stubs_on(host)
67
88
  (@data["stubs"] || [@data]).each do |stub_data|
68
- Stub.new(stub_data, stub_data['method'] || @method).on(host)
89
+ @stubs << Stub.new(stub_data, stub_data['method'] || @method).on(host)
69
90
  end
70
91
  end
71
92
 
@@ -76,9 +97,10 @@ module Mimic
76
97
  end
77
98
 
78
99
  def on(host)
79
- stub = host.send(@method.downcase.to_sym, path).returning(body, code, headers)
80
- stub.with_query_parameters(params)
81
- stub.echo_request!(echo_format)
100
+ host.send(@method.downcase.to_sym, path).returning(body, code, headers).tap do |stub|
101
+ stub.with_query_parameters(params)
102
+ stub.echo_request!(echo_format)
103
+ end
82
104
  end
83
105
 
84
106
  def echo_format
@@ -15,6 +15,10 @@ module Mimic
15
15
  build_url_map!
16
16
  end
17
17
 
18
+ def received_requests
19
+ @stubs.select { |s| s.received }
20
+ end
21
+
18
22
  def get(path, &block)
19
23
  request("GET", path, &block)
20
24
  end
@@ -127,19 +131,21 @@ module Mimic
127
131
  def to_hash
128
132
  {"echo" => {
129
133
  "params" => @request.params,
130
- "env" => env_without_rack_env,
134
+ "env" => env_without_rack_and_async_env,
131
135
  "body" => @request.body.read
132
136
  }}
133
137
  end
134
138
 
135
139
  private
136
140
 
137
- def env_without_rack_env
138
- Hash[*@request.env.select { |key, value| key !~ /^rack/i }.flatten]
141
+ def env_without_rack_and_async_env
142
+ Hash[*@request.env.select { |key, value| key !~ /^(rack|async)/i }.flatten]
139
143
  end
140
144
  end
141
145
 
142
146
  class StubbedRequest
147
+ attr_accessor :received
148
+
143
149
  def initialize(app, method, path)
144
150
  @method, @path = method, path
145
151
  @code = 200
@@ -147,6 +153,12 @@ module Mimic
147
153
  @params = {}
148
154
  @body = ""
149
155
  @app = app
156
+ @received = false
157
+ end
158
+
159
+ def to_hash
160
+ token = "#{@method} #{@path}"
161
+ Digest::MD5.hexdigest(token)
150
162
  end
151
163
 
152
164
  def returning(body, code = 200, headers = {})
@@ -180,7 +192,7 @@ module Mimic
180
192
  end
181
193
 
182
194
  def unmatched_response
183
- [404, "", {}]
195
+ [404, {}, ""]
184
196
  end
185
197
 
186
198
  def response_for_request(request)
@@ -193,7 +205,11 @@ module Mimic
193
205
 
194
206
  def build
195
207
  stub = self
196
- @app.send(@method.downcase, @path) { stub.response_for_request(request) }
208
+
209
+ @app.send(@method.downcase, @path) do
210
+ stub.received = true
211
+ stub.response_for_request(request)
212
+ end
197
213
  end
198
214
  end
199
215
  end
metadata CHANGED
@@ -1,185 +1,178 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: mimic
3
- version: !ruby/object:Gem::Version
4
- hash: 9
5
- prerelease:
6
- segments:
7
- - 0
8
- - 4
9
- - 3
10
- version: 0.4.3
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.4
11
5
  platform: ruby
12
- authors:
6
+ authors:
13
7
  - Luke Redpath
14
8
  autorequire:
15
9
  bindir: bin
16
10
  cert_chain: []
17
-
18
- date: 2011-06-25 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
11
+ date: 2016-06-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
21
14
  name: rack
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
24
- none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- hash: 3
29
- segments:
30
- - 0
31
- version: "0"
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
32
20
  type: :runtime
33
- version_requirements: *id001
34
- - !ruby/object:Gem::Dependency
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
35
28
  name: sinatra
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
36
35
  prerelease: false
37
- requirement: &id002 !ruby/object:Gem::Requirement
38
- none: false
39
- requirements:
40
- - - ">="
41
- - !ruby/object:Gem::Version
42
- hash: 3
43
- segments:
44
- - 0
45
- version: "0"
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'
46
48
  type: :runtime
47
- version_requirements: *id002
48
- - !ruby/object:Gem::Dependency
49
- name: json
50
49
  prerelease: false
51
- requirement: &id003 !ruby/object:Gem::Requirement
52
- none: false
53
- requirements:
54
- - - ">="
55
- - !ruby/object:Gem::Version
56
- hash: 3
57
- segments:
58
- - 0
59
- version: "0"
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'
60
62
  type: :runtime
61
- version_requirements: *id003
62
- - !ruby/object:Gem::Dependency
63
- name: plist
64
63
  prerelease: false
65
- requirement: &id004 !ruby/object:Gem::Requirement
66
- none: false
67
- requirements:
68
- - - ">="
69
- - !ruby/object:Gem::Version
70
- hash: 3
71
- segments:
72
- - 0
73
- version: "0"
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
74
76
  type: :runtime
75
- version_requirements: *id004
76
- - !ruby/object:Gem::Dependency
77
- name: rspec
78
77
  prerelease: false
79
- requirement: &id005 !ruby/object:Gem::Requirement
80
- none: false
81
- requirements:
82
- - - ~>
83
- - !ruby/object:Gem::Version
84
- hash: 31
85
- segments:
86
- - 2
87
- - 4
88
- - 0
89
- version: 2.4.0
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: '0'
90
90
  type: :development
91
- version_requirements: *id005
92
- - !ruby/object:Gem::Dependency
93
- name: cucumber
94
91
  prerelease: false
95
- requirement: &id006 !ruby/object:Gem::Requirement
96
- none: false
97
- requirements:
98
- - - ">="
99
- - !ruby/object:Gem::Version
100
- hash: 3
101
- segments:
102
- - 0
103
- version: "0"
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '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: '0'
104
104
  type: :development
105
- version_requirements: *id006
106
- - !ruby/object:Gem::Dependency
107
- name: mocha
108
105
  prerelease: false
109
- requirement: &id007 !ruby/object:Gem::Requirement
110
- none: false
111
- requirements:
112
- - - ">="
113
- - !ruby/object:Gem::Version
114
- hash: 3
115
- segments:
116
- - 0
117
- version: "0"
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
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
118
  type: :development
119
- version_requirements: *id007
120
- - !ruby/object:Gem::Dependency
121
- name: rest-client
122
119
  prerelease: false
123
- requirement: &id008 !ruby/object:Gem::Requirement
124
- none: false
125
- requirements:
126
- - - ">="
127
- - !ruby/object:Gem::Version
128
- hash: 3
129
- segments:
130
- - 0
131
- version: "0"
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
132
  type: :development
133
- version_requirements: *id008
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
134
139
  description:
135
140
  email: luke@lukeredpath.co.uk
136
141
  executables: []
137
-
138
142
  extensions: []
139
-
140
- extra_rdoc_files:
143
+ extra_rdoc_files:
141
144
  - README.md
142
- files:
143
- - LICENSE
145
+ files:
144
146
  - CHANGES
145
- - Rakefile
147
+ - LICENSE
146
148
  - README.md
149
+ - Rakefile
150
+ - lib/mimic.rb
147
151
  - lib/mimic/api.rb
148
152
  - lib/mimic/fake_host.rb
149
- - lib/mimic.rb
150
153
  homepage: http://lukeredpath.co.uk
151
154
  licenses: []
152
-
155
+ metadata: {}
153
156
  post_install_message:
154
- rdoc_options:
155
- - --main
157
+ rdoc_options:
158
+ - "--main"
156
159
  - README.md
157
- require_paths:
160
+ require_paths:
158
161
  - lib
159
- required_ruby_version: !ruby/object:Gem::Requirement
160
- none: false
161
- requirements:
162
+ required_ruby_version: !ruby/object:Gem::Requirement
163
+ requirements:
162
164
  - - ">="
163
- - !ruby/object:Gem::Version
164
- hash: 3
165
- segments:
166
- - 0
167
- version: "0"
168
- required_rubygems_version: !ruby/object:Gem::Requirement
169
- none: false
170
- requirements:
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ required_rubygems_version: !ruby/object:Gem::Requirement
168
+ requirements:
171
169
  - - ">="
172
- - !ruby/object:Gem::Version
173
- hash: 3
174
- segments:
175
- - 0
176
- version: "0"
170
+ - !ruby/object:Gem::Version
171
+ version: '0'
177
172
  requirements: []
178
-
179
173
  rubyforge_project:
180
- rubygems_version: 1.8.5
174
+ rubygems_version: 2.4.5.1
181
175
  signing_key:
182
- specification_version: 3
176
+ specification_version: 4
183
177
  summary: A Ruby gem for faking external web services for testing
184
178
  test_files: []
185
-