activeresource_httpmock_record 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1d1fd3f9a67765252be923b335fbda678a73fb22
4
+ data.tar.gz: 8d3240b88e3babe086dcacb336bfbf11b431ede2
5
+ SHA512:
6
+ metadata.gz: 7a779987f5998fa8f37068f4bd7686c4810fddc03795722d25161f07fb6c7898fd15e250d257177ac251834427ba17469dfa6f1f2201efac6afc2d6ec5b48729
7
+ data.tar.gz: 1d9dcb6ebe0c7c8b058e2cf786277e7f1fab28dd6fa9ff6aa5be0c81cc21271c07a82cc8281c203fdb3f497e9902b251185608b56029d1088dac28f697bb6d65
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in activeresource_httpmock_record.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Shintaro Kojima
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,99 @@
1
+ ActiveresourceHttpmockRecord
2
+ ============================
3
+
4
+ Testing models which communicate with remote web services could be painful.
5
+ This gem provides a simple way to record HTTP request and response through ActiveResource, and playback with ActiveResource::HttpMock.
6
+
7
+ Features
8
+ --------
9
+
10
+ If your application uses ActiveResource:
11
+
12
+ * ActiveresourceHttpmockRecord works together with your test code, records HTTP requests generated by an ActiveResource model and HTTP response coming from the remote web service.
13
+ * They're stored as YAML files to play back later
14
+ * During the same test code runs after that, ActiveresourceHttpmockRecord mocks HTTP requests and responses recorded istead of creating new actual HTTP sessions with ActiveResource.
15
+
16
+
17
+ Supported testing tool
18
+ ----------------------
19
+
20
+ * RSpec
21
+
22
+
23
+ Installation
24
+ ------------
25
+
26
+ Add this line to your application's Gemfile:
27
+
28
+ gem 'activeresource_httpmock_record'
29
+
30
+ And then execute:
31
+
32
+ $ bundle
33
+
34
+ Or install it yourself as:
35
+
36
+ $ gem install activeresource_httpmock_record
37
+
38
+
39
+ Usage
40
+ -----
41
+
42
+ ### RSpec
43
+
44
+ You may write your spec like:
45
+
46
+ ```ruby
47
+ describe User do
48
+ subject(:user) { User.find(1) }
49
+
50
+ it 'has a name' do
51
+ expect(user.name).to be_present
52
+ expect(user.name).to be_a(String)
53
+ end
54
+ end
55
+ ```
56
+
57
+ When running it, ```User.find(1)``` causes a HTTP request.
58
+
59
+ To activate ActiveresourceHttpmockRecord, add a metadata hash ```httpmock: <file_name>``` to groups or examples in your spec.
60
+
61
+ ```ruby
62
+ describe User do
63
+ subject(:user) { User.find(1) }
64
+
65
+ it 'has a name', httpmock: a_user do
66
+ expect(user.name).to be_present
67
+ expect(user.name).to be_a(String)
68
+ end
69
+ end
70
+ ```
71
+
72
+ Once you run the spec, ActiveresourceHttpmockRecord record HTTP request and response as a ```a_user.yml```, and if you run it again, ActiveresourceHttpmockRecord load the HTTP session from ```file_name.yml``` and mock it for you.
73
+
74
+
75
+ #### Warning
76
+
77
+ ActiveresourceHttpmockRecord doesn't support a multi-threaded testing like Capybara with ```js: true``` option.
78
+
79
+
80
+ Configuration
81
+ -------------
82
+
83
+ You can change the mock file store by adding the following lines (typically ```spec_helper.rb``` for RSpec):
84
+
85
+ ```ruby
86
+ ActiveResourceHttpMock.configure do |config|
87
+ config.httpmock_path = '/path/to/directory'
88
+ end
89
+ ```
90
+
91
+
92
+ Contributing
93
+ ------------
94
+
95
+ 1. Fork it
96
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
97
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
98
+ 4. Push to the branch (`git push origin my-new-feature`)
99
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'active_resource_http_mock/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'activeresource_httpmock_record'
8
+ spec.version = ActiveResourceHttpMock::VERSION
9
+ spec.authors = ['Shintaro Kojima']
10
+ spec.email = ['goodies@codeout.net']
11
+ spec.description = %q{Record HTTP request and response through ActiveResource, and play back with ActiveResource::HttpMock}
12
+ spec.summary = %q{ActiveResource::HttpMock extenstion for testing}
13
+ spec.homepage = 'https://github.com/codeout/activeresource_httpmock_record'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
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_runtime_dependency 'activeresource'
22
+ spec.add_runtime_dependency 'activesupport'
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.3'
25
+ spec.add_development_dependency 'rake'
26
+ end
@@ -0,0 +1,17 @@
1
+ require 'active_resource_http_mock/active_resource/http_mockable'
2
+ require 'active_resource_http_mock/active_resource/request_loggable'
3
+ require 'active_resource_http_mock/configuration'
4
+ require 'active_resource_http_mock/rspec' if defined?(RSpec)
5
+
6
+ module ActiveResourceHttpMock
7
+ class << self
8
+ def configuration
9
+ @configuration ||= ActiveResourceHttpMock::Configuration.new
10
+ end
11
+ alias config configuration
12
+
13
+ def configure(&block)
14
+ yield configuration if block_given?
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,32 @@
1
+ require 'active_resource/connection'
2
+ require 'active_resource/http_mock'
3
+
4
+ module ActiveResourceHttpMock
5
+ module ActiveResource
6
+ module HttpMockable
7
+ def self.included(base)
8
+ base.class_eval do
9
+ private
10
+
11
+ def http
12
+ if Thread.current[:httpmock]
13
+ _http_with_mock_
14
+ else
15
+ _http_without_mock_
16
+ end
17
+ end
18
+
19
+ def _http_with_mock_
20
+ @http ||= ::ActiveResource::HttpMock.new(@site)
21
+ end
22
+
23
+ def _http_without_mock_
24
+ configure_http(new_http)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ ActiveResource::Connection.send :include, ActiveResourceHttpMock::ActiveResource::HttpMockable
@@ -0,0 +1,31 @@
1
+ require 'active_resource/connection'
2
+ require 'active_support/notifications'
3
+
4
+ module ActiveResourceHttpMock
5
+ module ActiveResource
6
+ module RequestLoggable
7
+ def self.included(base)
8
+ base.class_eval do
9
+ private
10
+
11
+ # OPTIMIZE: It looks hard to swap Connection object with modified one as ActiveResource::* works together monolithically
12
+ def request(method, path, *arguments)
13
+ result = ActiveSupport::Notifications.instrument("request.active_resource") do |payload|
14
+ payload[:method] = method
15
+ payload[:request_uri] = "#{site.scheme}://#{site.host}:#{site.port}#{path}"
16
+ payload[:request] = arguments # monkey patch for request logging
17
+ payload[:result] = http.send(method, path, *arguments)
18
+ end
19
+ handle_response(result)
20
+ rescue Timeout::Error => e
21
+ raise TimeoutError.new(e.message)
22
+ rescue OpenSSL::SSL::SSLError => e
23
+ raise SSLError.new(e.message)
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ ActiveResource::Connection.send :include, ActiveResourceHttpMock::ActiveResource::RequestLoggable
@@ -0,0 +1,19 @@
1
+ module ActiveResourceHttpMock
2
+ class Configuration
3
+ attr_accessor :httpmock_path, :url_encoder, :url_decoder
4
+
5
+ def initialize
6
+ self.httpmock_path = 'spec/fixtures/httpmock'
7
+ self.url_encoder = ->(url) { url }
8
+ self.url_decoder = ->(url) { url }
9
+ end
10
+
11
+ def encode_url(&block)
12
+ @url_encoder = block if block_given?
13
+ end
14
+
15
+ def decode_url(&block)
16
+ @url_decoder = block if block_given?
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,59 @@
1
+ require 'active_resource/http_mock'
2
+ require 'uri'
3
+ require 'yaml'
4
+
5
+ module ActiveResourceHttpMock
6
+ class DataFile
7
+ def initialize(context)
8
+ @context = context
9
+ end
10
+
11
+ def data_dir
12
+ if defined?(Rails)
13
+ File.join(Rails.root, ActiveResourceHttpMock.config.httpmock_path)
14
+ else
15
+ ActiveResourceHttpMock.config.httpmock_path
16
+ end
17
+ end
18
+
19
+ def load_mocks
20
+ return unless data_file_exists?
21
+
22
+ requests = YAML.load_file(data_file_path).map {|request|
23
+ url = ActiveResourceHttpMock.config.url_decoder.call(request[1])
24
+ request[1] = URI.parse(url).request_uri
25
+ request
26
+ }
27
+
28
+ ::ActiveResource::HttpMock.respond_to(false) {|mock|
29
+ requests.each {|request| mock.send *request }
30
+ }
31
+ end
32
+
33
+ def dump_mocks(responses)
34
+ yaml = YAML.dump(
35
+ responses.map {|response|
36
+ response[1] = ActiveResourceHttpMock.config.url_encoder.call(response[1])
37
+ response
38
+ }
39
+ )
40
+
41
+ File.write data_file_path, yaml
42
+ end
43
+
44
+ private
45
+
46
+ def data_file_exists?
47
+ File.exists?(data_file_path)
48
+ end
49
+
50
+ def data_file_path
51
+ ensure_data_dir!
52
+ File.join(data_dir, @context.to_s) + '.yml'
53
+ end
54
+
55
+ def ensure_data_dir!
56
+ Dir.mkdir(data_dir) unless Dir.exist?(data_dir)
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,29 @@
1
+ require 'active_support/log_subscriber'
2
+
3
+ module ActiveResourceHttpMock
4
+ class LogSubscriber < ActiveSupport::LogSubscriber
5
+ class_attribute :events
6
+ self.events = {}
7
+
8
+ def self.responses
9
+ events.values
10
+ end
11
+
12
+ def request(event)
13
+ req = compose_event(event)
14
+ events[req[0..1].join('%')] = req
15
+ end
16
+
17
+ private
18
+
19
+ def compose_event(event)
20
+ [
21
+ event.payload[:method],
22
+ event.payload[:request_uri],
23
+ event.payload[:request].last,
24
+ event.payload[:result].body,
25
+ event.payload[:result].code
26
+ ]
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,50 @@
1
+ require 'active_support/notifications'
2
+ require 'rspec'
3
+ require 'active_resource_http_mock/data_file'
4
+ require 'active_resource_http_mock/log_subscriber'
5
+
6
+ module ActiveResourceHttpMock
7
+ module RSpec
8
+ class << self
9
+ def configure
10
+ ::RSpec.configure do |config|
11
+ config.include self
12
+
13
+ config.around :each do |example|
14
+ if (context = example.metadata[:httpmock]) && example.metadata[:js].blank?
15
+ data = ActiveResourceHttpMock::DataFile.new(context)
16
+
17
+ if data.load_mocks
18
+ enable_http_mock
19
+ begin
20
+ example.run
21
+ ensure
22
+ disable_http_mock
23
+ end
24
+ else
25
+ ActiveResourceHttpMock::LogSubscriber.events.clear
26
+ example.run
27
+ data.dump_mocks ActiveResourceHttpMock::LogSubscriber.responses unless @example.exception
28
+ end
29
+ else
30
+ example.run
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def enable_http_mock
40
+ Thread.current[:httpmock] = true
41
+ end
42
+
43
+ def disable_http_mock
44
+ Thread.current[:httpmock] = nil
45
+ end
46
+ end
47
+ end
48
+
49
+ ActiveResourceHttpMock::RSpec.configure
50
+ ActiveResourceHttpMock::LogSubscriber.attach_to :active_resource
@@ -0,0 +1,3 @@
1
+ module ActiveResourceHttpMock
2
+ VERSION = '0.0.2'
3
+ end
@@ -0,0 +1 @@
1
+ require 'active_resource_http_mock'
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activeresource_httpmock_record
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Shintaro Kojima
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activeresource
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: activesupport
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.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
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
+ description: Record HTTP request and response through ActiveResource, and play back
70
+ with ActiveResource::HttpMock
71
+ email:
72
+ - goodies@codeout.net
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - .gitignore
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - activeresource_httpmock_record.gemspec
83
+ - lib/active_resource_http_mock.rb
84
+ - lib/active_resource_http_mock/active_resource/http_mockable.rb
85
+ - lib/active_resource_http_mock/active_resource/request_loggable.rb
86
+ - lib/active_resource_http_mock/configuration.rb
87
+ - lib/active_resource_http_mock/data_file.rb
88
+ - lib/active_resource_http_mock/log_subscriber.rb
89
+ - lib/active_resource_http_mock/rspec.rb
90
+ - lib/active_resource_http_mock/version.rb
91
+ - lib/activeresource_httpmock_record.rb
92
+ homepage: https://github.com/codeout/activeresource_httpmock_record
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.0.3
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: ActiveResource::HttpMock extenstion for testing
116
+ test_files: []