activeresource-httpmock-flow 0.1.0
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.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.travis.yml +5 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +21 -0
- data/README.md +112 -0
- data/Rakefile +9 -0
- data/activeresource-mock-flow.gemspec +27 -0
- data/bin/console +7 -0
- data/bin/setup +7 -0
- data/lib/activeresource/http_mock/flow.rb +12 -0
- data/lib/activeresource/http_mock/flow/http_mock.rb +34 -0
- data/lib/activeresource/http_mock/flow/responder.rb +44 -0
- data/lib/activeresource/http_mock/flow/version.rb +9 -0
- metadata +141 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e771b1e321b120abd1b0988973a4180c3810034b
|
4
|
+
data.tar.gz: 0b5b5ce7896415a80775199ec488d0352d3e9c5a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b7bde8f69bcea397801b3091fa81415db5a515ae21574ca12d68eb235fb6e829fd420447fd882c80b706457fc088708bd8ecea787f03fed9b13853c73d610dc3
|
7
|
+
data.tar.gz: 8813e603668972132a22bd0e691b3100a6bd5ee2b618e93ac647e7c64f87affbc15f90d38eac0de86c8c122e33d504648c3665ea6a6bc27fffda1a38ee28a64d
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Hi5 Development, LLC
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
# ActiveResource::HttpMock::Flow
|
2
|
+
|
3
|
+
Adds dynamic mocks to ActiveResource::HttpMock.
|
4
|
+
|
5
|
+
The default implementation of ActiveResource::HttpMock is not able to test code
|
6
|
+
that has this kind of work flow:
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
class Charge < ActiveResource::Base; end
|
10
|
+
|
11
|
+
def charge_customer
|
12
|
+
count = Charge.all.count
|
13
|
+
|
14
|
+
Charge.create(amount: 10.00)
|
15
|
+
|
16
|
+
fail 'could not charge customer' unless Charge.all.count == count + 1
|
17
|
+
end
|
18
|
+
```
|
19
|
+
|
20
|
+
ActiveResource::HttpMock::Flow can test the above like so:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
def setup
|
24
|
+
@charges = []
|
25
|
+
|
26
|
+
ActiveResource::HttpMock.respond_to do |mock|
|
27
|
+
mock.get '/charges.json' do |request, response|
|
28
|
+
response.body = {charges: @charges}.to_json
|
29
|
+
end
|
30
|
+
|
31
|
+
mock.post '/charges.json' do |request, response|
|
32
|
+
@charges << [{id: @charges.length + 1}]
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_customers_are_charged
|
38
|
+
assert_equal 0, Charge.all.count
|
39
|
+
|
40
|
+
charge_customer
|
41
|
+
|
42
|
+
assert_equal 1, Charge.all.count
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
It also allows you to check if a request was actually made, and to inspect it:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
def setup
|
50
|
+
ActiveResource::HttpMock.respond_to do |mock|
|
51
|
+
mock.post '/charges.json' do |request, _|
|
52
|
+
@request = request
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_customers_are_charged
|
58
|
+
charge_customer
|
59
|
+
|
60
|
+
refute_nil @request
|
61
|
+
|
62
|
+
json = ActiveSupport::JSON.decode(@request.body)
|
63
|
+
charge = json['charge']
|
64
|
+
|
65
|
+
assert_equal 10.00, charge['amount']
|
66
|
+
end
|
67
|
+
```
|
68
|
+
|
69
|
+
## Installation
|
70
|
+
|
71
|
+
Update your Gemfile to the following:
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
group :test do
|
75
|
+
gem 'activeresource-httpmock-flow'
|
76
|
+
end
|
77
|
+
```
|
78
|
+
|
79
|
+
And then execute:
|
80
|
+
|
81
|
+
$ bundle
|
82
|
+
|
83
|
+
## Usage
|
84
|
+
|
85
|
+
Require the library in your test helper:
|
86
|
+
|
87
|
+
```ruby
|
88
|
+
require 'activeresource/http_mock/flow'
|
89
|
+
```
|
90
|
+
|
91
|
+
And use ActiveResource::HttpMock as you would normally. It is compatible with
|
92
|
+
the default implementation as well.
|
93
|
+
|
94
|
+
## Development
|
95
|
+
|
96
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then,
|
97
|
+
run `rake test` to run the tests. You can also run `bin/console` for an
|
98
|
+
interactive prompt that will allow you to experiment.
|
99
|
+
|
100
|
+
To install the development version onto your local machine, run
|
101
|
+
`bundle exec rake install`.
|
102
|
+
|
103
|
+
## Contributing
|
104
|
+
|
105
|
+
Bug reports and pull requests are welcome on GitHub at
|
106
|
+
https://github.com/hi5dev/activeresource-httpmock-flow.
|
107
|
+
|
108
|
+
|
109
|
+
## License
|
110
|
+
|
111
|
+
The gem is available as open source under the terms of the
|
112
|
+
[MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'activeresource-httpmock-flow'
|
7
|
+
spec.version = '0.1.0'
|
8
|
+
spec.authors = ['Travis Haynes']
|
9
|
+
spec.email = ['travis@hi5dev.com']
|
10
|
+
|
11
|
+
spec.summary = 'Adds multi-stage requests to ActiveResource::HttpMock'
|
12
|
+
spec.homepage = 'https://github.com/hi5dev/activeresource-httpmock-flow'
|
13
|
+
spec.license = 'MIT'
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
16
|
+
spec.bindir = "exe"
|
17
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
18
|
+
spec.require_paths = %w[lib]
|
19
|
+
|
20
|
+
spec.add_dependency 'activeresource'
|
21
|
+
|
22
|
+
spec.add_development_dependency 'activesupport'
|
23
|
+
spec.add_development_dependency 'bundler'
|
24
|
+
spec.add_development_dependency 'byebug'
|
25
|
+
spec.add_development_dependency 'minitest'
|
26
|
+
spec.add_development_dependency 'rake'
|
27
|
+
end
|
data/bin/console
ADDED
data/bin/setup
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'activeresource'
|
2
|
+
|
3
|
+
module ActiveResource
|
4
|
+
class HttpMock
|
5
|
+
module Flow
|
6
|
+
autoload :VERSION, 'activeresource/http_mock/flow/version'
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
require 'activeresource/http_mock/flow/http_mock'
|
12
|
+
require 'activeresource/http_mock/flow/responder'
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module ActiveResource
|
2
|
+
class HttpMock
|
3
|
+
[:patch, :post, :put].each do |verb|
|
4
|
+
define_method(verb) do |path, body, headers|
|
5
|
+
mock(verb, path, body, headers)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
[:delete, :get, :head].each do |verb|
|
10
|
+
define_method(verb) do |path, headers|
|
11
|
+
mock(verb, path, nil, headers)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def mock(verb, path, body, headers)
|
18
|
+
request = Request.new(verb, path, body, headers)
|
19
|
+
|
20
|
+
self.class.requests << request
|
21
|
+
|
22
|
+
if mocks = self.class.responses.assoc(request)
|
23
|
+
mocks[2].call(request, mocks[1]) unless mocks[2].nil?
|
24
|
+
|
25
|
+
return mocks[1]
|
26
|
+
end
|
27
|
+
|
28
|
+
fail InvalidRequestError.new([
|
29
|
+
"Could not find a response recorded for #{request.to_s}",
|
30
|
+
"Responses recorded are: #{inspect_responses}"
|
31
|
+
].join(' - '))
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module ActiveResource
|
2
|
+
class HttpMock
|
3
|
+
class Responder
|
4
|
+
def initialize(responses)
|
5
|
+
@responses = responses
|
6
|
+
end
|
7
|
+
|
8
|
+
[:delete, :get, :head, :patch, :post].each do |http_verb|
|
9
|
+
define_method(http_verb) do |path, *args, &block|
|
10
|
+
mock(http_verb, path, options_from_args(*args), &block)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def options_from_args(*args)
|
17
|
+
{
|
18
|
+
request_headers: args[0],
|
19
|
+
body: args[1],
|
20
|
+
status: args[2],
|
21
|
+
response_headers: args[3]
|
22
|
+
}
|
23
|
+
end
|
24
|
+
|
25
|
+
def mock(http_verb, path, options={}, &block)
|
26
|
+
body = options[:body] || ''
|
27
|
+
request_headers = options[:request_headers] || {}
|
28
|
+
response_headers = options[:response_headers] || {}
|
29
|
+
status = options[:status] || 200
|
30
|
+
|
31
|
+
request = Request.new(http_verb, path, body, request_headers)
|
32
|
+
response = Response.new(body, status, response_headers)
|
33
|
+
|
34
|
+
delete_duplicate_responses(request, block)
|
35
|
+
|
36
|
+
@responses << [request, response, block]
|
37
|
+
end
|
38
|
+
|
39
|
+
def delete_duplicate_responses(request, block)
|
40
|
+
@responses.delete_if {|r| r[0] == request }
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
metadata
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activeresource-httpmock-flow
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Travis Haynes
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-09-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activeresource
|
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: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
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: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
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: byebug
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
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: minitest
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rake
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description:
|
98
|
+
email:
|
99
|
+
- travis@hi5dev.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- ".travis.yml"
|
106
|
+
- Gemfile
|
107
|
+
- LICENSE.txt
|
108
|
+
- README.md
|
109
|
+
- Rakefile
|
110
|
+
- activeresource-mock-flow.gemspec
|
111
|
+
- bin/console
|
112
|
+
- bin/setup
|
113
|
+
- lib/activeresource/http_mock/flow.rb
|
114
|
+
- lib/activeresource/http_mock/flow/http_mock.rb
|
115
|
+
- lib/activeresource/http_mock/flow/responder.rb
|
116
|
+
- lib/activeresource/http_mock/flow/version.rb
|
117
|
+
homepage: https://github.com/hi5dev/activeresource-httpmock-flow
|
118
|
+
licenses:
|
119
|
+
- MIT
|
120
|
+
metadata: {}
|
121
|
+
post_install_message:
|
122
|
+
rdoc_options: []
|
123
|
+
require_paths:
|
124
|
+
- lib
|
125
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
127
|
+
- - ">="
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: '0'
|
130
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
131
|
+
requirements:
|
132
|
+
- - ">="
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: '0'
|
135
|
+
requirements: []
|
136
|
+
rubyforge_project:
|
137
|
+
rubygems_version: 2.5.1
|
138
|
+
signing_key:
|
139
|
+
specification_version: 4
|
140
|
+
summary: Adds multi-stage requests to ActiveResource::HttpMock
|
141
|
+
test_files: []
|