mock-web-service 0.7 → 0.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e3a587e637926beb6fe72ef84c971cd879c9b9d7
4
- data.tar.gz: 1cd23439eabc46159695becef50b1c9ab86fab13
3
+ metadata.gz: 5d271bc6486c63ee8546ce7ba810db4f5804689b
4
+ data.tar.gz: 3bdb1cd6040790d820463b2f462f95301d0bc025
5
5
  SHA512:
6
- metadata.gz: 3e89ce168a303049039a30a65d874661b64ed992fb0949ca8686eb7a52f008c11e61395513b44c3fb115b2dcfc4c6bf524b0598ac6ac02219765b527c2e69f04
7
- data.tar.gz: 25014abbc57b099fb5af8271d5f597d665df8c7702508d31e8744dbf09f84a5df8ad8fe617d6adbafcc4196e467d860426b7f8aed5211d5f600269984504094f
6
+ metadata.gz: 8b825be28e4d589d5e87e96423c5278ea8867a028de82be6af46aa9da945a7e5688a384ee835997833bb83401d26b9ea40fa3d75231c100e7fb99b8d345faf82
7
+ data.tar.gz: 0ede7bac4ddee569b6831676da0cb1f56b20bd99905ce2e5e6f93e840b5db4f3cc004b434e931e44fbb2b10421fc0b613c74ebe1f3848e4f6e2e9b0351e00904
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ /.idea
2
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,40 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mock-web-service (0.7)
5
+ sinatra
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.2.5)
11
+ rack (1.6.4)
12
+ rack-protection (1.5.3)
13
+ rack
14
+ rspec (3.0.0)
15
+ rspec-core (~> 3.0.0)
16
+ rspec-expectations (~> 3.0.0)
17
+ rspec-mocks (~> 3.0.0)
18
+ rspec-core (3.0.3)
19
+ rspec-support (~> 3.0.0)
20
+ rspec-expectations (3.0.3)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.0.0)
23
+ rspec-mocks (3.0.3)
24
+ rspec-support (~> 3.0.0)
25
+ rspec-support (3.0.3)
26
+ sinatra (1.4.6)
27
+ rack (~> 1.4)
28
+ rack-protection (~> 1.4)
29
+ tilt (>= 1.3, < 3)
30
+ tilt (2.0.1)
31
+
32
+ PLATFORMS
33
+ x86-mingw32
34
+
35
+ DEPENDENCIES
36
+ mock-web-service!
37
+ rspec (>= 2.12.0)
38
+
39
+ BUNDLED WITH
40
+ 1.10.6
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ mock-web-server
2
+ ===============
3
+
4
+ Tool to mock out web service requests
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec) do |spec|
6
+ spec.pattern = 'spec/**/*_spec.rb'
7
+ end
8
+
9
+ task :spec
@@ -0,0 +1,14 @@
1
+ module MockWebService
2
+ class ResponseOptions
3
+ attr_accessor :method, :path, :status, :content_type
4
+
5
+ def initialize
6
+ self.method = :get
7
+ self.path = '/'
8
+ self.status = 200
9
+ self.content_type = 'text/html;charset=utf-8'
10
+
11
+ yield self if block_given?
12
+ end
13
+ end
14
+ end
@@ -1,56 +1,74 @@
1
- require_relative 'async'
2
-
3
1
  module MockWebService
4
-
5
2
  class Server
6
3
  @@requests = []
4
+ @@responses = {}
5
+
6
+ def get_latest_request
7
+ @@requests.pop
8
+ end
7
9
 
8
- def Server::requests
10
+ def get_all_requests
9
11
  @@requests
10
12
  end
11
13
 
12
- def Server::requests=(value)
13
- @@requests = value
14
+ def clear_requests
15
+ @@requests = []
14
16
  end
15
17
 
16
18
  def start(overrides = {})
17
- server_settings_that_disable_logging = {AccessLog: []}
18
- options = {:bind => '0.0.0.0', :server_settings => server_settings_that_disable_logging}
19
- options.merge!(overrides)
19
+ disable_logging = {AccessLog: []}
20
+ options = {:bind => '0.0.0.0', :server_settings => disable_logging}.merge(overrides)
20
21
  Async.instance.run { Sinatra::Base.start!(options)}
21
22
  end
22
23
 
23
24
  def add_response(response)
24
25
  Sinatra::Base.reset!
25
26
  Sinatra::Base.post '*/*' do
27
+ content_type 'text/xml'
28
+ @@requests.push(request.body.read)
29
+ response
30
+ end
31
+ end
32
+
33
+ def add_soap_response(response)
34
+ set_response(ResponseOptions.new {|opts| opts.path = '*/*'; opts.method = :post}) do
26
35
  content_type 'text/xml'
27
- @@requests.push(request.body.read)
28
36
  response
29
37
  end
30
38
  end
31
39
 
