her-webmock 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +97 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/her-webmock.gemspec +35 -0
- data/lib/her/webmock.rb +22 -0
- data/lib/her/webmock/model.rb +138 -0
- data/lib/her/webmock/version.rb +5 -0
- metadata +128 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 99be1cb35146e5876a026f891fdd5dce984877ed
|
4
|
+
data.tar.gz: 3af30122077a500a2f6cd157215ff728c0068cf1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 511961e112fa0a37d007100174c69a997560616fa37de3ebcf99f09718759afaa275b7585337809fcb46833026c5ef7af5e0243c9093eb08ba83d41d1bd29a41
|
7
|
+
data.tar.gz: 7f6ec4890ce33dced99aa97011c973da6387fa1f73290aa19e2f2472d2ff7250278ae43490a81a32e6e8170b4a76e9cb88899fff62a77637cfde72eb69d2e743
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Octavian Neamtu
|
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,97 @@
|
|
1
|
+
# Her::WebMock
|
2
|
+
|
3
|
+
Tired of hand-inputting paths and JSON hashes when Her is so sensible to use? Her::WebMock is a useful gem for easy Her::Model request stubs.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'her-webmock'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
In your spec_helper or initializer (or wherever you want to mock things) do:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
require 'her/webmock/model'
|
23
|
+
|
24
|
+
::User.extend ::Her::WebMock::Model
|
25
|
+
```
|
26
|
+
|
27
|
+
where User is a Her::Model. Now you can easily stub the requests that you'd normally expect to
|
28
|
+
be returned on User.find and User.all
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
User.stub_find(User.new(id: 5, name: 'user'))
|
32
|
+
# equivalent to
|
33
|
+
stub_request("http://.../users/5").and_return("so much JSON string that includes id 5 and name 'user'")
|
34
|
+
```
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
# stub for .all
|
38
|
+
User.stub_all([User.new(id: 5, name: 'user'), User.new(id: 6, name: "anon")])
|
39
|
+
# passing extra params - these get passed to WebMock
|
40
|
+
User.stub_all([User.new(id: 5, name: 'user')], query: hash_including({}), headers: { per_page: 12 }, metadata: { page: 1 })
|
41
|
+
# adding additional data to the response
|
42
|
+
User.stub_all([User.new(id: 5, name: 'user')], metadata: { page: 1, metadata: { additional: 'stuff' } })
|
43
|
+
```
|
44
|
+
|
45
|
+
You can also easily define your own stub method matchers:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
module Mocks
|
49
|
+
module User
|
50
|
+
include ::Her::WebMock::Model
|
51
|
+
|
52
|
+
def stub_for_resource_type_and_resource_id(issues, resource_type, resource_id)
|
53
|
+
query_params = {
|
54
|
+
'search' => {
|
55
|
+
'resource_id_eq' => resource_id,
|
56
|
+
'resource_type_eq' => resource_type
|
57
|
+
}
|
58
|
+
}
|
59
|
+
|
60
|
+
stub_all(issues, query_params: query_params)
|
61
|
+
end
|
62
|
+
|
63
|
+
...
|
64
|
+
|
65
|
+
User.extend Mocks::User
|
66
|
+
```
|
67
|
+
|
68
|
+
## Configuration
|
69
|
+
|
70
|
+
If you expect default headers in all your requests, you can configure them in:
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
Her::WebMock.configure do |config|
|
74
|
+
config.default_request_test_headers = { 'Authorization' => /Bearer .+/ }
|
75
|
+
end
|
76
|
+
```
|
77
|
+
|
78
|
+
## TODO
|
79
|
+
* Test stub associations
|
80
|
+
* Support more association stubs
|
81
|
+
* test default_request_test_headers functionality
|
82
|
+
* make the stub methods included in a model just stubs that call out to the stub methods
|
83
|
+
* Break out the stub components
|
84
|
+
|
85
|
+
## Development
|
86
|
+
|
87
|
+
Run `bin/console` for an interactive prompt that will allow you to experiment.
|
88
|
+
|
89
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
90
|
+
|
91
|
+
## Contributing
|
92
|
+
|
93
|
+
1. Fork it ( https://github.com/annkissam/her-webmock/fork )
|
94
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
95
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
96
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
97
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "her/webmock"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/her-webmock.gemspec
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'her/webmock/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "her-webmock"
|
8
|
+
spec.version = Her::WebMock::VERSION
|
9
|
+
spec.authors = ["Octavian Neamtu"]
|
10
|
+
spec.email = ["octavian.neamtu@annkissam.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Easier request stubbing for Her::Model with webmock.}
|
13
|
+
spec.homepage = "https://github.com/annkissam/her-webmock.git"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
17
|
+
# delete this section to allow pushing this gem to any host.
|
18
|
+
# if spec.respond_to?(:metadata)
|
19
|
+
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
|
20
|
+
# else
|
21
|
+
# raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
22
|
+
# end
|
23
|
+
|
24
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
25
|
+
spec.bindir = "exe"
|
26
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
27
|
+
spec.require_paths = ["lib"]
|
28
|
+
|
29
|
+
spec.add_runtime_dependency "her"
|
30
|
+
spec.add_runtime_dependency "webmock"
|
31
|
+
|
32
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
33
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
34
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
35
|
+
end
|
data/lib/her/webmock.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require "her/webmock/version"
|
2
|
+
|
3
|
+
module Her
|
4
|
+
module WebMock
|
5
|
+
class Configuration
|
6
|
+
attr_accessor :default_request_test_headers
|
7
|
+
|
8
|
+
def initialize(options = {})
|
9
|
+
self.default_request_test_headers = {}
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.configure
|
14
|
+
yield config
|
15
|
+
config.freeze
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.config
|
19
|
+
@config ||= Configuration.new
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,138 @@
|
|
1
|
+
require 'her/webmock'
|
2
|
+
require 'webmock'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module Her
|
6
|
+
module WebMock
|
7
|
+
module Helper
|
8
|
+
def self.attributes_without_embedded_associations(klass, object)
|
9
|
+
attributes = object.respond_to?(:attributes) ? object.attributes.dup : object.dup
|
10
|
+
|
11
|
+
klass.associations.each do |type, association_metadata_ary|
|
12
|
+
association_metadata_ary.each do |association_metadata|
|
13
|
+
association = attributes.delete(association_metadata[:name])
|
14
|
+
|
15
|
+
attributes[association_metadata[:foreign_key]] = association.id if association
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
attributes
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.request_params(options = {})
|
23
|
+
request_params = {}
|
24
|
+
|
25
|
+
request_params[:query] = options[:query]
|
26
|
+
|
27
|
+
headers_hash = default_headers.merge(options.fetch(:headers, {}))
|
28
|
+
request_params[:headers] = headers_hash unless headers_hash.empty?
|
29
|
+
|
30
|
+
request_params
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.default_headers
|
34
|
+
WebMock.config.default_request_test_headers
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
module Model
|
39
|
+
include ::WebMock::API
|
40
|
+
|
41
|
+
def stub_associations(klass, object)
|
42
|
+
attributes = object.respond_to?(:attributes) ? object.attributes : object
|
43
|
+
|
44
|
+
klass.associations.each do |type, association_metadata_ary|
|
45
|
+
association_metadata_ary.each do |association_metadata|
|
46
|
+
association = attributes[association_metadata[:name]]
|
47
|
+
|
48
|
+
if association
|
49
|
+
case type
|
50
|
+
when :belongs_to
|
51
|
+
Object.const_get(association_metadata[:class_name]).stub_find(association)
|
52
|
+
else
|
53
|
+
# TODO
|
54
|
+
fail NotImplementedError
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def stub_create(object, options = {})
|
62
|
+
model_class = self
|
63
|
+
attributes = object.is_a?(Her::Model) ? Helper.attributes_without_embedded_associations(model_class, object) : object
|
64
|
+
|
65
|
+
fail "Must pass in an object with an id attribute" unless attributes[:id]
|
66
|
+
|
67
|
+
if model_class.parsed_root_element
|
68
|
+
response = {
|
69
|
+
model_class.parsed_root_element => attributes
|
70
|
+
}
|
71
|
+
else
|
72
|
+
response = attributes
|
73
|
+
end
|
74
|
+
|
75
|
+
attributes_without_id = attributes.except(:id)
|
76
|
+
|
77
|
+
request_stub = stub_request(:post, model_class.use_api.base_uri + model_class.build_request_path(attributes_without_id)).
|
78
|
+
to_return(body: JSON.generate(response), status: 200)
|
79
|
+
|
80
|
+
request_params = Helper.request_params(options)
|
81
|
+
request_stub.with(request_params) unless request_params.empty?
|
82
|
+
|
83
|
+
if options[:stub_related]
|
84
|
+
stub_find(object)
|
85
|
+
stub_all([object])
|
86
|
+
end
|
87
|
+
|
88
|
+
stub_associations(model_class, object)
|
89
|
+
|
90
|
+
request_stub
|
91
|
+
end
|
92
|
+
|
93
|
+
def stub_find(object, options = {})
|
94
|
+
model_class = self
|
95
|
+
attributes = object.is_a?(Her::Model) ? Helper.attributes_without_embedded_associations(model_class, object) : object
|
96
|
+
|
97
|
+
if model_class.parsed_root_element
|
98
|
+
response = {
|
99
|
+
model_class.parsed_root_element => attributes
|
100
|
+
}
|
101
|
+
else
|
102
|
+
response = attributes
|
103
|
+
end
|
104
|
+
|
105
|
+
request_stub = stub_request(:get, model_class.use_api.base_uri + model_class.build_request_path(attributes)).
|
106
|
+
to_return(body: JSON.generate(response), status: 200)
|
107
|
+
|
108
|
+
request_params = Helper.request_params(options)
|
109
|
+
request_stub.with(request_params) unless request_params.empty?
|
110
|
+
|
111
|
+
stub_associations(model_class, object)
|
112
|
+
|
113
|
+
request_stub
|
114
|
+
end
|
115
|
+
|
116
|
+
def stub_all(collection, options = {})
|
117
|
+
model_class = self
|
118
|
+
|
119
|
+
collection_attributes = collection.map { |object| Helper.attributes_without_embedded_associations(model_class, object) }
|
120
|
+
response = {
|
121
|
+
model_class.pluralized_parsed_root_element => collection_attributes
|
122
|
+
}
|
123
|
+
|
124
|
+
response = options[:metadata].merge(response) if options[:metadata]
|
125
|
+
|
126
|
+
request_stub = stub_request(:get, model_class.use_api.base_uri + model_class.collection_path).
|
127
|
+
to_return(body: JSON.generate(response), status: 200)
|
128
|
+
|
129
|
+
request_params = Helper.request_params(options)
|
130
|
+
request_stub.with(request_params) unless request_params.empty?
|
131
|
+
|
132
|
+
collection.each { |object| stub_associations(model_class, object) }
|
133
|
+
|
134
|
+
request_stub
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
metadata
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: her-webmock
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Octavian Neamtu
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-07-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: her
|
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: webmock
|
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: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.9'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.9'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
83
|
+
description:
|
84
|
+
email:
|
85
|
+
- octavian.neamtu@annkissam.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
93
|
+
- CODE_OF_CONDUCT.md
|
94
|
+
- Gemfile
|
95
|
+
- LICENSE.txt
|
96
|
+
- README.md
|
97
|
+
- Rakefile
|
98
|
+
- bin/console
|
99
|
+
- bin/setup
|
100
|
+
- her-webmock.gemspec
|
101
|
+
- lib/her/webmock.rb
|
102
|
+
- lib/her/webmock/model.rb
|
103
|
+
- lib/her/webmock/version.rb
|
104
|
+
homepage: https://github.com/annkissam/her-webmock.git
|
105
|
+
licenses:
|
106
|
+
- MIT
|
107
|
+
metadata: {}
|
108
|
+
post_install_message:
|
109
|
+
rdoc_options: []
|
110
|
+
require_paths:
|
111
|
+
- lib
|
112
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - ">="
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
requirements: []
|
123
|
+
rubyforge_project:
|
124
|
+
rubygems_version: 2.4.5
|
125
|
+
signing_key:
|
126
|
+
specification_version: 4
|
127
|
+
summary: Easier request stubbing for Her::Model with webmock.
|
128
|
+
test_files: []
|