webmock-server 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YThkYTI3MzI0MDgwZWY2YTEwZWUzOWE1M2Y1ZjU2OTljNmQxYzA5Zg==
5
+ data.tar.gz: !binary |-
6
+ ZDVhMWE4YzU4ZTMzZjc3NjQ1OGU4YjU3MTg2ZDA5OGFhNGFkY2I4MA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ YzJiNmRmYjg3OThmZTJlNTM2NGMwM2RjY2MzNTgxNTYxYTM1NWEwOGZhZDdm
10
+ NGFiYmIxZGZjMjI3YTkyYjIzY2E1NzE4MDhmMDA4OWZhMTA1NDdhZDhlMjll
11
+ ZDdkMTkzNjhiMDg1Mzc2NDViNzAwMTZmZTE3ZTJkYjNlM2Q3MTI=
12
+ data.tar.gz: !binary |-
13
+ ZmNhZDNiNTA3ZDJhNjM4ZGU2YzU0YjE1ZTcxZWM5YTRjNjM3Y2M3M2FlOThk
14
+ Mzc0ODAwMTdmM2RhOTVkMjQ4OTQyY2M5MzJjNGIwMDEzMzQ3ZDRmM2ZmOWRj
15
+ YTgyZTdhYzdhY2MwZmVmY2Y0YTJkYWYwNTViYzVjNDkxOTYxNjk=
data/.gitignore ADDED
@@ -0,0 +1,23 @@
1
+ .ruby-*
2
+ *.gem
3
+ *.rbc
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
19
+ *.bundle
20
+ *.so
21
+ *.o
22
+ *.a
23
+ mkmf.log
data/.travis.yml ADDED
@@ -0,0 +1,14 @@
1
+ ---
2
+ language: ruby
3
+ rvm:
4
+ - 2.0.0
5
+ - 2.1.1
6
+ notifications:
7
+ email: false
8
+ deploy:
9
+ provider: rubygems
10
+ api_key:
11
+ secure: GovDViLCjDKdoGK35Y462dswcOAWnl+gB/wBJw0NzJk6cnzQDGcBBQlc/2b0p09RzvvVpjB2/p61wuatbBnJCU9sYgWNid3fz01lf3gD7KNypxbGoJBSkUTvHM0CYtdOiV1Owt2mdt8eQauRrvWvjR62rEbwB6TrJMook20YB2k=
12
+ on:
13
+ all_branches: true
14
+ tags: true
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in webmock-server.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 rdsubhas
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # WebMock::Server
2
+
3
+ [![Build Status](https://travis-ci.org/rdsubhas/webmock-server.svg?branch=master)](https://travis-ci.org/rdsubhas/webmock-server)
4
+
5
+ A fast, lightweight stub server using WebMock and Rack!
6
+
7
+ [WebMock](https://github.com/bblimke/webmock) is a popular HTTP mocking library with great features. But it has one limitation: It mocks only at the language/framework/library level (e.g. it mocks Net::HTTP directly).
8
+
9
+ This gem bridges that gap. It starts a barebones Rack application which will delegate requests to WebMock. So you get all the features of WebMock for stubbing *as well as* a running server that can return the stubbed responses. Even better, you can control the running server directly from your tests, so you can now have different stubs for different test scenarios.
10
+
11
+ ## Rationale
12
+
13
+ #### vs [WebMock](https://github.com/bblimke/webmock)
14
+
15
+ WebMock is very popular and feature packed, but as explained above, it works by patching Net::HTTP (or other language-level libraries) directly. It does not come with a real HTTP server.
16
+
17
+ If you're testing a non-Ruby web app (e.g. Java) using Cucumber/Capybara, or doing black box testing, or testing a bunch of microservices - stubbing libraries is not going to work, you'll need a live server, and that's where WebMock::Server fits in.
18
+
19
+ WebMock::Server is also built on top of WebMock, so you get the full power of WebMock already!
20
+
21
+ #### vs [Stubby4J](https://github.com/azagniotov/stubby4j)
22
+
23
+ Stubby4J runs a real HTTP server, where all the stubbing configuration is defined in simple YML files. Once you've defined the YML files, you cannot change the behavior at runtime. This can be both good and bad. If you have a `/login` endpoint, and for one test you want it to return `200` and for another you want to return `400` - it becomes difficult now since the configuration is static. So you'll end up using different *data or keywords* (e.g. username/password) just to trigger different stubs, and you have to make sure that your tests don't step on each other.
24
+
25
+ Another common problem with static configuration is that, it leads to reusing same YML files for multiple tests, and this causes interdependency between tests (i.e. you change one YML and many tests get affected). And finally, you will have difficulty finding out which YML file is tied to which scenario, because its not a direct correlation.
26
+
27
+ In WebMock::Server, your stubs lie right near your steps, so there are no shared files or configurations and thus no dependencies between tests. All this because WebMock::Server can be controlled at runtime. It doesn't stop you from having shared stubs if you *really* need it though (e.g. sometimes you may want some default login/logout responses to be available all the time). If you are using RSpec - setup the shared stubs under `config.before(:each)`. Or if you're using Cucumber, use `Before` hooks. So it becomes completely obvious and under your control which ones are shared and which are not.
28
+
29
+ #### Other Tools
30
+
31
+ * [VCR](https://github.com/vcr/vcr) with [PuffingBilly](https://github.com/oesmith/puffing-billy) are not really stub servers, but they are built to record and re-run HTTP API calls. You record actual HTTP requests that hit your real application, and then Play them back during the tests. This again can be good or bad. Its good because it hits real application code. But it leads to specific ordering and dependencies in tests. And everytime you write a new test, you have to first record against a real server, so you still need to have seeding and truncating as part of your test steps. If you're testing complex applications (like a bunch of microservices), everytime you write a new test, you have to run and manage the data in multiple instances. So while replaying tests is easy, creating new tests is more complex.
32
+
33
+ * [Interfake](https://github.com/basicallydan/interfake) is very similar, and it even has a HTTP API to control the stubs (controlling HTTP over HTTP ftw). Except when it comes to actual matching of requests, its not as feature packed as WebMock.
34
+
35
+ #### Other Use Cases
36
+
37
+ Since WebMock::Server can be controlled at runtime, you can write your stubs in such a way that, whenever you get a call to create an entity, say `POST /users`, then you can automatically setup default responses for `GET /users/id`, `DELETE /users/id`, `PUT /users/id`, etc.
38
+
39
+ `TODO: Example with automatic stubs`
40
+
41
+ ## Usage
42
+
43
+ * Add the gem to your Gemfile
44
+ * Start the stub server by saying `WebMock::Server.start <port>`
45
+ * Use [WebMock](https://github.com/bblimke/webmock) to stub responses, like:
46
+
47
+ WebMock::API.stub_request("http://stubme/").to_return(status: 200, body: 'test')
48
+
49
+ * Note that we're using the same Stub URL that we specified earlier to `WebMock::Server.start`
50
+ * You can use the full power of WebMock such as query parameter matching, request body matching, regular expression URLs and much more!
51
+
52
+ * See the `examples` folder for some partial list of examples. But remember: You can stub whatever WebMock can! So check the WebMock guide as well on how to stub.
53
+
54
+ ## Contributing
55
+
56
+ 1. Fork it ( https://github.com/rdsubhas/webmock-server/fork )
57
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
58
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
59
+ 4. Push to the branch (`git push origin my-new-feature`)
60
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,16 @@
1
+ require "webmock/server/version"
2
+
3
+ module WebMock
4
+ module Server
5
+ STUB_URI = 'http://stubme'
6
+
7
+ autoload :Handler, "webmock/server/handler"
8
+ autoload :API, "webmock/server/api"
9
+ autoload :Cucumber, "webmock/server/cucumber"
10
+
11
+ def self.start(port)
12
+ require "rack"
13
+ ::Rack::Handler::WEBrick.run Handler.new, Port: port
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,31 @@
1
+ module WebMock
2
+ module Server
3
+ module API
4
+
5
+ def stub_server_request(method, uri)
6
+ WebMock::API.stub_request(method, stubbed_uri(uri))
7
+ end
8
+
9
+ def a_server_request(method, uri)
10
+ WebMock::API.a_request(method, stubbed_uri(uri))
11
+ end
12
+
13
+ private
14
+
15
+ def stubbed_uri(uri)
16
+ if uri.is_a? String
17
+ stub_uri + uri
18
+ elsif uri.is_a? Regexp
19
+ Regexp.new stub_uri + uri.source
20
+ elsif uri.is_a? Addressable::Template
21
+ Addressable::Template.new stub_uri + uri.pattern
22
+ end
23
+ end
24
+
25
+ def stub_uri
26
+ STUB_URI
27
+ end
28
+
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,7 @@
1
+ require 'webmock/server/api'
2
+
3
+ World(WebMock::Server::API)
4
+
5
+ Given /^I stub ([^ ]+) "(.+)"$/ do |method, url|
6
+ @stub = stub_server_request(method.downcase.to_sym, url)
7
+ end
@@ -0,0 +1,38 @@
1
+ require "webmock"
2
+ require "rack"
3
+
4
+ module WebMock
5
+ module Server
6
+ class Handler
7
+
8
+ def call(env)
9
+ uri = stub_uri
10
+ uri.path = env['PATH_INFO']
11
+ uri.query = env['QUERY_STRING']
12
+
13
+ request = case env['REQUEST_METHOD']
14
+ when 'GET'
15
+ Net::HTTP::Get.new uri
16
+ when 'POST'
17
+ Net::HTTP::Post.new uri
18
+ else
19
+ nil
20
+ end
21
+ request.body = env['rack.input'].gets if env['rack.input']
22
+ request['CONTENT_TYPE'] = env['CONTENT_TYPE']
23
+
24
+ response = Net::HTTP.new(uri.host, uri.port).request(request)
25
+
26
+ headers = Hash[response.to_hash.map { |k,v| [k, v[0]] }]
27
+ [ response.code, headers, [ response.body ]]
28
+ end
29
+
30
+ private
31
+
32
+ def stub_uri
33
+ URI.parse STUB_URI
34
+ end
35
+
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,5 @@
1
+ module WebMock
2
+ module Server
3
+ VERSION = "0.2.0"
4
+ end
5
+ end
data/spec/get_spec.rb ADDED
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'GET' do
4
+ it "root" do
5
+ stub_server_request(:get, '/')
6
+ .to_return(body: 'stub body', status: 201, headers: { 'x-custom-header' => 'stub header'})
7
+
8
+ get '/'
9
+ expect(last_response.status).to eq(201)
10
+ expect(last_response.body).to eq('stub body')
11
+ expect(last_response.headers['x-custom-header']).to eq('stub header')
12
+ end
13
+
14
+ it "path" do
15
+ stub_server_request(:get, '/test')
16
+ .to_return(body: 'stub body', status: 201, headers: { 'x-custom-header' => 'stub header'})
17
+
18
+ get '/test'
19
+ expect(last_response.status).to eq(201)
20
+ expect(last_response.body).to eq('stub body')
21
+ expect(last_response.headers['x-custom-header']).to eq('stub header')
22
+ end
23
+
24
+ it "query parameters" do
25
+ stub_server_request(:get, '/test')
26
+ .with(query: hash_including(x: 'y'))
27
+ .to_return(body: 'stub body', status: 201, headers: { 'x-custom-header' => 'stub header'})
28
+
29
+ get '/test?x=y'
30
+ expect(last_response.status).to eq(201)
31
+ expect(last_response.body).to eq('stub body')
32
+ expect(last_response.headers['x-custom-header']).to eq('stub header')
33
+ end
34
+ end
data/spec/post_spec.rb ADDED
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'POST' do
4
+ it "root" do
5
+ stub_server_request(:post, '/')
6
+ .to_return(body: 'stub body', status: 201, headers: { 'x-custom-header' => 'stub header'})
7
+
8
+ post '/'
9
+ expect(last_response.status).to eq(201)
10
+ expect(last_response.body).to eq('stub body')
11
+ expect(last_response.headers['x-custom-header']).to eq('stub header')
12
+ end
13
+
14
+ it "path" do
15
+ stub_server_request(:post, '/test')
16
+ .to_return(body: 'stub body', status: 201, headers: { 'x-custom-header' => 'stub header'})
17
+
18
+ post '/test'
19
+ expect(last_response.status).to eq(201)
20
+ expect(last_response.body).to eq('stub body')
21
+ expect(last_response.headers['x-custom-header']).to eq('stub header')
22
+ end
23
+
24
+ it "query parameters" do
25
+ stub_server_request(:post, '/test')
26
+ .with(query: hash_including(x: 'y'))
27
+ .to_return(body: 'stub body', status: 201, headers: { 'x-custom-header' => 'stub header'})
28
+
29
+ post '/test?x=y'
30
+ expect(last_response.status).to eq(201)
31
+ expect(last_response.body).to eq('stub body')
32
+ expect(last_response.headers['x-custom-header']).to eq('stub header')
33
+ end
34
+
35
+ it "request body" do
36
+ stub_server_request(:post, '/test')
37
+ .with(body: { x: 'y' })
38
+ .to_return(body: 'stub body', status: 201, headers: { 'x-custom-header' => 'stub header'})
39
+
40
+ post '/test', '{ "x" : "y" }', { 'CONTENT_TYPE' => 'application/json' }
41
+ expect(last_response.status).to eq(201)
42
+ expect(last_response.body).to eq('stub body')
43
+ expect(last_response.headers['x-custom-header']).to eq('stub header')
44
+ end
45
+ end
@@ -0,0 +1,20 @@
1
+ require "bundler/setup"
2
+ Bundler.setup
3
+
4
+ require 'webmock'
5
+ require 'webmock/server'
6
+ require 'rack/test'
7
+
8
+ RSpec.configure do |config|
9
+ config.include WebMock::Server::API
10
+ config.include Rack::Test::Methods
11
+
12
+ config.before :each do
13
+ WebMock.allow_net_connect!
14
+ WebMock.reset!
15
+ end
16
+
17
+ def app
18
+ WebMock::Server::Handler.new
19
+ end
20
+ 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 'webmock/server/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "webmock-server"
8
+ spec.version = WebMock::Server::VERSION
9
+ spec.authors = ["rdsubhas"]
10
+ spec.email = ["rdsubhas@gmail.com"]
11
+ spec.summary = %q{A fast, lightweight live stub server using WebMock and Rack}
12
+ spec.description = %q{WebMock allows you to stub only Net::HTTP requests. This gem bridges the gap and allows you to run a live stub server where all requests are stubbed directly using WebMock API}
13
+ spec.homepage = "http://github.com/rdsubhas/webmock-server"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "rack-test"
25
+
26
+ spec.add_dependency "webmock"
27
+ spec.add_dependency "rack"
28
+ end
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: webmock-server
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - rdsubhas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
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: rspec
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: rack-test
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: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
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: rack
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: WebMock allows you to stub only Net::HTTP requests. This gem bridges
98
+ the gap and allows you to run a live stub server where all requests are stubbed
99
+ directly using WebMock API
100
+ email:
101
+ - rdsubhas@gmail.com
102
+ executables: []
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - .gitignore
107
+ - .travis.yml
108
+ - Gemfile
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - lib/webmock/server.rb
113
+ - lib/webmock/server/api.rb
114
+ - lib/webmock/server/cucumber.rb
115
+ - lib/webmock/server/handler.rb
116
+ - lib/webmock/server/version.rb
117
+ - spec/get_spec.rb
118
+ - spec/post_spec.rb
119
+ - spec/spec_helper.rb
120
+ - webmock-server.gemspec
121
+ homepage: http://github.com/rdsubhas/webmock-server
122
+ licenses:
123
+ - MIT
124
+ metadata: {}
125
+ post_install_message:
126
+ rdoc_options: []
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ! '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ requirements: []
140
+ rubyforge_project:
141
+ rubygems_version: 2.2.2
142
+ signing_key:
143
+ specification_version: 4
144
+ summary: A fast, lightweight live stub server using WebMock and Rack
145
+ test_files:
146
+ - spec/get_spec.rb
147
+ - spec/post_spec.rb
148
+ - spec/spec_helper.rb