pauldix-typhoeus 0.0.8 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,19 +6,64 @@ module Typhoeus
6
6
  end
7
7
 
8
8
  module ClassMethods
9
+ def mock(method, args = {})
10
+ @remote_mocks ||= {}
11
+ @remote_mocks[method] ||= {}
12
+ args[:code] ||= 200
13
+ args[:body] ||= ""
14
+ args[:headers] ||= ""
15
+ args[:time] ||= 0
16
+ url = args.delete(:url)
17
+ url ||= :catch_all
18
+ @remote_mocks[method][url] = args
19
+ end
20
+
21
+ def get_mock(method, url, options)
22
+ return nil unless @remote_mocks
23
+ if @remote_mocks.has_key? method
24
+ if @remote_mocks[method].has_key? url
25
+ get_mock_and_run_handlers(@remote_mocks[method][url], options)
26
+ elsif @remote_mocks[method].has_key? :catch_all
27
+ get_mock_and_run_handlers(@remote_mocks[method][:catch_all], options)
28
+ else
29
+ nil
30
+ end
31
+ else
32
+ nil
33
+ end
34
+ end
35
+
36
+ def get_mock_and_run_handlers(response_args, options)
37
+ response = Response.new(response_args[:code], response_args[:headers], response_args[:body], response_args[:time])
38
+ if response.code >= 200 && response.code < 300 && options.has_key?(:on_success)
39
+ response = options[:on_success].call(response)
40
+ elsif options.has_key?(:on_failure)
41
+ response = options[:on_failure].call(response)
42
+ end
43
+ response
44
+ end
45
+
9
46
  def get(url, options = {})
47
+ mock_object = get_mock(:get, url, options)
48
+ return mock_object if mock_object
10
49
  remote_proxy_object(url, :get, options)
11
50
  end
12
51
 
13
52
  def post(url, options = {}, &block)
53
+ mock_object = get_mock(:post, url, options)
54
+ return mock_object if mock_object
14
55
  remote_proxy_object(url, :post, options)
15
56
  end
16
57
 
17
58
  def put(url, options = {}, &block)
59
+ mock_object = get_mock(:put, url, options)
60
+ return mock_object if mock_object
18
61
  remote_proxy_object(url, :put, options)
19
62
  end
20
63
 
21
64
  def delete(url, options = {}, &block)
65
+ mock_object = get_mock(:delete, url, options)
66
+ return mock_object if mock_object
22
67
  remote_proxy_object(url, :delete, options)
23
68
  end
24
69
 
@@ -16,7 +16,7 @@ module Typhoeus
16
16
  def method_missing(sym, *args, &block)
17
17
  unless @proxied_object
18
18
  if @cache && @cache_key
19
- @proxied_object = @cache.get(@cache_key)
19
+ @proxied_object = @cache.get(@cache_key) rescue nil
20
20
  end
21
21
 
22
22
  unless @proxied_object
data/lib/typhoeus.rb CHANGED
@@ -12,7 +12,7 @@ require 'typhoeus/remote_proxy_object'
12
12
  require 'typhoeus/response'
13
13
 
14
14
  module Typhoeus
15
- VERSION = "0.0.8"
15
+ VERSION = "0.0.10"
16
16
 
17
17
  def self.easy_object_pool
18
18
  @easy_objects ||= []
@@ -14,6 +14,111 @@ describe Typhoeus do
14
14
  after(:all) do
15
15
  stop_method_server(@pid)
16
16
  end
17
+
18
+ describe "mocking" do
19
+ it "should mock out GET" do
20
+ @klass.mock(:get)
21
+ response = @klass.get("http://mock_url")
22
+ response.code.should == 200
23
+ response.body.should == ""
24
+ response.headers.should == ""
25
+ response.time.should == 0
26
+ end
27
+
28
+ it "should mock out PUT" do
29
+ @klass.mock(:put)
30
+ response = @klass.put("http://mock_url")
31
+ response.code.should == 200
32
+ response.body.should == ""
33
+ response.headers.should == ""
34
+ response.time.should == 0
35
+ end
36
+
37
+ it "should mock out POST" do
38
+ @klass.mock(:post)
39
+ response = @klass.post("http://mock_url")
40
+ response.code.should == 200
41
+ response.body.should == ""
42
+ response.headers.should == ""
43
+ response.time.should == 0
44
+ end
45
+
46
+ it "should mock out DELETE" do
47
+ @klass.mock(:delete)
48
+ response = @klass.delete("http://mock_url")
49
+ response.code.should == 200
50
+ response.body.should == ""
51
+ response.headers.should == ""
52
+ response.time.should == 0
53
+ end
54
+
55
+ it "should take an optional url" do
56
+ @klass.mock(:get, :url => "http://stuff")
57
+ response = @klass.get("http://stuff")
58
+ response.code.should == 200
59
+ response.body.should == ""
60
+ response.headers.should == ""
61
+ response.time.should == 0
62
+
63
+ @klass.get("http://localhost:234234234").code.should == 0
64
+ end
65
+
66
+ describe "remote methods" do
67
+ it "should work for defined remote methods" do
68
+ @klass.instance_eval do
69
+ define_remote_method :do_stuff, :base_uri => "http://localhost:3001", :on_success => lambda {|r| r.body.should == "hi"; :great_success}
70
+ end
71
+ @klass.mock(:get, :url => "http://localhost:3001", :body => "hi")
72
+ @klass.do_stuff.should == :great_success
73
+ end
74
+
75
+ it "should call the on failure handler for remote methods" do
76
+ @klass.instance_eval do
77
+ define_remote_method :do_stuff, :base_uri => "http://localhost:3001", :on_failure => lambda {|r| r.body.should == "hi"; :fail}
78
+ end
79
+ @klass.mock(:get, :url => "http://localhost:3001", :body => "hi", :code => 500)
80
+ @klass.do_stuff.should == :fail
81
+ end
82
+ end
83
+
84
+ describe "response hash" do
85
+ it "should use provided code" do
86
+ @klass.mock(:get, :url => "http://localhost/whatever", :code => 301)
87
+ response = @klass.get("http://localhost/whatever")
88
+ response.code.should == 301
89
+ response.body.should == ""
90
+ response.headers.should == ""
91
+ response.time.should == 0
92
+ end
93
+
94
+ it "should use provided body" do
95
+ @klass.mock(:get, :url => "http://localhost/whatever", :body => "hey paul")
96
+ response = @klass.get("http://localhost/whatever")
97
+ response.code.should == 200
98
+ response.body.should == "hey paul"
99
+ response.headers.should == ""
100
+ response.time.should == 0
101
+ end
102
+
103
+ it "should use provided headers" do
104
+ @klass.mock(:get, :url => "http://localhost/whatever", :headers => "whooo, headers!")
105
+ response = @klass.get("http://localhost/whatever")
106
+ response.code.should == 200
107
+ response.body.should == ""
108
+ response.headers.should == "whooo, headers!"
109
+ response.time.should == 0
110
+ end
111
+
112
+ it "should use provided time" do
113
+ @klass.mock(:get, :url => "http://localhost/whatever", :time => 123)
114
+ response = @klass.get("http://localhost/whatever")
115
+ response.code.should == 200
116
+ response.body.should == ""
117
+ response.headers.should == ""
118
+ response.time.should == 123
119
+ end
120
+ end
121
+ end
17
122
 
18
123
  describe "get" do
19
124
  it "should add a get method" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pauldix-typhoeus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Dix