32
- def wait_for_next_request(timeout_seconds = 5)
33
- waited_seconds = 0
34
- request = @@requests.pop
35
- while request.nil? && waited_seconds < timeout_seconds
36
- sleep 1
37
- waited_seconds +=1
38
- request = @@requests.pop
40
+ def add(response, opts = ResponseOptions.new)
41
+ @@responses[opts.path] = response
42
+ set_response(opts) do
43
+ status(opts.status)
44
+ content_type opts.content_type
45
+ @@requests.push(request)
46
+ return @@responses[opts.path]
39
47
  end
40
- request
41
48
  end
42
49
 
43
- def get_latest_request
44
- @@requests.pop
50
+ def wait_for_next_request(timeout_seconds = 5, retry_interval = 0.5)
51
+ start = Time.now
52
+ current_requests_count = @@requests.length
53
+ while (@@requests.length == current_requests_count)
54
+ break if (Time.now - start).to_i >= timeout_seconds
55
+ sleep(retry_interval)
56
+ end
57
+ @@requests.last
45
58
  end
46
59
 
47
- def get_all_requests
48
- @@requests
60
+ def reset_server
61
+ Sinatra::Base.reset!
62
+ @@requests = []
63
+ @@responses = {}
49
64
  end
50
65
 
51
- def clear_requests
52
- @@requests = []
66
+ private
67
+
68
+ def set_response(opts, &block)
69
+ Sinatra::Base.send(opts.method, opts.path, &block)
53
70
  end
71
+
54
72
  end
55
73
 
56
74
  end
@@ -1,3 +1,3 @@
1
1
  module MockWebService
2
- VERSION = 0.7
2
+ VERSION = 0.8
3
3
  end
@@ -1,6 +1,9 @@
1
+ require 'sinatra'
2
+ require 'singleton'
3
+ require 'mock-web-service/response_options'
4
+ require 'mock-web-service/async'
1
5
  require 'mock-web-service/server'
2
6
  require 'mock-web-service/version'
3
- require 'sinatra'
4
7
 
5
8
  module MockWebService
6
9
 
