fake_mechanize 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +5 -0
- data/LICENSE +20 -0
- data/README.rdoc +40 -0
- data/Rakefile +48 -0
- data/VERSION +1 -0
- data/lib/fake_mechanize/agent.rb +60 -0
- data/lib/fake_mechanize/error_request.rb +27 -0
- data/lib/fake_mechanize/request.rb +21 -0
- data/lib/fake_mechanize/responder.rb +25 -0
- data/lib/fake_mechanize.rb +6 -0
- data/test/fake_mechanize_test.rb +7 -0
- data/test/test_helper.rb +10 -0
- metadata +78 -0
data/.document
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Fabien Jakimowicz
|
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.
|
data/README.rdoc
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
= fake-mechanize
|
2
|
+
|
3
|
+
== DESCRIPTION
|
4
|
+
Description goes here.
|
5
|
+
|
6
|
+
== SYNOPSIS
|
7
|
+
=== Options
|
8
|
+
=== Types
|
9
|
+
=== Examples
|
10
|
+
|
11
|
+
== REQUIREMENTS
|
12
|
+
|
13
|
+
* mechanize gem
|
14
|
+
|
15
|
+
== INSTALL
|
16
|
+
gem install fake-mechanize
|
17
|
+
|
18
|
+
== LICENSE
|
19
|
+
|
20
|
+
(The MIT License)
|
21
|
+
|
22
|
+
Copyright (c) 2009, Fabien Jakimowicz <fabien@jakimowicz.com>
|
23
|
+
|
24
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
25
|
+
this software and associated documentation files (the "Software"), to deal in
|
26
|
+
the Software without restriction, including without limitation the rights to
|
27
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
28
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
29
|
+
so, subject to the following conditions:
|
30
|
+
|
31
|
+
The above copyright notice and this permission notice shall be included in all
|
32
|
+
copies or substantial portions of the Software.
|
33
|
+
|
34
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
35
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
36
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
37
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
38
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
39
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
40
|
+
SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "fake_mechanize"
|
8
|
+
gem.summary = %Q{toolset for offline unit tests on mechanize, like httpmock}
|
9
|
+
gem.email = "fabien@jakimowicz.com"
|
10
|
+
gem.homepage = "http://github.com/jakimowicz/fake_mechanize"
|
11
|
+
gem.authors = ["Fabien Jakimowicz"]
|
12
|
+
gem.rubyforge_project = "fake-mechanize"
|
13
|
+
gem.add_dependency 'mechanize'
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
|
17
|
+
Jeweler::RubyforgeTasks.new
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'rake/testtask'
|
23
|
+
Rake::TestTask.new(:test) do |test|
|
24
|
+
test.libs << 'lib' << 'test'
|
25
|
+
test.pattern = 'test/**/*_test.rb'
|
26
|
+
test.verbose = true
|
27
|
+
end
|
28
|
+
|
29
|
+
begin
|
30
|
+
require 'rcov/rcovtask'
|
31
|
+
Rcov::RcovTask.new do |test|
|
32
|
+
test.libs << 'test'
|
33
|
+
test.pattern = 'test/**/*_test.rb'
|
34
|
+
test.verbose = true
|
35
|
+
end
|
36
|
+
rescue LoadError
|
37
|
+
task :rcov do
|
38
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
desc "build rdoc using hanna theme"
|
46
|
+
task :rdoc do
|
47
|
+
`rm -rf rdoc && rdoc --op=rdoc --title=FakeMechanize --inline-source --format=darkfish LICENSE README* lib/**/*.rb`
|
48
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.0
|
@@ -0,0 +1,60 @@
|
|
1
|
+
module FakeMechanize
|
2
|
+
class Agent
|
3
|
+
attr_accessor :cookie_jar
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@cookie_jar = WWW::Mechanize::CookieJar.new
|
7
|
+
@responses = []
|
8
|
+
@errors = []
|
9
|
+
@history = []
|
10
|
+
end
|
11
|
+
|
12
|
+
def respond_to
|
13
|
+
reset_responses!
|
14
|
+
yield Responder.new(@responses, @errors)
|
15
|
+
@errors << ErrorRequest.new(:status => 404, :body => "not found")
|
16
|
+
end
|
17
|
+
|
18
|
+
def get(uri)
|
19
|
+
return_mechanize_response Request.new(:uri => uri)
|
20
|
+
end
|
21
|
+
|
22
|
+
def post(uri, args = {})
|
23
|
+
return_mechanize_response Request.new(:method => :post, :uri => uri, :request_headers => args)
|
24
|
+
end
|
25
|
+
|
26
|
+
def return_mechanize_response(given_request)
|
27
|
+
@history << given_request
|
28
|
+
request = search_for_request(given_request)
|
29
|
+
page = WWW::Mechanize::File.new(URI.parse(given_request.uri), request.response_headers, request.body, request.status)
|
30
|
+
raise WWW::Mechanize::ResponseCodeError.new(page) if request.status != 200
|
31
|
+
page
|
32
|
+
end
|
33
|
+
|
34
|
+
def search_for_request(given_request)
|
35
|
+
@responses.find {|request| request == given_request} || search_for_error_request(given_request)
|
36
|
+
end
|
37
|
+
|
38
|
+
def search_for_error_request(given_request)
|
39
|
+
@errors.max_by {|request| request.match(given_request)}
|
40
|
+
end
|
41
|
+
|
42
|
+
def assert_queried(method, uri, params = {})
|
43
|
+
request = Request.new(:method => method, :uri => uri, :request_headers => params)
|
44
|
+
@history.any? {|history_query| history_query == request}
|
45
|
+
end
|
46
|
+
|
47
|
+
[:head, :get, :post, :put, :delete].each do |method|
|
48
|
+
module_eval <<-EOE, __FILE__, __LINE__
|
49
|
+
def was_#{method}?(uri, params = {})
|
50
|
+
assert_queried(:#{method}, uri, params)
|
51
|
+
end
|
52
|
+
EOE
|
53
|
+
end
|
54
|
+
|
55
|
+
protected
|
56
|
+
def reset_responses!
|
57
|
+
@responses.clear
|
58
|
+
end
|
59
|
+
end # Agent
|
60
|
+
end # FakeMechanize
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module FakeMechanize
|
2
|
+
class ErrorRequest < Request
|
3
|
+
attr_reader :params_not_equal
|
4
|
+
|
5
|
+
def initialize(args = {})
|
6
|
+
super
|
7
|
+
@params_not_equal = args[:params_not_equal]
|
8
|
+
end
|
9
|
+
|
10
|
+
def match(alt)
|
11
|
+
count = 0
|
12
|
+
|
13
|
+
# Simple calculations
|
14
|
+
count += 1 if method == alt.method
|
15
|
+
count += 1 if uri == alt.uri
|
16
|
+
|
17
|
+
# More complicated: evaluates if params are equals or if they are different on purpose
|
18
|
+
if !request_headers.empty? and request_headers == alt.request_headers
|
19
|
+
count += 1
|
20
|
+
elsif method == alt.method and uri == alt.uri and request_headers != params_not_equal
|
21
|
+
count += 1
|
22
|
+
end
|
23
|
+
|
24
|
+
count
|
25
|
+
end
|
26
|
+
end # ErrorRequest
|
27
|
+
end # FakeMechanize
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module FakeMechanize
|
2
|
+
class Request
|
3
|
+
attr_reader :method, :uri, :request_headers, :body, :status, :response_headers
|
4
|
+
|
5
|
+
def initialize(args = {})
|
6
|
+
# Query
|
7
|
+
@method = args[:method] || :get
|
8
|
+
@uri = args[:uri]
|
9
|
+
@request_headers = args[:request_headers] || {}
|
10
|
+
|
11
|
+
# Answer
|
12
|
+
@body = args[:body]
|
13
|
+
@status = args[:status] || 200
|
14
|
+
@response_headers = args[:response_headers] || {}
|
15
|
+
end
|
16
|
+
|
17
|
+
def ==(alt)
|
18
|
+
method == alt.method and uri == alt.uri and request_headers == alt.request_headers
|
19
|
+
end
|
20
|
+
end # Request
|
21
|
+
end # FakeMechanize
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module FakeMechanize
|
2
|
+
class Responder
|
3
|
+
def initialize(responses, errors)
|
4
|
+
@responses, @errors = responses, errors
|
5
|
+
end
|
6
|
+
|
7
|
+
def error(args)
|
8
|
+
if args[:method]
|
9
|
+
@errors << ErrorRequest.new(args)
|
10
|
+
else
|
11
|
+
[:head, :get, :post, :put, :delete].each do |method|
|
12
|
+
@errors << ErrorRequest.new({:method => method}.merge(args))
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
[:head, :get, :post, :put, :delete].each do |method|
|
18
|
+
module_eval <<-EOE, __FILE__, __LINE__
|
19
|
+
def #{method}(args = {})
|
20
|
+
@responses << Request.new({:method => :#{method}}.merge(args))
|
21
|
+
end
|
22
|
+
EOE
|
23
|
+
end
|
24
|
+
end # Responder
|
25
|
+
end # FakeMechanize
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fake_mechanize
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Fabien Jakimowicz
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-07-22 00:00:00 +02:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: mechanize
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description:
|
26
|
+
email: fabien@jakimowicz.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- .document
|
36
|
+
- .gitignore
|
37
|
+
- LICENSE
|
38
|
+
- README.rdoc
|
39
|
+
- Rakefile
|
40
|
+
- VERSION
|
41
|
+
- lib/fake_mechanize.rb
|
42
|
+
- lib/fake_mechanize/agent.rb
|
43
|
+
- lib/fake_mechanize/error_request.rb
|
44
|
+
- lib/fake_mechanize/request.rb
|
45
|
+
- lib/fake_mechanize/responder.rb
|
46
|
+
- test/fake_mechanize_test.rb
|
47
|
+
- test/test_helper.rb
|
48
|
+
has_rdoc: true
|
49
|
+
homepage: http://github.com/jakimowicz/fake_mechanize
|
50
|
+
licenses: []
|
51
|
+
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options:
|
54
|
+
- --charset=UTF-8
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: "0"
|
62
|
+
version:
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: "0"
|
68
|
+
version:
|
69
|
+
requirements: []
|
70
|
+
|
71
|
+
rubyforge_project: fake-mechanize
|
72
|
+
rubygems_version: 1.3.4
|
73
|
+
signing_key:
|
74
|
+
specification_version: 3
|
75
|
+
summary: toolset for offline unit tests on mechanize, like httpmock
|
76
|
+
test_files:
|
77
|
+
- test/fake_mechanize_test.rb
|
78
|
+
- test/test_helper.rb
|