pauldix-typhoeus 0.0.10 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -22,9 +22,9 @@ module Typhoeus
22
22
  return nil unless @remote_mocks
23
23
  if @remote_mocks.has_key? method
24
24
  if @remote_mocks[method].has_key? url
25
- get_mock_and_run_handlers(@remote_mocks[method][url], options)
25
+ get_mock_and_run_handlers(method, @remote_mocks[method][url], options)
26
26
  elsif @remote_mocks[method].has_key? :catch_all
27
- get_mock_and_run_handlers(@remote_mocks[method][:catch_all], options)
27
+ get_mock_and_run_handlers(method, @remote_mocks[method][:catch_all], options)
28
28
  else
29
29
  nil
30
30
  end
@@ -33,8 +33,16 @@ module Typhoeus
33
33
  end
34
34
  end
35
35
 
36
- def get_mock_and_run_handlers(response_args, options)
36
+ def get_mock_and_run_handlers(method, response_args, options)
37
37
  response = Response.new(response_args[:code], response_args[:headers], response_args[:body], response_args[:time])
38
+ if response_args.has_key? :expected_body
39
+ raise "#{method} expected body of \"#{response_args[:expected_body]}\" but received #{options[:body]}" if response_args[:expected_body] != options[:body]
40
+ end
41
+
42
+ if response_args.has_key? :expected_headers
43
+ raise "#{method} expected body of \"#{response_args[:expected_headers].inspect}\" but received #{options[:headers].inspect}" if response_args[:expected_headers] != options[:headers]
44
+ end
45
+
38
46
  if response.code >= 200 && response.code < 300 && options.has_key?(:on_success)
39
47
  response = options[:on_success].call(response)
40
48
  elsif options.has_key?(:on_failure)
@@ -72,6 +80,7 @@ module Typhoeus
72
80
 
73
81
  easy.url = url
74
82
  easy.method = method
83
+ easy.headers = options[:headers] if options.has_key? :headers
75
84
  easy.headers["User-Agent"] = (options[:user_agent] || Typhoeus::USER_AGENT)
76
85
  easy.params = options[:params] if options[:params]
77
86
  easy.request_body = options[:body] if options[:body]
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.10"
15
+ VERSION = "0.0.11"
16
16
 
17
17
  def self.easy_object_pool
18
18
  @easy_objects ||= []
@@ -63,6 +63,44 @@ describe Typhoeus do
63
63
  @klass.get("http://localhost:234234234").code.should == 0
64
64
  end
65
65
 
66
+ describe "request body expectations" do
67
+ before(:all) do
68
+ @body_klass = Class.new do
69
+ include Typhoeus
70
+ end
71
+ @body_klass.mock(:put, :url => "http://whatev", :expected_body => "hi")
72
+ end
73
+
74
+ it "should take an expected request body" do
75
+ @body_klass.put("http://whatev", :body => "hi").code.should == 200
76
+ end
77
+
78
+ it "should raise if the expected request body doesn't match" do
79
+ lambda {
80
+ @body_klass.put("http://whatev", :body => "not what we expect")
81
+ }.should raise_error
82
+ end
83
+ end
84
+
85
+ describe "request header expectations" do
86
+ before(:all) do
87
+ @header_klass = Class.new do
88
+ include Typhoeus
89
+ end
90
+ @header_klass.mock(:get, :url => "http://asdf", :expected_headers => {"If-None-Match" => "\"lkjsd90823\""})
91
+ end
92
+
93
+ it "should take expected request headers" do
94
+ @header_klass.get("http://asdf", :headers => {"If-None-Match" => "\"lkjsd90823\""})
95
+ end
96
+
97
+ it "should raise if the expected request headers don't match" do
98
+ lambda {
99
+ @header_klass.get("http://asdf")
100
+ }.should raise_error
101
+ end
102
+ end
103
+
66
104
  describe "remote methods" do
67
105
  it "should work for defined remote methods" do
68
106
  @klass.instance_eval 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.10
4
+ version: 0.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Dix
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-03-12 00:00:00 -07:00
12
+ date: 2009-07-03 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15