mocktopus 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,166 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ class ResponseTest < Mocktopus::Test
5
+
6
+ def test_to_hash
7
+ input = build_input({})
8
+ hash = input.to_hash
9
+
10
+ assert_equal( '/uri', hash['uri'])
11
+ assert_equal( 'application/json', hash['headers']['Content-Type'])
12
+ assert_equal( '{}', hash['body'])
13
+ assert_equal( {}, hash['response'])
14
+ end
15
+
16
+ def test_to_hash_extra_fields
17
+ input = build_input({'nosuch' => 'extra'})
18
+ hash = input.to_hash
19
+
20
+ assert_equal(hash['nosuch'], nil)
21
+ end
22
+
23
+ def test_validate_input_errors
24
+ assert_raises Mocktopus::Input::ValidationError do
25
+ build_input({'uri' => '/[^\-_.!~*\'()a-zA-Z\d;\/?:@&=+$,\[\]]/
26
+ => /[^\-_.!~*\'()a-zA-Z\d;\/?:@&=+$,\[\]]/
27
+ irb(main):003:0> p Regexp.union([URI::Parser.new.regexp[:UNSAFE],\'~\',\'@\'])
28
+ /(?-mix:[^\-_.!~*\'()a-zA-Z\d;\/?:@&=+$,\[\]])|~|@/
29
+ => /(?-mix:[^\-_.!~*\'()a-zA-Z\d;\/?:@&'})
30
+ end
31
+
32
+ assert_raises Mocktopus::Input::ValidationError do
33
+ build_input({'uri' => 'irb(main):005:0*'})
34
+ end
35
+
36
+ assert_raises Mocktopus::Input::ValidationError do
37
+ build_input({'headers' => {nil => nil}})
38
+ end
39
+
40
+ build_input({'body' => nil})
41
+
42
+ assert_raises Mocktopus::Input::ValidationError do
43
+ build_input({}, "Not a response object")
44
+ end
45
+
46
+ assert_raises Mocktopus::Input::ValidationError do
47
+ build_input({'verb' => SecureRandom.uuid})
48
+ end
49
+
50
+ build_input({}, nil)
51
+ end
52
+
53
+ def test_flexible_uris
54
+ assert_equal(build_input({'uri' => '%C3%B6%C3%BF%C3%A0%C3%A1%C3%A4'}).to_hash, build_input({'uri' => 'öÿàáä'}).to_hash)
55
+ end
56
+
57
+ def test_to_hash_returns_properties_with_one_param
58
+ hash = {
59
+ 'uri' => '/test?parameter_one=foo',
60
+ 'headers' => {
61
+ 'Content-Type' => 'appilcation/json'
62
+ },
63
+ 'body' => {
64
+ 'body_one' => 'uno',
65
+ 'body_two' => {
66
+ 'nested' => 'value'
67
+ }
68
+ },
69
+ 'verb' => 'POST'
70
+ }
71
+ input = Mocktopus::Input.new(hash, response_with_empty_hash())
72
+
73
+ hashed_input = input.to_hash
74
+ assert_equal(hash['uri'], hashed_input['uri'])
75
+ assert_equal(hash['headers'], hashed_input['headers'])
76
+ assert_equal(JSON.pretty_generate(hash['body']), hashed_input['body'])
77
+ assert_equal(hash['verb'], hashed_input['verb'])
78
+ end
79
+
80
+ def test_to_hash_returns_properties_with_no_params
81
+ hash = {
82
+ 'uri' => '/test',
83
+ 'headers' => {
84
+ 'Content-Type' => 'appilcation/json'
85
+ },
86
+ 'body' => {
87
+ 'body_one' => 'uno',
88
+ 'body_two' => {
89
+ 'nested' => 'value'
90
+ }
91
+ },
92
+ 'verb' => 'POST'
93
+ }
94
+ input = Mocktopus::Input.new(hash, response_with_empty_hash())
95
+
96
+ hashed_input = input.to_hash
97
+ assert_equal(hash['uri'], hashed_input['uri'])
98
+ assert_equal(hash['headers'], hashed_input['headers'])
99
+ assert_equal(JSON.pretty_generate(hash['body']), hashed_input['body'])
100
+ assert_equal(hash['verb'], hashed_input['verb'])
101
+ end
102
+
103
+ def test_to_hash_returns_properties_with_two_params
104
+ hash = {
105
+ 'uri' => '/test?parameter_one=foo&parameter_two=bar',
106
+ 'headers' => {
107
+ 'Content-Type' => 'appilcation/json'
108
+ },
109
+ 'body' => {
110
+ 'body_one' => 'uno',
111
+ 'body_two' => {
112
+ 'nested' => 'value'
113
+ }
114
+ },
115
+ 'verb' => 'POST'
116
+ }
117
+ input = Mocktopus::Input.new(hash, response_with_empty_hash())
118
+
119
+ hashed_input = input.to_hash
120
+ assert_equal(hash['uri'], hashed_input['uri'])
121
+ assert_equal(hash['headers'], hashed_input['headers'])
122
+ assert_equal(JSON.pretty_generate(hash['body']), hashed_input['body'])
123
+ assert_equal(hash['verb'], hashed_input['verb'])
124
+ end
125
+
126
+ def test_to_s
127
+ hash = {
128
+ 'uri' => '/test/to/s',
129
+ 'verb' => 'GET'
130
+ }
131
+ input = Mocktopus::Input.new(hash, response_with_empty_hash())
132
+ s = input.to_s
133
+ assert_equal 'String', s.class.name
134
+
135
+ end
136
+
137
+ private
138
+
139
+ def build_input(hash = {}, response = response_with_empty_hash())
140
+ input = Mocktopus::Input.new(
141
+ {
142
+ 'uri' => 'http://localhost/uri',
143
+ 'headers' => {
144
+ 'Content-Type' => 'application/json'
145
+ },
146
+ 'body' => '{}',
147
+ 'verb' => 'post'
148
+ }.merge(hash),
149
+ response
150
+ )
151
+ return input
152
+ end
153
+
154
+ def response_with_empty_hash
155
+ response = Mocktopus::Response.new({
156
+ 'code' => 200,
157
+ 'headers' => nil,
158
+ 'body' => nil
159
+ })
160
+
161
+ def response.to_hash
162
+ {}
163
+ end
164
+ return response
165
+ end
166
+ end
@@ -0,0 +1,26 @@
1
+ require 'test_helper'
2
+
3
+ class MockApiCallContainerTest < Mocktopus::Test
4
+
5
+ def test_initialize_not_nil
6
+ container = Mocktopus::MockApiCallContainer.new
7
+ refute_nil container
8
+ assert_equal('Mocktopus::MockApiCallContainer', container.class.name)
9
+ end
10
+
11
+ def test_add
12
+ container = Mocktopus::MockApiCallContainer.new
13
+ call = Mocktopus::MockApiCall.new('/foo/bar', 'POST', {"header1" => "header_one"}, {"body1" => "body_one"})
14
+ container.add(call)
15
+ end
16
+
17
+ def test_all
18
+ container = Mocktopus::MockApiCallContainer.new
19
+ 10.times do |i|
20
+ container.add(Mocktopus::MockApiCall.new("/foo/bar/#{i}", 'POST', {"header#{i}" => "header_#{i}"}, {"body#{i}" => "body_#{i}"}))
21
+ end
22
+ all = container.all
23
+ refute_nil all.select{|a| a['path'] == '/foo/bar/0'}.first
24
+ end
25
+
26
+ end
@@ -0,0 +1,35 @@
1
+ require 'test_helper'
2
+
3
+ class MockApiCallTest < Mocktopus::Test
4
+
5
+ def test_constructor
6
+ path = '/test_constructor/1'
7
+ verb = 'POST'
8
+ headers = {}
9
+ headers['header1'] = 'header1'
10
+ headers['header2'] = 'header2'
11
+ body = {}
12
+ body['one'] = 1
13
+ body['two'] = 2
14
+ call = Mocktopus::MockApiCall.new(path, verb, headers, body)
15
+ refute_nil call
16
+ assert_equal(path, call.path)
17
+ assert_equal(verb, call.verb)
18
+ assert_equal(headers, call.headers)
19
+ assert_equal(body, call.body)
20
+ end
21
+
22
+ def test_timestamp
23
+ call = Mocktopus::MockApiCall.new('/foo/bar', 'DELETE', {}, {})
24
+ refute_nil call.timestamp
25
+ parsed_time = Time.iso8601(call.timestamp)
26
+ assert_equal('Time', parsed_time.class.name)
27
+ end
28
+
29
+ def test_to_s
30
+ call = Mocktopus::MockApiCall.new('/test/to/s', 'GET', {}, {})
31
+ s = call.to_s
32
+ assert_equal 'String', s.class.name
33
+ end
34
+
35
+ end
@@ -0,0 +1,64 @@
1
+ require 'test_helper'
2
+
3
+ class ResponseTest < Mocktopus::Test
4
+
5
+ def test_to_hash
6
+ input = build_response({})
7
+ hash = input.to_hash
8
+
9
+ assert_equal(hash['headers']['Content-Type'], 'application/json')
10
+ assert_equal(hash['body'], '{}')
11
+ assert_equal(hash['code'], 200)
12
+ end
13
+
14
+ def test_to_hash_extra_fields
15
+ input = build_response({'nosuch' => 'extra'})
16
+ hash = input.to_hash
17
+
18
+ assert_equal(hash['nosuch'], nil)
19
+ end
20
+
21
+ def test_validation
22
+
23
+ assert_raises Mocktopus::Response::ValidationError do
24
+ build_response({'headers' => {nil => nil}})
25
+ end
26
+
27
+ assert_raises Mocktopus::Response::ValidationError do
28
+ build_response({'code' => "Not a code"})
29
+ end
30
+ build_response({})
31
+
32
+ end
33
+
34
+ def test_delay_integer
35
+ input = build_response({'delay' => 15})
36
+ hash = input.to_hash
37
+ assert_equal(15, hash['delay'])
38
+ end
39
+
40
+ def test_delay_float
41
+ input = build_response({'delay' => 12.54542})
42
+ hash = input.to_hash
43
+ assert_equal(12.54542, hash['delay'])
44
+ end
45
+
46
+ def test_delay_invalid
47
+ input = build_response({'delay' => 'invalid_delay'})
48
+ hash = input.to_hash
49
+ assert_equal(0, hash['delay'])
50
+ end
51
+
52
+ def build_response(hash)
53
+ response = Mocktopus::Response.new(
54
+ {
55
+ 'code' => 200,
56
+ 'headers' => {
57
+ 'Content-Type' => 'application/json'
58
+ },
59
+ 'body' => '{}'
60
+ }.merge(hash)
61
+ )
62
+ return response
63
+ end
64
+ end
@@ -0,0 +1,33 @@
1
+ require 'simplecov'
2
+ require 'coveralls'
3
+
4
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
5
+ Coveralls::SimpleCov::Formatter,
6
+ SimpleCov::Formatter::HTMLFormatter
7
+ ]
8
+
9
+ SimpleCov.start do
10
+ add_filter '/test/'
11
+ add_filter '/ascii.rb'
12
+ end
13
+
14
+ require 'rack/test'
15
+ require 'tmpdir'
16
+ require 'fakeweb'
17
+ require 'minitest/autorun'
18
+ require 'mocha/setup'
19
+
20
+ require_relative '../lib/mocktopus'
21
+
22
+ FakeWeb.allow_net_connect = %r[^https?://coveralls.io]
23
+
24
+ ENV['RACK_ENV'] = 'test'
25
+ WORKING_DIRECTORY = Dir.pwd.freeze
26
+ ARGV.clear
27
+
28
+ module Mocktopus
29
+ class Test < Minitest::Test
30
+ include Rack::Test::Methods
31
+
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,292 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mocktopus
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Rackspace
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sinatra
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.4'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.4.4
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '1.4'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.4.4
33
+ - !ruby/object:Gem::Dependency
34
+ name: sinatra-contrib
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: 1.4.2
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 1.4.2
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: 1.4.2
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 1.4.2
53
+ - !ruby/object:Gem::Dependency
54
+ name: thin
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: 1.6.1
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 1.6.1
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: 1.6.1
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 1.6.1
73
+ - !ruby/object:Gem::Dependency
74
+ name: thor
75
+ requirement: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - "~>"
78
+ - !ruby/object:Gem::Version
79
+ version: 0.19.1
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 0.19.1
83
+ type: :runtime
84
+ prerelease: false
85
+ version_requirements: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.19.1
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: 0.19.1
93
+ - !ruby/object:Gem::Dependency
94
+ name: rack
95
+ requirement: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - "~>"
98
+ - !ruby/object:Gem::Version
99
+ version: 1.5.2
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: 1.5.2
103
+ type: :runtime
104
+ prerelease: false
105
+ version_requirements: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: 1.5.2
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: 1.5.2
113
+ - !ruby/object:Gem::Dependency
114
+ name: rake
115
+ requirement: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - "~>"
118
+ - !ruby/object:Gem::Version
119
+ version: 10.1.0
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: 10.1.0
123
+ type: :runtime
124
+ prerelease: false
125
+ version_requirements: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - "~>"
128
+ - !ruby/object:Gem::Version
129
+ version: 10.1.0
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: 10.1.0
133
+ - !ruby/object:Gem::Dependency
134
+ name: minitest
135
+ requirement: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - "~>"
138
+ - !ruby/object:Gem::Version
139
+ version: 5.2.0
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: 5.2.0
143
+ type: :runtime
144
+ prerelease: false
145
+ version_requirements: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - "~>"
148
+ - !ruby/object:Gem::Version
149
+ version: 5.2.0
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: 5.2.0
153
+ - !ruby/object:Gem::Dependency
154
+ name: fakeweb
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: 1.3.0
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ version: 1.3.0
163
+ type: :runtime
164
+ prerelease: false
165
+ version_requirements: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - "~>"
168
+ - !ruby/object:Gem::Version
169
+ version: 1.3.0
170
+ - - ">="
171
+ - !ruby/object:Gem::Version
172
+ version: 1.3.0
173
+ - !ruby/object:Gem::Dependency
174
+ name: mocha
175
+ requirement: !ruby/object:Gem::Requirement
176
+ requirements:
177
+ - - "~>"
178
+ - !ruby/object:Gem::Version
179
+ version: 0.14.0
180
+ - - ">="
181
+ - !ruby/object:Gem::Version
182
+ version: 0.14.0
183
+ type: :runtime
184
+ prerelease: false
185
+ version_requirements: !ruby/object:Gem::Requirement
186
+ requirements:
187
+ - - "~>"
188
+ - !ruby/object:Gem::Version
189
+ version: 0.14.0
190
+ - - ">="
191
+ - !ruby/object:Gem::Version
192
+ version: 0.14.0
193
+ - !ruby/object:Gem::Dependency
194
+ name: simplecov
195
+ requirement: !ruby/object:Gem::Requirement
196
+ requirements:
197
+ - - "~>"
198
+ - !ruby/object:Gem::Version
199
+ version: 0.9.2
200
+ - - ">="
201
+ - !ruby/object:Gem::Version
202
+ version: 0.9.2
203
+ type: :runtime
204
+ prerelease: false
205
+ version_requirements: !ruby/object:Gem::Requirement
206
+ requirements:
207
+ - - "~>"
208
+ - !ruby/object:Gem::Version
209
+ version: 0.9.2
210
+ - - ">="
211
+ - !ruby/object:Gem::Version
212
+ version: 0.9.2
213
+ - !ruby/object:Gem::Dependency
214
+ name: coveralls
215
+ requirement: !ruby/object:Gem::Requirement
216
+ requirements:
217
+ - - "~>"
218
+ - !ruby/object:Gem::Version
219
+ version: 0.7.11
220
+ - - ">="
221
+ - !ruby/object:Gem::Version
222
+ version: 0.7.11
223
+ type: :runtime
224
+ prerelease: false
225
+ version_requirements: !ruby/object:Gem::Requirement
226
+ requirements:
227
+ - - "~>"
228
+ - !ruby/object:Gem::Version
229
+ version: 0.7.11
230
+ - - ">="
231
+ - !ruby/object:Gem::Version
232
+ version: 0.7.11
233
+ description: The Mocktopus is a Sinatra/thin-based Web API that lets you mock your
234
+ app dependencies
235
+ email:
236
+ - racksburg_automation@lists.rackspace.com
237
+ executables:
238
+ - mocktopus
239
+ extensions: []
240
+ extra_rdoc_files: []
241
+ files:
242
+ - ".coveralls.yml"
243
+ - ".gitignore"
244
+ - ".travis.yml"
245
+ - Gemfile
246
+ - MIT-LICENSE
247
+ - README.md
248
+ - Rakefile
249
+ - ascii.rb
250
+ - bin/mocktopus
251
+ - config.ru
252
+ - lib/mocktopus.rb
253
+ - lib/mocktopus/app.rb
254
+ - lib/mocktopus/cli.rb
255
+ - lib/mocktopus/input.rb
256
+ - lib/mocktopus/input_container.rb
257
+ - lib/mocktopus/mock_api_call.rb
258
+ - lib/mocktopus/mock_api_call_container.rb
259
+ - lib/mocktopus/response.rb
260
+ - mocktopus.gemspec
261
+ - test/app_test.rb
262
+ - test/cli_test.rb
263
+ - test/input_container_test.rb
264
+ - test/input_test.rb
265
+ - test/mock_api_call_container_test.rb
266
+ - test/mock_api_call_test.rb
267
+ - test/response_test.rb
268
+ - test/test_helper.rb
269
+ homepage: https://github.com/rackspaceautomationco/mocktopus
270
+ licenses: []
271
+ metadata: {}
272
+ post_install_message:
273
+ rdoc_options: []
274
+ require_paths:
275
+ - lib
276
+ required_ruby_version: !ruby/object:Gem::Requirement
277
+ requirements:
278
+ - - ">="
279
+ - !ruby/object:Gem::Version
280
+ version: 1.9.3
281
+ required_rubygems_version: !ruby/object:Gem::Requirement
282
+ requirements:
283
+ - - ">="
284
+ - !ruby/object:Gem::Version
285
+ version: '0'
286
+ requirements: []
287
+ rubyforge_project:
288
+ rubygems_version: 2.2.2
289
+ signing_key:
290
+ specification_version: 4
291
+ summary: A configurable mock Web API
292
+ test_files: []