forked from chrisyoung/netrecorder
/
http.rb
51 lines (41 loc) · 1.42 KB
/
http.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Extend Net:HTTP to record requests and responses
module NetRecorder
module NetHTTP
def self.extended(base) #:nodoc:
base.class_eval do
alias :alias_for_request :request
@@fakes = fakes_cache || []
# request is overridden and the request and response are stored as a hash that can be written to a cache file
def request(req, body = nil, &block)
response = alias_for_request(req, body)
if NetRecorder.recording?
scope = NetRecorder.scope || 'global'
path = "http://#{req.bauth if req.bauth}#{req['host']}#{req.path}"
existing_fake = @@fakes.detect{|fake| fake[0] == path && fake[1][scope] && fake[1][scope][:method] == req.method}
existing_fake[1][scope][:response] << {:response => response} and return response if existing_fake
@@fakes << [path, {scope => {:method => req.method, :response => [{:response => response}]}}]
end
yield response if block
response
end
# returns the fakes hash
def self.fakes
@@fakes
end
def self.clear_netrecorder_cache! #:nodoc:
@@fakes = []
end
end
end
end
end
def fakes_cache #:nodoc:
fakes =
if File.exist?(NetRecorder.cache_file)
File.open(NetRecorder.cache_file, "r") do |f|
YAML.load(f.read)
end
end
return fakes if fakes.class == Hash
nil
end