@@ -0,0 +1,20 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'mock-web-service/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'mock-web-service'
7
+ s.version = MockWebService::VERSION
8
+ s.date = '2015-11-10'
9
+ s.description = 'Tool for mocking web requests'
10
+ s.authors = ['Mosaic']
11
+ s.email = 'mosaic.development@sherwin.com'
12
+ s.summary = 'Beta'
13
+ s.files = `git ls-files`.split($/)
14
+ s.require_paths = ['lib']
15
+ s.homepage = ''
16
+ s.license = 'MIT'
17
+ s.add_dependency 'sinatra'
18
+
19
+ s.add_development_dependency 'rspec', '>= 2.12.0'
20
+ end
@@ -0,0 +1,84 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Add response' do
4
+
5
+ let(:server){Server.new}
6
+ let(:default_uri) {URI('http://127.0.0.1:4567')}
7
+ let(:default_response) {'Default Response'}
8
+
9
+ before(:each) do
10
+ server.start({:port => 4567})
11
+ end
12
+
13
+ after(:each) do
14
+ Sinatra::Base.reset!
15
+ end
16
+
17
+ it 'should be able to get the response from default path' do
18
+ server.add(default_response)
19
+ res = Net::HTTP.get(default_uri)
20
+ expect(res).to eq(default_response)
21
+ end
22
+
23
+ it 'should be able to return different for same route' do
24
+ server.add('Response One')
25
+ server.add('Response Two')
26
+ res = Net::HTTP.get(default_uri)
27
+
28
+ expect(res).to eq('Response Two')
29
+ end
30
+
31
+ it 'should be able to post and get the response from default path' do
32
+ server.add(default_response,ResponseOptions.new { |opts| opts.method = :post })
33
+ res = Net::HTTP.post_form(default_uri, {})
34
+ expect(res.body).to eq(default_response)
35
+ end
36
+
37
+ it 'should be able to get the response from specific path' do
38
+ expected_path = '/expectedPath.html'
39
+ server.add(default_response)
40
+ server.add('Expected Response', ResponseOptions.new{|opts| opts.path = expected_path})
41
+
42
+ default_uri.path = expected_path
43
+ expected_response = Net::HTTP.get_response(default_uri)
44
+
45
+ expect(expected_response.body).to eq('Expected Response')
46
+ end
47
+
48
+ it 'should set response content type' do
49
+ server.add('')
50
+ res = Net::HTTP.get_response(default_uri)
51
+ expect(res['Content-Type']).to eq('text/html;charset=utf-8')
52
+ end
53
+
54
+ it 'should be able to override response content type' do
55
+ content_type = 'text/json;charset=utf-8'
56
+ server.add('{name: json}', ResponseOptions.new { |opts| opts.content_type = content_type })
57
+
58
+ res = Net::HTTP.get_response(default_uri)
59
+
60
+ expect(res['Content-Type']).to include(content_type)
61
+ end
62
+
63
+ describe 'with request method of post' do
64
+ it 'should receive a default HTTP Status of OK' do
65
+ server.add('Expected Response', ResponseOptions.new { |opts| opts.method = :post })
66
+
67
+ res = Net::HTTP.post_form(default_uri, {})
68
+
69
+ expect(res).to be_a(Net::HTTPOK)
70
+ end
71
+
72
+ it 'should be able to override status' do
73
+ opts = ResponseOptions.new do |opts|
74
+ opts.method = :post
75
+ opts.status = 500
76
+ end
77
+
78
+ server.add('Expected Response', opts)
79
+ res = Net::HTTP.post_form(default_uri, {})
80
+ expect(res).to be_a(Net::HTTPInternalServerError)
81
+ end
82
+ end
83
+
84
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Add soap response' do
4
+
5
+ let(:server){Server.new}
6
+ let(:default_uri) {URI('http://127.0.0.1:4567')}
7
+ let(:default_response) {'Default Response'}
8
+
9
+ before(:each) do
10
+ server.start({:port => 4567})
11
+ end
12
+
13
+ after(:each) do
14
+ Sinatra::Base.reset!
15
+ end
16
+
17
+ it 'should default to post method' do
18
+ server.add_soap_response(default_response)
19
+ res = Net::HTTP.post_form(default_uri, {})
20
+ expect(res.body).to eq(default_response)
21
+ end
22
+
23
+ it 'should default to xml content type' do
24
+ server.add_soap_response('')
25
+ res = Net::HTTP.post_form(default_uri, {})
26
+ expect(res['Content-Type']).to eq('text/xml;charset=utf-8')
27
+ end
28
+
29
+ end
@@ -0,0 +1,62 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Wait for next request' do
4
+
5
+ let(:server){Server.new}
6
+ let(:default_uri) {URI('http://127.0.0.1:4567')}
7
+ let(:default_response) {'Default Response'}
8
+
9
+ before(:each) do
10
+ server.start({:port => 4567})
11
+ end
12
+
13
+ after(:each) do
14
+ Sinatra::Base.reset!
15
+ server.clear_requests
16
+ end
17
+
18
+ it 'should return the first request' do
19
+ server.add(default_response)
20
+ thread = Thread.new do
21
+ Thread.current[:result]= server.wait_for_next_request
22
+ end
23
+ Net::HTTP.get(default_uri)
24
+ thread.join
25
+ expect(thread[:result].env['REQUEST_URI']).to include default_uri.to_s
26
+ end
27
+
28
+ it 'should wait for a new request' do
29
+ server.add(default_response)
30
+ server.add(default_response, ResponseOptions.new{|opts| opts.method= :post; opts.path= '/Test.html'})
31
+
32
+ Net::HTTP.get(default_uri)
33
+ thread = Thread.new do
34
+ Thread.current[:result]= server.wait_for_next_request
35
+ end
36
+ Net::HTTP.post_form(default_uri + '/Test.html', {:test => 'data'})
37
+ thread.join
38
+ expect(thread[:result].env['REQUEST_METHOD']).to eq "POST"
39
+ end
40
+
41
+ it 'should default timeout value to 5' do
42
+ t1 = Time.now
43
+
44
+ request = server.wait_for_next_request
45
+ t2 = Time.now
46
+
47
+ expect(request).to be_nil
48
+ time = (t2 - t1)
49
+ expect(time.to_i).to be 5
50
+ end
51
+
52
+ it 'should allow the timeout value to be set' do
53
+ t1 = Time.now
54
+
55
+ request = server.wait_for_next_request 2
56
+ t2 = Time.now
57
+
58
+ expect(request).to be_nil
59
+ expect((t2 - t1).to_i).to be 2
60
+ end
61
+
62
+ end
@@ -0,0 +1,8 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+
3
+ require 'net/http'
4
+ require 'rspec'
5
+ require 'singleton'
6
+ require 'mock_web_service'
7
+
8
+ include MockWebService
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mock-web-service
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.7'
4
+ version: '0.8'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mosaic
@@ -24,16 +24,41 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 2.12.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 2.12.0
27
41
  description: Tool for mocking web requests
28
42
  email: mosaic.development@sherwin.com
29
43
  executables: []
30
44
  extensions: []
31
45
  extra_rdoc_files: []
32
46
  files:
47
+ - ".gitignore"
48
+ - Gemfile
49
+ - Gemfile.lock
50
+ - README.md
51
+ - Rakefile
33
52
  - lib/mock-web-service/async.rb
53
+ - lib/mock-web-service/response_options.rb
34
54
  - lib/mock-web-service/server.rb
35
55
  - lib/mock-web-service/version.rb
36
56
  - lib/mock_web_service.rb
57
+ - mock-web-service.gemspec
58
+ - spec/mock-web-service/add_response_spec.rb
59
+ - spec/mock-web-service/add_soap_response_spec.rb
60
+ - spec/mock-web-service/server_spec.rb
61
+ - spec/spec_helper.rb
37
62
  homepage: ''
38
63
  licenses:
39
64
  - MIT