artifice-passthru 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +5 -0
- data/lib/artifice-passthru.rb +2 -95
- data/lib/artifice-passthru/core.rb +97 -0
- data/lib/artifice-passthru/version.rb +10 -0
- data/lib/artifice/passthru/core.rb +2 -0
- metadata +7 -4
data/README.md
CHANGED
data/lib/artifice-passthru.rb
CHANGED
@@ -1,98 +1,5 @@
|
|
1
|
-
require 'artifice'
|
2
|
-
|
3
|
-
module Artifice # :nodoc:
|
4
|
-
|
5
|
-
# Artifice.passthru! returns a Rack response created by making a *real* Net::HTTP
|
6
|
-
# request (using the Artifice::Passthru.last_request_info) and then turning
|
7
|
-
# the resulting Net::HTTPResponse object into a Rack response (which Artifice expects).
|
8
|
-
def self.passthru!
|
9
|
-
Artifice::Passthru.make_real_request_and_return_response!
|
10
|
-
end
|
11
|
-
|
12
|
-
# Given a constant (class or module), this gives it access to the *real* Net::HTTP so
|
13
|
-
# every request made from within this class/module will use the real Net::HTTP
|
14
|
-
def self.use_real_net_http class_or_module
|
15
|
-
Artifice::Passthru.setup_to_use_real_net_http class_or_module
|
16
|
-
end
|
17
|
-
|
18
|
-
# Artifice Passthru
|
19
|
-
module Passthru
|
20
|
-
|
21
|
-
# Simple class for storing information about the last #request that was made,
|
22
|
-
# allowing us to recreate the request
|
23
|
-
class RequestInfo
|
24
|
-
attr_accessor :address, :port, :request, :body, :block
|
25
|
-
|
26
|
-
def initialize address, port, req, body, block
|
27
|
-
self.address = address
|
28
|
-
self.port = port
|
29
|
-
self.request = req
|
30
|
-
self.body = body
|
31
|
-
self.block = block
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
# When Artifice::Passthru is included into Artifice::Net::HTTP, it uses "alias_method_chain"
|
36
|
-
# to override the #request method so we can get the arguments that were passed.
|
37
|
-
def self.included base
|
38
|
-
base.class_eval do
|
39
|
-
alias_method :request_without_passthru_argument_tracking, :request
|
40
|
-
alias_method :request, :request_with_passthru_argument_tracking
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
# Returns the last information that were passed to Net::HTTP#request (which Artifice hijacked)
|
45
|
-
# so we can use these arguments to fire off a real request, if necessary.
|
46
|
-
def self.last_request_info
|
47
|
-
Thread.current[:artifice_passthru_arguments]
|
48
|
-
end
|
49
|
-
|
50
|
-
# Accepts and stores the last information that were passed to Net::HTTP#request (which Artifice hijacked)
|
51
|
-
# so we can use these arguments to fire off a real request, if necessary.
|
52
|
-
def self.last_request_info= request_info
|
53
|
-
Thread.current[:artifice_passthru_arguments] = request_info
|
54
|
-
end
|
55
|
-
|
56
|
-
# Makes a real Net::HTTP request and returns the response, converted to a Rack response
|
57
|
-
def self.make_real_request_and_return_response!
|
58
|
-
raise 'Artifice.passthru! was called but no previous Artifice request was found to make via real Net::HTTP' if last_request_info.nil?
|
59
|
-
to_rack_response make_real_request(last_request_info)
|
60
|
-
end
|
61
|
-
|
62
|
-
# Given a Net::HTTPResponse, returns a Rack response
|
63
|
-
def self.to_rack_response net_http_response
|
64
|
-
# There's some voodoo magic going on that makes the real Net::HTTP#request method populate our response body for us?
|
65
|
-
headers = net_http_response.instance_variable_get('@header').inject({}){|all,this| all[this.first] = this.last.join("\n"); all }
|
66
|
-
[ net_http_response.code, headers, [net_http_response.body] ]
|
67
|
-
end
|
68
|
-
|
69
|
-
# Given the last_request_info (that would normally be passed to Net::HTTP#request),
|
70
|
-
# makes a real request and returns the Net::HTTPResponse
|
71
|
-
def self.make_real_request request_info
|
72
|
-
http = Artifice::NET_HTTP.new request_info.address, request_info.port
|
73
|
-
http.request request_info.request, request_info.body, &request_info.block
|
74
|
-
end
|
75
|
-
|
76
|
-
# Given a constant (class or module), this gives it access to the *real* Net::HTTP so
|
77
|
-
# every request made from within this class/module will use the real Net::HTTP
|
78
|
-
#
|
79
|
-
# Taken from: http://matschaffer.com/2011/04/net-http-mock-cucumber/
|
80
|
-
def self.setup_to_use_real_net_http class_or_module
|
81
|
-
class_or_module.class_eval %{
|
82
|
-
Net = ::Net.dup
|
83
|
-
module Net
|
84
|
-
HTTP = Artifice::NET_HTTP
|
85
|
-
end
|
86
|
-
}
|
87
|
-
end
|
88
|
-
|
89
|
-
# Simply stores the arguments that are passed and then calls "super" (request_without_passthru_argument_tracking)
|
90
|
-
def request_with_passthru_argument_tracking req, body = nil, &block
|
91
|
-
Artifice::Passthru.last_request_info = RequestInfo.new address, port, req, body, block
|
92
|
-
request_without_passthru_argument_tracking req, body, &block
|
93
|
-
end
|
94
|
-
end
|
95
|
-
end
|
1
|
+
require 'artifice-passthru/core'
|
96
2
|
|
97
3
|
# Inject our #request method override into Artifice's implementation of Net::HTTP
|
4
|
+
require 'artifice' unless defined? Artifice::Net::HTTP
|
98
5
|
Artifice::Net::HTTP.send :include, Artifice::Passthru
|
@@ -0,0 +1,97 @@
|
|
1
|
+
require 'artifice-passthru/version'
|
2
|
+
|
3
|
+
module Artifice
|
4
|
+
|
5
|
+
# Artifice.passthru! returns a Rack response created by making a *real* Net::HTTP
|
6
|
+
# request (using the Artifice::Passthru.last_request_info) and then turning
|
7
|
+
# the resulting Net::HTTPResponse object into a Rack response (which Artifice expects).
|
8
|
+
def self.passthru!
|
9
|
+
Artifice::Passthru.make_real_request_and_return_response!
|
10
|
+
end
|
11
|
+
|
12
|
+
# Given a constant (class or module), this gives it access to the *real* Net::HTTP so
|
13
|
+
# every request made from within this class/module will use the real Net::HTTP
|
14
|
+
def self.use_real_net_http class_or_module
|
15
|
+
Artifice::Passthru.setup_to_use_real_net_http class_or_module
|
16
|
+
end
|
17
|
+
|
18
|
+
# Artifice Passthru
|
19
|
+
module Passthru
|
20
|
+
|
21
|
+
# Simple class for storing information about the last #request that was made,
|
22
|
+
# allowing us to recreate the request
|
23
|
+
class RequestInfo
|
24
|
+
attr_accessor :address, :port, :request, :body, :block
|
25
|
+
|
26
|
+
def initialize address, port, req, body, block
|
27
|
+
self.address = address
|
28
|
+
self.port = port
|
29
|
+
self.request = req
|
30
|
+
self.body = body
|
31
|
+
self.block = block
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# When Artifice::Passthru is included into Artifice::Net::HTTP, it uses "alias_method_chain"
|
36
|
+
# to override the #request method so we can get the arguments that were passed.
|
37
|
+
def self.included base
|
38
|
+
unless base.instance_methods.map(&:to_s).include?('request_without_passthru_argument_tracking')
|
39
|
+
base.class_eval do
|
40
|
+
alias_method :request_without_passthru_argument_tracking, :request
|
41
|
+
alias_method :request, :request_with_passthru_argument_tracking
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# Returns the last information that were passed to Net::HTTP#request (which Artifice hijacked)
|
47
|
+
# so we can use these arguments to fire off a real request, if necessary.
|
48
|
+
def self.last_request_info
|
49
|
+
Thread.current[:artifice_passthru_arguments]
|
50
|
+
end
|
51
|
+
|
52
|
+
# Accepts and stores the last information that were passed to Net::HTTP#request (which Artifice hijacked)
|
53
|
+
# so we can use these arguments to fire off a real request, if necessary.
|
54
|
+
def self.last_request_info= request_info
|
55
|
+
Thread.current[:artifice_passthru_arguments] = request_info
|
56
|
+
end
|
57
|
+
|
58
|
+
# Makes a real Net::HTTP request and returns the response, converted to a Rack response
|
59
|
+
def self.make_real_request_and_return_response!
|
60
|
+
raise 'Artifice.passthru! was called but no previous Artifice request was found to make via real Net::HTTP' if last_request_info.nil?
|
61
|
+
to_rack_response make_real_request(last_request_info)
|
62
|
+
end
|
63
|
+
|
64
|
+
# Given a Net::HTTPResponse, returns a Rack response
|
65
|
+
def self.to_rack_response net_http_response
|
66
|
+
# There's some voodoo magic going on that makes the real Net::HTTP#request method populate our response body for us?
|
67
|
+
headers = net_http_response.instance_variable_get('@header').inject({}){|all,this| all[this.first] = this.last.join("\n"); all }
|
68
|
+
[ net_http_response.code, headers, [net_http_response.body] ]
|
69
|
+
end
|
70
|
+
|
71
|
+
# Given the last_request_info (that would normally be passed to Net::HTTP#request),
|
72
|
+
# makes a real request and returns the Net::HTTPResponse
|
73
|
+
def self.make_real_request request_info
|
74
|
+
http = Artifice::NET_HTTP.new request_info.address, request_info.port
|
75
|
+
http.request request_info.request, request_info.body, &request_info.block
|
76
|
+
end
|
77
|
+
|
78
|
+
# Given a constant (class or module), this gives it access to the *real* Net::HTTP so
|
79
|
+
# every request made from within this class/module will use the real Net::HTTP
|
80
|
+
#
|
81
|
+
# Taken from: http://matschaffer.com/2011/04/net-http-mock-cucumber/
|
82
|
+
def self.setup_to_use_real_net_http class_or_module
|
83
|
+
class_or_module.class_eval %{
|
84
|
+
Net = ::Net.dup
|
85
|
+
module Net
|
86
|
+
HTTP = Artifice::NET_HTTP
|
87
|
+
end
|
88
|
+
}
|
89
|
+
end
|
90
|
+
|
91
|
+
# Simply stores the arguments that are passed and then calls "super" (request_without_passthru_argument_tracking)
|
92
|
+
def request_with_passthru_argument_tracking req, body = nil, &block
|
93
|
+
Artifice::Passthru.last_request_info = RequestInfo.new address, port, req, body, block
|
94
|
+
request_without_passthru_argument_tracking req, body, &block
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: artifice-passthru
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- remi
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-08-19 00:00:00 -07:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -42,7 +42,10 @@ extra_rdoc_files: []
|
|
42
42
|
|
43
43
|
files:
|
44
44
|
- README.md
|
45
|
+
- lib/artifice/passthru/core.rb
|
45
46
|
- lib/artifice/passthru.rb
|
47
|
+
- lib/artifice-passthru/core.rb
|
48
|
+
- lib/artifice-passthru/version.rb
|
46
49
|
- lib/artifice-passthru.rb
|
47
50
|
has_rdoc: true
|
48
51
|
homepage: https://github.com/remi/artifice-passthru
|