autowebreplay 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.
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Brett Gibson
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,18 @@
1
+ = autowebreplay
2
+ Automatic replay of HTTP requests to keep tests fast and not externally dependent with the goal of keeping the API to just one require statement.
3
+
4
+ == Usage
5
+ Add
6
+ require 'autowebreplay'
7
+ and all responses get cached to disk. Once saved all subsequent identical requests will use the version on disk.
8
+
9
+ A cache directory (.autowebreplay) gets created in the current working directory. Delete the cache directory to clear the cache.
10
+
11
+ It's essentially a thin wrapper on webmock[https://github.com/bblimke/webmock] so it supports all the same HTTP libraries.
12
+
13
+ ---
14
+
15
+ If you're looking for more customized HTTP request playback vcr[https://github.com/myronmarston/vcr] looks worth a try.
16
+
17
+ == Thanks
18
+ webmock[https://github.com/bblimke/webmock]!!!
@@ -0,0 +1,11 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rake/testtask'
5
+ Rake::TestTask.new(:test) do |test|
6
+ test.libs << 'lib' << 'test'
7
+ test.pattern = 'test/**/*_test.rb'
8
+ test.verbose = true
9
+ end
10
+
11
+ task :default => :test
@@ -0,0 +1,60 @@
1
+ require 'webmock'
2
+
3
+ module Autowebreplay
4
+ FILE_DELIM = '+'
5
+ class << self
6
+ # a fancier individual than myself might make this configurable
7
+ # for now a monkey patch would do the trick
8
+ def cache_dir
9
+ ".#{to_s.downcase}"
10
+ end
11
+
12
+ def save(request_signature, response)
13
+ create_cache_dir!
14
+
15
+ File.open(filename(request_signature), 'w') do |f|
16
+ f << Marshal.dump(response)
17
+ end
18
+ end
19
+
20
+ def lookup(request_signature)
21
+ if File.exists?(f = filename(request_signature))
22
+ Marshal.load(File.read(f))
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def filename(request_signature)
29
+ filename = [request_signature.uri.host,
30
+ request_signature.to_s.hash.to_s].join(Autowebreplay::FILE_DELIM)
31
+ File.join(cache_dir, filename)
32
+ end
33
+
34
+ def create_cache_dir!
35
+ unless File.exists?(cache_dir)
36
+ Dir.mkdir(cache_dir)
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ # need this to populate cache in first place
43
+ WebMock.allow_net_connect!
44
+
45
+ WebMock.after_request(:real_requests_only => true) do |request_signature, response|
46
+ Autowebreplay.save(request_signature, response)
47
+ end
48
+
49
+ lookup_response = lambda do |request_signature|
50
+ if resp = Autowebreplay.lookup(request_signature)
51
+ [:headers, :body, :status, :exception].inject({}) do |h, prop|
52
+ h[prop] = resp.send(prop)
53
+ h
54
+ end
55
+ end
56
+ end
57
+
58
+ WebMock::StubRegistry.instance.register_request_stub(WebMock::RequestStub.new(:any, /.*/)).
59
+ with(&lookup_response).
60
+ to_return(&lookup_response)
@@ -0,0 +1,9 @@
1
+ module Autowebreplay
2
+ module Version
3
+ MAJOR = 0
4
+ MINOR = 1
5
+ PATCH = 0
6
+
7
+ STRING = [MAJOR, MINOR, PATCH].compact.join('.')
8
+ end
9
+ end
@@ -0,0 +1,65 @@
1
+ require 'test/unit'
2
+ require 'fileutils'
3
+ require 'net/http'
4
+
5
+ require 'autowebreplay'
6
+ require 'webmock'
7
+
8
+ class AutowebreplayTest < Test::Unit::TestCase
9
+ def setup
10
+ proj_root = File.join(File.dirname(__FILE__), '..')
11
+ full_cache_dir = File.join(proj_root, Autowebreplay.cache_dir)
12
+ FileUtils.rm_rf(full_cache_dir)
13
+
14
+ @request_signature = WebMock::RequestSignature.new('GET', 'http://www.google.com/')
15
+ @response = WebMock::Response.new(:body => 'some specific body')
16
+ end
17
+
18
+ def test_should_have_cache_dir
19
+ assert_equal '.autowebreplay', Autowebreplay.cache_dir
20
+ end
21
+
22
+ def test_should_create_cache_dir_as_needed
23
+ Autowebreplay.save(@request_signature, @response)
24
+
25
+ assert File.directory?(Autowebreplay.cache_dir),
26
+ 'cache dir should exist and be directory'
27
+ end
28
+
29
+ def test_should_create_response_file_using_domain_and_hashed_signature_for_file_name
30
+ Autowebreplay.save(@request_signature, @response)
31
+
32
+ files = Dir[File.join(Autowebreplay.cache_dir, '*')]
33
+ assert_equal 1, files.size
34
+
35
+ file = files.first
36
+ domain, hash = File.basename(file).split(Autowebreplay::FILE_DELIM)
37
+ assert_equal 'www.google.com', domain
38
+ assert_equal @request_signature.to_s.hash.to_s, hash
39
+ end
40
+
41
+ def test_should_be_able_to_look_up_saved_response
42
+ Autowebreplay.save(@request_signature, @response)
43
+
44
+ new_request_sig = WebMock::RequestSignature.new('GET', 'http://www.yahoo.com/')
45
+ assert_nil Autowebreplay.lookup(new_request_sig)
46
+
47
+ assert_equal @response, Autowebreplay.lookup(@request_signature)
48
+ end
49
+
50
+ def test_should_return_file_contents_on_subsequent_requests
51
+ the_req = lambda {Net::HTTP.get_response('www.google.com', '/')}
52
+ resp = the_req.call
53
+
54
+ WebMock.disable_net_connect! # requests going to net will raise error
55
+
56
+ saved_resp = the_req.call
57
+
58
+ assert_equal resp.body, saved_resp.body
59
+
60
+ # set-cookie gets normalized and the order might change so ignore it here
61
+ headers, saved_headers = [resp.to_hash, saved_resp.to_hash]
62
+ [headers, saved_headers].each {|h| h.delete('set-cookie')}
63
+ assert_equal headers, saved_headers
64
+ end
65
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: autowebreplay
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Brett Gibson
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-03-03 00:00:00 -08:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: webmock
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 13
30
+ segments:
31
+ - 1
32
+ - 6
33
+ - 1
34
+ version: 1.6.1
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: bundler
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 0
48
+ version: "0"
49
+ type: :development
50
+ version_requirements: *id002
51
+ description: automatic playback of web requests for testing
52
+ email:
53
+ - brettdgibson@gmail.com
54
+ executables: []
55
+
56
+ extensions: []
57
+
58
+ extra_rdoc_files: []
59
+
60
+ files:
61
+ - LICENSE.txt
62
+ - README.rdoc
63
+ - Rakefile
64
+ - lib/autowebreplay/version.rb
65
+ - lib/autowebreplay.rb
66
+ - test/autowebreplay_test.rb
67
+ has_rdoc: true
68
+ homepage: ""
69
+ licenses:
70
+ - MIT
71
+ post_install_message:
72
+ rdoc_options: []
73
+
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ hash: 3
82
+ segments:
83
+ - 0
84
+ version: "0"
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ hash: 3
91
+ segments:
92
+ - 0
93
+ version: "0"
94
+ requirements: []
95
+
96
+ rubyforge_project:
97
+ rubygems_version: 1.5.2
98
+ signing_key:
99
+ specification_version: 3
100
+ summary: automatic playback of web requests for testing
101
+ test_files:
102
+ - test/autowebreplay_test.rb