webmock-fixtures 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 +3 -0
- data/.rubocop.yml +58 -0
- data/.travis.yml +13 -0
- data/Gemfile +4 -0
- data/LICENSE-MIT +22 -0
- data/README.md +82 -0
- data/Rakefile +22 -0
- data/examples/getting_started_spec.rb +27 -0
- data/lib/webmock/fixtures.rb +2 -0
- data/lib/webmock/fixtures/manager.rb +110 -0
- data/lib/webmock/fixtures/version.rb +6 -0
- data/spec/get_httpbin_200.raw +21 -0
- data/spec/manager_spec.rb +180 -0
- data/webmock-fixtures.gemspec +26 -0
- metadata +141 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 265a0bb8c9102300fe5881cb6443435826486c6c
|
4
|
+
data.tar.gz: bf9f77302fb741fcc5d4814bea7c0a2e4e219371
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f4af9073934dda9747992b56ef579a8a6a403dc858c1026f563027d32cb0917e68a7d060333e911cae44c7582beae53300f6dfd400d05e11bfd5409ef1d9d10c
|
7
|
+
data.tar.gz: 917d257a74b3f11b85b4c9230bc6db82c0bbbfcdc818cbc03e0a1cd5c328fb0a01465f02c79729246948f57c2762bc9c86b5d4a268274f4ff28cca83a396b1d3
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
Metrics/LineLength:
|
2
|
+
# 80 just isn't enough
|
3
|
+
Max: 120
|
4
|
+
|
5
|
+
Metrics/MethodLength:
|
6
|
+
# This gets noisy for scripts and we don't need it yet
|
7
|
+
Enabled: false
|
8
|
+
|
9
|
+
Style/HashSyntax:
|
10
|
+
# Enforce `{ :key => :value }` over `{ key: :value }`
|
11
|
+
EnforcedStyle: hash_rockets
|
12
|
+
|
13
|
+
Style/IfUnlessModifier:
|
14
|
+
# Allow single-line body if statements
|
15
|
+
# if condition
|
16
|
+
# do_something
|
17
|
+
# end
|
18
|
+
Enabled: false
|
19
|
+
|
20
|
+
Style/LineEndConcatenation:
|
21
|
+
# Allow concatenation with `+` and `<<`
|
22
|
+
# This was built to micro-optimize string concatenation by using `\`
|
23
|
+
# since `+` and `<<` have a method call https://github.com/bbatsov/rubocop/issues/759
|
24
|
+
# However, any decent compiler *should* optimize the concatenation away
|
25
|
+
Enabled: False
|
26
|
+
|
27
|
+
Style/MethodCallParentheses:
|
28
|
+
# Allow usage of parens for argument-less invocations
|
29
|
+
# App.new()
|
30
|
+
Enabled: false
|
31
|
+
|
32
|
+
Style/RedundantReturn:
|
33
|
+
# Allow to explicitly state that a function should return
|
34
|
+
# e.g. `return 1` instead of `1`
|
35
|
+
Enabled: false
|
36
|
+
|
37
|
+
Style/RegexpLiteral:
|
38
|
+
# Always use %r{...} around regular expressions
|
39
|
+
# e.g. `%r{pattern}` instead of `/pattern/`
|
40
|
+
EnforcedStyle: percent_r
|
41
|
+
|
42
|
+
Style/SpaceAroundEqualsInParameterDefault:
|
43
|
+
# Disable spaces around equal sign in default parameters
|
44
|
+
# e.g. `func(param=nil)` instead of `func(param = nil)`
|
45
|
+
EnforcedStyle: no_space
|
46
|
+
|
47
|
+
Style/StringLiterals:
|
48
|
+
# Always use double quotes as we find it tedious to jump back/forth when adding/removing interpolation
|
49
|
+
EnforcedStyle: double_quotes
|
50
|
+
|
51
|
+
Style/TrailingComma:
|
52
|
+
# Force trailing comma for multiline array definitions
|
53
|
+
EnforcedStyleForMultiline: comma
|
54
|
+
|
55
|
+
Style/WordArray:
|
56
|
+
# Prefer normal string array `["string"]` over word arrays `%w(string)`
|
57
|
+
# DEV: This is to make Ruby more approachable for non-Ruby users
|
58
|
+
Enabled: false
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE-MIT
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Underdog.io
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person
|
4
|
+
obtaining a copy of this software and associated documentation
|
5
|
+
files (the "Software"), to deal in the Software without
|
6
|
+
restriction, including without limitation the rights to use,
|
7
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
+
copies of the Software, and to permit persons to whom the
|
9
|
+
Software is furnished to do so, subject to the following
|
10
|
+
conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
17
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
19
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
20
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
21
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
WebMock fixtures
|
2
|
+
================
|
3
|
+
Library for managing [WebMock][] fixtures.
|
4
|
+
|
5
|
+
[WebMock]: https://github.com/bblimke/webmock
|
6
|
+
|
7
|
+
[Documentation](http://www.rubydoc.info/github/underdogio/webmock-fixtures).
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
`gem install webmock-fixtures`
|
11
|
+
|
12
|
+
or with Bundler
|
13
|
+
|
14
|
+
`gem "webmock-fixtures"`
|
15
|
+
|
16
|
+
## Getting Started
|
17
|
+
WebMock fixtures provides a way to pre-define all of your fixtures ahead of time and start them easily during tests.
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
require "rspec"
|
21
|
+
require "webmock/fixtures"
|
22
|
+
require "webmock/rspec"
|
23
|
+
|
24
|
+
# Register our fixture for www.example.org
|
25
|
+
WebMock::Fixtures::Manager.register_fixture(
|
26
|
+
:get_example, :get, %r{www.example.org}, :body => "Hello World")
|
27
|
+
|
28
|
+
RSpec.describe do
|
29
|
+
describe "a web request to www.example.org" do
|
30
|
+
# Start the registered fixtures
|
31
|
+
# DEV: `WebMock` will auto-reset after each test:
|
32
|
+
# https://github.com/bblimke/webmock/blob/v1.22.3/lib/webmock/rspec.rb#L23-L31
|
33
|
+
let!(:manager) { WebMock::Fixtures::Manager.run([:get_example]) }
|
34
|
+
|
35
|
+
it "should respond with the fixture body" do
|
36
|
+
# Assert that an HTTP request gets the expected response
|
37
|
+
response = Net::HTTP.get("www.example.org", "/")
|
38
|
+
expect(response).to(eq("Hello World"))
|
39
|
+
|
40
|
+
# Assert that our stub was called
|
41
|
+
# DEV: `stub` is the created `WebMock::RequestStub` for the started fixture
|
42
|
+
stub = manager[:get_example]
|
43
|
+
expect(stub).to(have_been_requested.once)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
```
|
48
|
+
|
49
|
+
As well, by subclassing `WebMock::Fixtures::Manager` you can easily separate and organize your test fixtures. For example:
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
# Manager to store fixtures associated with Google
|
53
|
+
class GoogleManager < WebMock::Fixtures::Manager
|
54
|
+
# Define fixtures in class definition
|
55
|
+
@fixtures = {
|
56
|
+
:get_homepage => {
|
57
|
+
:verb => :get,
|
58
|
+
:pattern => %r{www.google.com},
|
59
|
+
:response => {
|
60
|
+
:body => "Google",
|
61
|
+
},
|
62
|
+
},
|
63
|
+
}
|
64
|
+
end
|
65
|
+
|
66
|
+
# Manager to store fixtures associated with Elasticsearch
|
67
|
+
class ElasticsearchManager < WebMock::Fixtures::Manager
|
68
|
+
end
|
69
|
+
ElasticsearchManager.register_fixture_file(
|
70
|
+
:search_people, :get, %r{localhost:9002/people/person/_search}, "/path/to/file.raw")
|
71
|
+
|
72
|
+
google_manager = GoogleManager.run([:get_homepage])
|
73
|
+
elasticsearch_manager = ElasticsearchManager.run([:search_people])
|
74
|
+
```
|
75
|
+
|
76
|
+
## Contributing
|
77
|
+
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality.
|
78
|
+
|
79
|
+
## License
|
80
|
+
Copyright (c) 2015 Underdog.io
|
81
|
+
|
82
|
+
Licensed under the MIT license.
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require "rspec/core/rake_task"
|
2
|
+
require "rubocop/rake_task"
|
3
|
+
|
4
|
+
task :default => :test
|
5
|
+
|
6
|
+
desc "Run RSpec on the source"
|
7
|
+
task :spec do
|
8
|
+
RSpec::Core::RakeTask.new(:spec)
|
9
|
+
end
|
10
|
+
|
11
|
+
desc "Run RuboCop on the source"
|
12
|
+
task :lint do
|
13
|
+
RuboCop::RakeTask.new(:lint) do |task|
|
14
|
+
task.options = ["--config", ".rubocop.yml"]
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
desc "Run RSpec and RuboCop on the source"
|
19
|
+
task :test do
|
20
|
+
Rake::Task[:lint].invoke
|
21
|
+
Rake::Task[:spec].invoke
|
22
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require "rspec"
|
2
|
+
require "webmock/fixtures"
|
3
|
+
require "webmock/rspec"
|
4
|
+
|
5
|
+
# Register our fixture for www.example.org
|
6
|
+
WebMock::Fixtures::Manager.register_fixture(
|
7
|
+
:get_example, :get, %r{www.example.org}, :body => "Hello World")
|
8
|
+
|
9
|
+
RSpec.describe do
|
10
|
+
describe "a web request to www.example.org" do
|
11
|
+
# Start the registered fixtures
|
12
|
+
# DEV: `WebMock` will auto-reset after each test:
|
13
|
+
# https://github.com/bblimke/webmock/blob/v1.22.3/lib/webmock/rspec.rb#L23-L31
|
14
|
+
let!(:manager) { WebMock::Fixtures::Manager.run([:get_example]) }
|
15
|
+
|
16
|
+
it "should respond with the fixture body" do
|
17
|
+
# Assert that an HTTP request gets the expected response
|
18
|
+
response = Net::HTTP.get("www.example.org", "/")
|
19
|
+
expect(response).to(eq("Hello World"))
|
20
|
+
|
21
|
+
# Assert that our stub was called
|
22
|
+
# DEV: `stub` is the created `WebMock::RequestStub` for the started fixture
|
23
|
+
stub = manager[:get_example]
|
24
|
+
expect(stub).to(have_been_requested.once)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
module WebMock
|
2
|
+
# WebMock fixture helper
|
3
|
+
module Fixtures
|
4
|
+
# Class used to manage WebMock fixtures
|
5
|
+
class Manager
|
6
|
+
# Available fixtures which have been loaded
|
7
|
+
@fixtures = {}
|
8
|
+
|
9
|
+
# Fetch the started `WebMock::RequestStub`s that belong to this manager
|
10
|
+
# @return [Hash] a Hash of fixture names mapped to their started `WebMock::RequestStub`
|
11
|
+
attr_reader :started_fixtures
|
12
|
+
|
13
|
+
# Constructor for Manager
|
14
|
+
def initialize
|
15
|
+
# We will use `@started_fixtures` to store our started fixtures
|
16
|
+
@started_fixtures = {}
|
17
|
+
end
|
18
|
+
|
19
|
+
# Fetch the `WebMock::RequestStub` for any started fixtures
|
20
|
+
# @param fixture_name [Symbol] the symbol name of the fixture
|
21
|
+
# @return [WebMock::RequestStub] the started fixture stub
|
22
|
+
# @raise [KeyError] if the fixture requested has not been started
|
23
|
+
def [](fixture_name)
|
24
|
+
# DEV: `fetch` wil raise `KeyError` if `fixture_name` does not exist
|
25
|
+
return @started_fixtures.fetch(fixture_name)
|
26
|
+
end
|
27
|
+
|
28
|
+
# Store an instance of `WebMock::RequestStub` for a given fixture
|
29
|
+
# @param fixture_name [Symbol] the symbol name of the fixture started
|
30
|
+
# @param stub [WebMock::RequestStub] the stub that was started for the fixture
|
31
|
+
def []=(fixture_name, stub)
|
32
|
+
@started_fixtures[fixture_name] = stub
|
33
|
+
end
|
34
|
+
|
35
|
+
# Ensure every subclass of `Manager` sets `@fixtures` to `{}` (by default it will be `nil`)
|
36
|
+
# @classmethod
|
37
|
+
# @param subclass [Class] the class instance which is subclassing `Manager`
|
38
|
+
def self.inherited(subclass)
|
39
|
+
subclass.instance_variable_set(:@fixtures, {})
|
40
|
+
end
|
41
|
+
|
42
|
+
# Preload a web fixture which can then be started via `Manager.run`
|
43
|
+
# @classmethod
|
44
|
+
# @param name [Symbol] the name to register the fixture as
|
45
|
+
# @param verb [Symbol] symbol method to mock (e.g. `:get`, `:post`)
|
46
|
+
# @param pattern [Regexp|String] URI pattern to match for this mock
|
47
|
+
# @param response [String|File|Lambda|Hash] the response, this is the same as what would be supplied
|
48
|
+
# to `WebMock::RequestStub#to_return` please see https://github.com/bblimke/webmock for examples
|
49
|
+
def self.register_fixture(name, verb, pattern, response)
|
50
|
+
@fixtures[name] = {
|
51
|
+
:pattern => pattern,
|
52
|
+
:verb => verb,
|
53
|
+
:response => response,
|
54
|
+
}
|
55
|
+
end
|
56
|
+
|
57
|
+
# Retrieve a hash of registered fixtures available to this Manager
|
58
|
+
# @classmethod
|
59
|
+
# @return [Hash] a hash mapping the registered name to a hash of properties
|
60
|
+
# e.g. `{ :get_example => { :pattern => %r{example.org}, :verb => :get, :response => "Hello World" } }`
|
61
|
+
def self.fixtures
|
62
|
+
return @fixtures
|
63
|
+
end
|
64
|
+
|
65
|
+
# Reset this manager by removing all previously registered fixtures
|
66
|
+
# @classmethod
|
67
|
+
def self.reset!
|
68
|
+
@fixtures = {}
|
69
|
+
end
|
70
|
+
|
71
|
+
# Preload a web fixture from a file which can then be started via `Manager.run`
|
72
|
+
# @classmethod
|
73
|
+
# @param name [Symbol] the name to register the fixture as
|
74
|
+
# @param verb [Symbol] symbol method to mock (e.g. `:get`, `:post`)
|
75
|
+
# @param pattern [Regexp|String] URI pattern to match for this mock
|
76
|
+
# @param file_name [String] the name of the file to load the fixture response from
|
77
|
+
def self.register_fixture_file(name, verb, pattern, file_name)
|
78
|
+
# Read the contents from the file and use as the response
|
79
|
+
# https://github.com/bblimke/webmock/tree/v1.22.1#replaying-raw-responses-recorded-with-curl--is
|
80
|
+
register_fixture(name, verb, pattern, File.new(file_name).read)
|
81
|
+
end
|
82
|
+
|
83
|
+
# Start the named preloaded web fixtures
|
84
|
+
# @classmethod
|
85
|
+
# @param fixture_names [Array] the symbol names of fixtures to load and start
|
86
|
+
# @return [Manager] an instance of Manager which has properties set for each started fixture
|
87
|
+
# @raise [KeyError] if the fixture has not been preloaded via `::register_fixture` or `::register_fixture_file`
|
88
|
+
def self.run(fixture_names)
|
89
|
+
# Create a new instance to store started mocks on
|
90
|
+
manager = new()
|
91
|
+
fixture_names.each do |fixture_name|
|
92
|
+
# DEV: `fetch` will fail if the key does not exist
|
93
|
+
unless @fixtures.key?(fixture_name)
|
94
|
+
fail(KeyError, "The fixture \"#{fixture_name}\" was not found. Please make sure to " +
|
95
|
+
"register it with ::register_fixture or ::register_fixture_file")
|
96
|
+
end
|
97
|
+
|
98
|
+
# Create the WebMock stub
|
99
|
+
fixture_options = @fixtures[fixture_name]
|
100
|
+
stub = WebMock::API.stub_request(fixture_options[:verb], fixture_options[:pattern])
|
101
|
+
stub.to_return(fixture_options[:response])
|
102
|
+
|
103
|
+
# Store the started stub on the manager instance
|
104
|
+
manager[fixture_name] = stub
|
105
|
+
end
|
106
|
+
return manager
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
HTTP/1.1 200 OK
|
2
|
+
Server: nginx
|
3
|
+
Date: Mon, 09 Nov 2015 21:14:59 GMT
|
4
|
+
Content-Type: application/json
|
5
|
+
Content-Length: 211
|
6
|
+
Connection: keep-alive
|
7
|
+
Access-Control-Allow-Origin: *
|
8
|
+
Access-Control-Allow-Credentials: true
|
9
|
+
|
10
|
+
{
|
11
|
+
"args": {
|
12
|
+
"page": "1"
|
13
|
+
},
|
14
|
+
"headers": {
|
15
|
+
"Accept": "*/*",
|
16
|
+
"Host": "httpbin.org",
|
17
|
+
"User-Agent": "curl/7.43.0"
|
18
|
+
},
|
19
|
+
"origin": "69.12.27.190",
|
20
|
+
"url": "http://httpbin.org/get?page=1"
|
21
|
+
}
|
@@ -0,0 +1,180 @@
|
|
1
|
+
require "webmock/fixtures"
|
2
|
+
require "webmock/rspec"
|
3
|
+
|
4
|
+
describe WebMock::Fixtures::Manager do
|
5
|
+
after(:example) do
|
6
|
+
# Ensure we remove any added fixtures between tests
|
7
|
+
WebMock::Fixtures::Manager.reset!
|
8
|
+
end
|
9
|
+
|
10
|
+
before(:example) do
|
11
|
+
# Register a single fixture before each test
|
12
|
+
WebMock::Fixtures::Manager.register_fixture(:get_example, :get, %r{www.example.org}, :body => "Hello World")
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "::reset!" do
|
16
|
+
it "should remove any registered fixtures" do
|
17
|
+
expect(WebMock::Fixtures::Manager.fixtures[:get_example]).to_not(be_nil)
|
18
|
+
WebMock::Fixtures::Manager.reset!
|
19
|
+
expect(WebMock::Fixtures::Manager.fixtures).to(eq({}))
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "::register_fixture" do
|
24
|
+
context "with a subclassed manager" do
|
25
|
+
it "should not share registered fixtures" do
|
26
|
+
class TestManager < WebMock::Fixtures::Manager
|
27
|
+
@fixtures = {
|
28
|
+
:get_google => {
|
29
|
+
:verb => :get,
|
30
|
+
:pattern => %r{www.google.com},
|
31
|
+
:response => {
|
32
|
+
:body => "Google",
|
33
|
+
},
|
34
|
+
},
|
35
|
+
}
|
36
|
+
end
|
37
|
+
|
38
|
+
# Ensure TestManager has the appropriate fixtures
|
39
|
+
expect(TestManager.fixtures.keys()).to(eq([:get_google]))
|
40
|
+
expect(TestManager.fixtures[:get_google]).to_not(be_nil)
|
41
|
+
|
42
|
+
# Ensure WebMock::Fixtures::Manager has the appropriate fixtures
|
43
|
+
expect(WebMock::Fixtures::Manager.fixtures.keys()).to(eq([:get_example]))
|
44
|
+
expect(WebMock::Fixtures::Manager.fixtures[:get_google]).to(be_nil)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should properly register the fixture" do
|
49
|
+
expected = {
|
50
|
+
:pattern => %r{www.example.org},
|
51
|
+
:verb => :get,
|
52
|
+
:response => {
|
53
|
+
:body => "Hello World",
|
54
|
+
},
|
55
|
+
}
|
56
|
+
expect(WebMock::Fixtures::Manager.fixtures[:get_example]).to(eq(expected))
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should properly respond to a web request" do
|
60
|
+
# Start our fixtures
|
61
|
+
manager = WebMock::Fixtures::Manager.run([:get_example])
|
62
|
+
|
63
|
+
# Assert http call responds with our fixture
|
64
|
+
response = Net::HTTP.get("www.example.org", "/")
|
65
|
+
expect(response).to(eq("Hello World"))
|
66
|
+
|
67
|
+
# Assert our stub was requested
|
68
|
+
stub = manager[:get_example]
|
69
|
+
expect(stub).to(have_been_requested.once)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
describe "::register_fixture_file" do
|
74
|
+
before(:example) do
|
75
|
+
fixture_path = File.join(File.expand_path(File.dirname(__FILE__)), "get_httpbin_200.raw")
|
76
|
+
WebMock::Fixtures::Manager.register_fixture_file(
|
77
|
+
:get_httpbin, :get, %r{^http://httpbin\.org/get\?page=1$}, fixture_path)
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should properly register a fixture" do
|
81
|
+
fixture = WebMock::Fixtures::Manager.fixtures[:get_httpbin]
|
82
|
+
expect(fixture[:verb]).to(eq(:get))
|
83
|
+
expect(fixture[:pattern]).to(eq(%r{^http://httpbin\.org/get\?page=1$}))
|
84
|
+
|
85
|
+
# Assert that we loaded the raw HTTP request from the file as a string
|
86
|
+
expect(fixture[:response]).to(be_a(String))
|
87
|
+
# Assert we have response headers
|
88
|
+
expect(fixture[:response]).to(match(%r{Content-Type: application/json}))
|
89
|
+
# Assert we have part of the response body
|
90
|
+
expect(fixture[:response]).to(match(%r{"url": "http://httpbin\.org/get\?page=1"}))
|
91
|
+
end
|
92
|
+
|
93
|
+
it "should properly respond to a web request" do
|
94
|
+
# Start our fixtures
|
95
|
+
manager = WebMock::Fixtures::Manager.run([:get_httpbin])
|
96
|
+
|
97
|
+
# Assert http call responds with our fixture
|
98
|
+
response = Net::HTTP.get(URI("http://httpbin.org/get?page=1"))
|
99
|
+
response = JSON.load(response)
|
100
|
+
expect(response["url"]).to(eq("http://httpbin.org/get?page=1"))
|
101
|
+
|
102
|
+
# Assert that our stub was requested
|
103
|
+
stub = manager[:get_httpbin]
|
104
|
+
expect(stub).to(have_been_requested.once)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
describe "::run" do
|
109
|
+
context "with an unknown fixture name" do
|
110
|
+
it "should raise an error" do
|
111
|
+
expect { WebMock::Fixtures::Manager.run([:unknown_fixture]) } .to(raise_error(KeyError))
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
context "with multiple instances" do
|
116
|
+
before(:example) do
|
117
|
+
# Register an additional fixture to use during these tests
|
118
|
+
WebMock::Fixtures::Manager.register_fixture(:get_google, :get, %r{www.google.com}, :body => "Google")
|
119
|
+
end
|
120
|
+
|
121
|
+
it "should not share started fixtures" do
|
122
|
+
google_manager = WebMock::Fixtures::Manager.run([:get_google])
|
123
|
+
example_manager = WebMock::Fixtures::Manager.run([:get_example])
|
124
|
+
|
125
|
+
expect(google_manager.started_fixtures.keys()).to(eq([:get_google]))
|
126
|
+
expect(example_manager.started_fixtures.keys()).to(eq([:get_example]))
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
it "should return a manager" do
|
131
|
+
manager = WebMock::Fixtures::Manager.run([:get_example])
|
132
|
+
expect(manager).to(be_a(WebMock::Fixtures::Manager))
|
133
|
+
end
|
134
|
+
|
135
|
+
it "should start the requested request stub" do
|
136
|
+
manager = WebMock::Fixtures::Manager.run([:get_example])
|
137
|
+
expect(manager.started_fixtures.keys()).to(eq([:get_example]))
|
138
|
+
expect(manager.started_fixtures[:get_example]).to(be_a(WebMock::RequestStub))
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
describe "::new" do
|
143
|
+
it "should not start any fixtures" do
|
144
|
+
manager = WebMock::Fixtures::Manager.new()
|
145
|
+
expect(manager.started_fixtures).to(eq({}))
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
describe "#[]" do
|
150
|
+
it "should retreive a started fixture" do
|
151
|
+
manager = WebMock::Fixtures::Manager.run([:get_example])
|
152
|
+
expect(manager[:get_example]).to(be_a(WebMock::RequestStub))
|
153
|
+
end
|
154
|
+
|
155
|
+
it "should raise an error with an unknown fixture" do
|
156
|
+
manager = WebMock::Fixtures::Manager.run([:get_example])
|
157
|
+
expect { manager[:unknown_example] }.to(raise_error(KeyError))
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
describe "#[]=" do
|
162
|
+
it "should assign over a known fixture" do
|
163
|
+
manager = WebMock::Fixtures::Manager.run([:get_example])
|
164
|
+
manager[:get_example] = "Test"
|
165
|
+
expect(manager[:get_example]).to(eq("Test"))
|
166
|
+
end
|
167
|
+
|
168
|
+
it "should assign a new fixture" do
|
169
|
+
manager = WebMock::Fixtures::Manager.run([:get_example])
|
170
|
+
manager[:unknown_example] = "Test"
|
171
|
+
expect(manager[:unknown_example]).to(eq("Test"))
|
172
|
+
end
|
173
|
+
|
174
|
+
it "should not effect existing fixtures" do
|
175
|
+
manager = WebMock::Fixtures::Manager.run([:get_example])
|
176
|
+
manager[:unknown_example] = "Test"
|
177
|
+
expect(manager[:get_example]).to(be_a(WebMock::RequestStub))
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
$LOAD_PATH.push File.expand_path("../lib", __FILE__)
|
2
|
+
require "webmock/fixtures/version"
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
# Gem information
|
6
|
+
s.name = "webmock-fixtures"
|
7
|
+
s.version = WebMock::Fixtures::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Brett Langdon"]
|
10
|
+
s.email = ["me@brett.is"]
|
11
|
+
s.homepage = "http://github.com/underdogio/webmock-fixtures"
|
12
|
+
s.summary = "Manage WebMock fixtures"
|
13
|
+
s.description = "Library to help manage WebMock fixtures"
|
14
|
+
s.license = "MIT"
|
15
|
+
|
16
|
+
# Dependencies
|
17
|
+
s.add_runtime_dependency("webmock", [">= 1.0.0", "< 2.0.0"])
|
18
|
+
s.add_development_dependency("rake", [">= 10.0.0", "< 11.0.0"])
|
19
|
+
s.add_development_dependency("rspec", [">= 3.3.0", "< 3.4.0"])
|
20
|
+
s.add_development_dependency("rubocop", [">= 0.34.0", "< 0.35.0"])
|
21
|
+
|
22
|
+
# Files
|
23
|
+
s.files = `git ls-files`.split("\n")
|
24
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
25
|
+
s.require_paths = ["lib"]
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: webmock-fixtures
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brett Langdon
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-11-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: webmock
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.0.0
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 2.0.0
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 1.0.0
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 2.0.0
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: rake
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: 10.0.0
|
40
|
+
- - "<"
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: 11.0.0
|
43
|
+
type: :development
|
44
|
+
prerelease: false
|
45
|
+
version_requirements: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: 10.0.0
|
50
|
+
- - "<"
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: 11.0.0
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: rspec
|
55
|
+
requirement: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: 3.3.0
|
60
|
+
- - "<"
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 3.4.0
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 3.3.0
|
70
|
+
- - "<"
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: 3.4.0
|
73
|
+
- !ruby/object:Gem::Dependency
|
74
|
+
name: rubocop
|
75
|
+
requirement: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: 0.34.0
|
80
|
+
- - "<"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.35.0
|
83
|
+
type: :development
|
84
|
+
prerelease: false
|
85
|
+
version_requirements: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.34.0
|
90
|
+
- - "<"
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: 0.35.0
|
93
|
+
description: Library to help manage WebMock fixtures
|
94
|
+
email:
|
95
|
+
- me@brett.is
|
96
|
+
executables: []
|
97
|
+
extensions: []
|
98
|
+
extra_rdoc_files: []
|
99
|
+
files:
|
100
|
+
- ".gitignore"
|
101
|
+
- ".rubocop.yml"
|
102
|
+
- ".travis.yml"
|
103
|
+
- Gemfile
|
104
|
+
- LICENSE-MIT
|
105
|
+
- README.md
|
106
|
+
- Rakefile
|
107
|
+
- examples/getting_started_spec.rb
|
108
|
+
- lib/webmock/fixtures.rb
|
109
|
+
- lib/webmock/fixtures/manager.rb
|
110
|
+
- lib/webmock/fixtures/version.rb
|
111
|
+
- spec/get_httpbin_200.raw
|
112
|
+
- spec/manager_spec.rb
|
113
|
+
- webmock-fixtures.gemspec
|
114
|
+
homepage: http://github.com/underdogio/webmock-fixtures
|
115
|
+
licenses:
|
116
|
+
- MIT
|
117
|
+
metadata: {}
|
118
|
+
post_install_message:
|
119
|
+
rdoc_options: []
|
120
|
+
require_paths:
|
121
|
+
- lib
|
122
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
requirements: []
|
133
|
+
rubyforge_project:
|
134
|
+
rubygems_version: 2.2.2
|
135
|
+
signing_key:
|
136
|
+
specification_version: 4
|
137
|
+
summary: Manage WebMock fixtures
|
138
|
+
test_files:
|
139
|
+
- spec/get_httpbin_200.raw
|
140
|
+
- spec/manager_spec.rb
|
141
|
+
has_rdoc:
|