service_contract_webmock 0.1.0
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 +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +64 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/service_contract_webmock.rb +10 -0
- data/lib/service_contract_webmock/contract_matcher.rb +57 -0
- data/lib/service_contract_webmock/field.rb +58 -0
- data/lib/service_contract_webmock/service_mock.rb +62 -0
- data/lib/service_contract_webmock/version.rb +3 -0
- data/service_contract_webmock.gemspec +28 -0
- metadata +143 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 59e4650bf65a0018ea8b8efcab72e01cb81a60c9
|
4
|
+
data.tar.gz: 6cf0fa47ae94be22429ea8b48430c510315caf9a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5f38f725beafa855ca724e5103160a6730044b89f3ddf179f66c3f281801623c7a252c1e3d824800cdc7f0863f435e3962ec09963cd3f28e2a99d54aaa5de605
|
7
|
+
data.tar.gz: 272ded90a1937744f125d8554c86a99aa9e5122bc4ad0508385d3ff970abf91c11a5cf7aeddfb7f4863eadf598b7ce2b9607e07c45b0d3759cbeab4018d02655
|
data/.gitignore
ADDED
data/.rspec
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) 2015 Donald Plummer
|
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,64 @@
|
|
1
|
+
# ServiceContractWebmock
|
2
|
+
|
3
|
+
A library for taking Avro contracts from ServiceContract and generating Webmock
|
4
|
+
HTTP mocks to return fixture data.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'service_contract_webmock'
|
12
|
+
```
|
13
|
+
|
14
|
+
## Usage
|
15
|
+
|
16
|
+
Have some mocks for a service, like:
|
17
|
+
|
18
|
+
```yaml
|
19
|
+
# spec/mocks/foobar/orders.yml
|
20
|
+
---
|
21
|
+
- id: 1
|
22
|
+
customer_id: 1
|
23
|
+
status: Fulfilled
|
24
|
+
```
|
25
|
+
|
26
|
+
And assuming the `Foobar::Contract` has a schema defined for `Order`, then the
|
27
|
+
following will create webmocks to return the order fixture.
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
ledger_contract = Foobar::Contract::Service.find("v1")
|
31
|
+
base_url = 'http://localhost:6002/api/v1'
|
32
|
+
|
33
|
+
Dir['spec/mocks/foobar/*.yml'].each do |filename|
|
34
|
+
name = File.basename(filename, '.yml')
|
35
|
+
resources = YAML.load_file(File.join(Rails.root, filename))
|
36
|
+
controller = ledger_contract.protocol(name.singularize)
|
37
|
+
next if controller.nil?
|
38
|
+
|
39
|
+
ServiceContractWebmock.webmock!(name, resources, controller, base_url)
|
40
|
+
end
|
41
|
+
```
|
42
|
+
|
43
|
+
## Development
|
44
|
+
|
45
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
46
|
+
`rake rspec` to run the tests. You can also run `bin/console` for an
|
47
|
+
interactive prompt that will allow you to experiment.
|
48
|
+
|
49
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To
|
50
|
+
release a new version, update the version number in `version.rb`, and then run
|
51
|
+
`bundle exec rake release`, which will create a git tag for the version, push
|
52
|
+
git commits and tags, and push the `.gem` file to
|
53
|
+
[rubygems.org](https://rubygems.org).
|
54
|
+
|
55
|
+
## Contributing
|
56
|
+
|
57
|
+
Bug reports and pull requests are welcome on GitHub at
|
58
|
+
https://github.com/[USERNAME]/service_contract_webmock.
|
59
|
+
|
60
|
+
|
61
|
+
## License
|
62
|
+
|
63
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
64
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "service_contract_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
@@ -0,0 +1,10 @@
|
|
1
|
+
require "service_contract_webmock/version"
|
2
|
+
require "service_contract_webmock/field"
|
3
|
+
require "service_contract_webmock/contract_matcher"
|
4
|
+
require "service_contract_webmock/service_mock"
|
5
|
+
|
6
|
+
module ServiceContractWebmock
|
7
|
+
def self.webmock!(name, resources, contract, base_url)
|
8
|
+
ServiceMock.new(name, resources, contract, base_url).webmock!
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'cgi'
|
2
|
+
require 'avro'
|
3
|
+
|
4
|
+
module ServiceContractWebmock
|
5
|
+
class ContractMatcher
|
6
|
+
attr_reader :endpoint, :resources
|
7
|
+
|
8
|
+
def initialize(endpoint, resources)
|
9
|
+
@endpoint = endpoint
|
10
|
+
@resources = resources
|
11
|
+
end
|
12
|
+
|
13
|
+
INT = Avro::Schema::PrimitiveSchema.new(:int)
|
14
|
+
|
15
|
+
def to_regex
|
16
|
+
params = (fields + [Field.new('page', INT), Field.new('per_page', INT)]).map do |field|
|
17
|
+
"#{field.name}=#{field.value}&?"
|
18
|
+
end.join("|")
|
19
|
+
"(#{params})+"
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
def fields
|
24
|
+
@fields ||= endpoint.parameters.map do |param|
|
25
|
+
param.type.definition.fields.map do |field|
|
26
|
+
Field.new(field.name, field.type)
|
27
|
+
end
|
28
|
+
end.flatten
|
29
|
+
end
|
30
|
+
|
31
|
+
def field_int?(name)
|
32
|
+
fields.detect {|f| f.name == name}.try(:int?)
|
33
|
+
end
|
34
|
+
|
35
|
+
def extract_request(query)
|
36
|
+
params = CGI.parse(query)
|
37
|
+
fields.select {|field| field.name.in?(params.keys)}.each_with_object({}) do |field, acc|
|
38
|
+
data = params[field.name][0].split(',').flat_map {|value| field.convert(value)}
|
39
|
+
acc[field.name] = data
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def found(query)
|
44
|
+
search = extract_request(query)
|
45
|
+
if search.empty?
|
46
|
+
resources
|
47
|
+
else
|
48
|
+
resources.select do |resource|
|
49
|
+
search.all? do |key, data|
|
50
|
+
key = "id" if key == "ids"
|
51
|
+
resource[key].in?(data)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module ServiceContractWebmock
|
2
|
+
class Field
|
3
|
+
attr_reader :name, :type, :value
|
4
|
+
|
5
|
+
def initialize(name, type)
|
6
|
+
@name = name
|
7
|
+
@type = type
|
8
|
+
@value = build_value(type)
|
9
|
+
end
|
10
|
+
|
11
|
+
def int?(t = type)
|
12
|
+
t.type_sym == :int ||
|
13
|
+
(t.type_sym == :array && t.items.type_sym == :int) ||
|
14
|
+
(t.type_sym == :union && t.schemas.any? {|sub| int?(sub)})
|
15
|
+
end
|
16
|
+
|
17
|
+
def boolean?(t = type)
|
18
|
+
t.type_sym == :boolean ||
|
19
|
+
(t.type_sym == :array && t.items.type_sym == :boolean) ||
|
20
|
+
(t.type_sym == :union && t.schemas.any? {|sub| boolean?(sub)})
|
21
|
+
end
|
22
|
+
|
23
|
+
def convert(value)
|
24
|
+
if int?
|
25
|
+
value.to_i
|
26
|
+
elsif boolean?
|
27
|
+
value == "true"
|
28
|
+
else
|
29
|
+
value
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def build_value(type)
|
34
|
+
case type.type_sym
|
35
|
+
when :union
|
36
|
+
subtypes = type.schemas.map {|subtype| build_value(subtype)}
|
37
|
+
"(#{subtypes.join("|")})"
|
38
|
+
when :null
|
39
|
+
""
|
40
|
+
when :int
|
41
|
+
"\\d+"
|
42
|
+
when :array
|
43
|
+
case type.items.type_sym
|
44
|
+
when :int
|
45
|
+
"[,\\d]+"
|
46
|
+
else
|
47
|
+
raise "unhandled array subtype #{type.items.type_sym}"
|
48
|
+
end
|
49
|
+
when :string
|
50
|
+
".+"
|
51
|
+
when :boolean
|
52
|
+
"true|false"
|
53
|
+
else
|
54
|
+
raise "unhandled type #{type.type_sym}"
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'webmock'
|
2
|
+
|
3
|
+
module ServiceContractWebmock
|
4
|
+
class ServiceMock
|
5
|
+
attr_reader :name, :resources, :contract, :base_url
|
6
|
+
|
7
|
+
def initialize(name, resources, contract, base_url)
|
8
|
+
@name = name
|
9
|
+
@resources = resources
|
10
|
+
@contract = contract
|
11
|
+
@base_url = base_url
|
12
|
+
end
|
13
|
+
|
14
|
+
def webmock!
|
15
|
+
stub_index_request
|
16
|
+
stub_search_request
|
17
|
+
stub_show_request
|
18
|
+
end
|
19
|
+
|
20
|
+
def stub_request(method, path)
|
21
|
+
WebMock.stub_request(method, %r{#{base_url}/#{path}})
|
22
|
+
end
|
23
|
+
|
24
|
+
def stub_index_request
|
25
|
+
stub_request(:get, "#{name}\\.json\\??$")
|
26
|
+
.to_return(body: { name => resources }.to_json,
|
27
|
+
headers: { 'Content-Type' => 'application/json' })
|
28
|
+
end
|
29
|
+
|
30
|
+
def stub_show_request
|
31
|
+
stub_request(:get, "#{name}/\\d+\\.json").
|
32
|
+
to_return do |request|
|
33
|
+
id = request.uri.path.scan(/\/(\d+)\.json/)[0][0].to_i
|
34
|
+
found = resources.select {|r| r['id'] == id}
|
35
|
+
|
36
|
+
if found.present?
|
37
|
+
{
|
38
|
+
body: { name => found }.to_json,
|
39
|
+
headers: { 'Content-Type' => 'application/json' }
|
40
|
+
}
|
41
|
+
else
|
42
|
+
{
|
43
|
+
body: { meta: { status: 404, errors: ['resource not found'] } }.to_json,
|
44
|
+
headers: { 'Content-Type' => 'application/json' },
|
45
|
+
status: 404
|
46
|
+
}
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def stub_search_request
|
52
|
+
matcher = ContractMatcher.new(contract.endpoint("index"), resources)
|
53
|
+
stub_request(:get, "#{name}\\.json\\?#{matcher.to_regex}").
|
54
|
+
to_return do |request|
|
55
|
+
{
|
56
|
+
body: { name => matcher.found(request.uri.query) }.to_json,
|
57
|
+
headers: { 'Content-Type' => 'application/json' }
|
58
|
+
}
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'service_contract_webmock/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "service_contract_webmock"
|
8
|
+
spec.version = ServiceContractWebmock::VERSION
|
9
|
+
spec.authors = ["Donald Plummer"]
|
10
|
+
spec.email = ["donald.plummer@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A library for generating webmock mocks with ServiceContract schemas}
|
13
|
+
spec.homepage = "https://github.com/dplummer/service_contract_webmock"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "avro"
|
22
|
+
spec.add_dependency "service_contract"
|
23
|
+
spec.add_dependency "webmock"
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
spec.add_development_dependency "rspec"
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,143 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: service_contract_webmock
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Donald Plummer
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-08-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: avro
|
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: service_contract
|
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: webmock
|
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: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.10'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.10'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '10.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '10.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
|
+
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
|
+
- donald.plummer@gmail.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- ".rspec"
|
106
|
+
- ".travis.yml"
|
107
|
+
- Gemfile
|
108
|
+
- LICENSE.txt
|
109
|
+
- README.md
|
110
|
+
- Rakefile
|
111
|
+
- bin/console
|
112
|
+
- bin/setup
|
113
|
+
- lib/service_contract_webmock.rb
|
114
|
+
- lib/service_contract_webmock/contract_matcher.rb
|
115
|
+
- lib/service_contract_webmock/field.rb
|
116
|
+
- lib/service_contract_webmock/service_mock.rb
|
117
|
+
- lib/service_contract_webmock/version.rb
|
118
|
+
- service_contract_webmock.gemspec
|
119
|
+
homepage: https://github.com/dplummer/service_contract_webmock
|
120
|
+
licenses:
|
121
|
+
- MIT
|
122
|
+
metadata: {}
|
123
|
+
post_install_message:
|
124
|
+
rdoc_options: []
|
125
|
+
require_paths:
|
126
|
+
- lib
|
127
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
133
|
+
requirements:
|
134
|
+
- - ">="
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
version: '0'
|
137
|
+
requirements: []
|
138
|
+
rubyforge_project:
|
139
|
+
rubygems_version: 2.4.6
|
140
|
+
signing_key:
|
141
|
+
specification_version: 4
|
142
|
+
summary: A library for generating webmock mocks with ServiceContract schemas
|
143
|
+
test_files: []
|