Permalink
Browse files

Add support for specifying HTTP headers as options to FakeWeb.registe…

…r_uri when using the :string or :file response types
  • Loading branch information...
1 parent c531c0e commit b8fbab47133e3c9089e9218e13a268b9232e1d21 @chrisk committed Jun 14, 2009
Showing with 98 additions and 0 deletions.
  1. +4 −0 CHANGELOG
  2. +10 −0 README.rdoc
  3. +5 −0 lib/fake_web.rb
  4. +8 −0 lib/fake_web/responder.rb
  5. +71 −0 test/test_response_headers.rb
View
@@ -1,5 +1,9 @@
fakeweb (development)
+* add support for specifying HTTP headers as options to FakeWeb.register_uri
+ when using the :string or :file response types, since those methods only
+ specify a response body [David Michael, Chris Kampmeier]
+
* DEPRECATION: Calling FakeWeb.register_uri and FakeWeb.registered_uri? without
an HTTP method as the first argument is now deprecated. To match against any
HTTP method (the pre-1.2.0 behavior), use :any [Chris Kampmeier]
View
@@ -131,6 +131,16 @@ This is handy when you want to make sure your tests are self-contained, or you
want to catch the scenario when a URI is changed in implementation code
without a corresponding test change.
+=== Specifying HTTP response headers
+
+When you register a response using the <tt>:string</tt> or <tt>:file</tt>
+options, you're only setting the body of the response. If you want to add
+headers to these responses, simply add the header as an option to
++register_uri+:
+
+ FakeWeb.register_uri(:get, "http://example.com/hello.txt", :string => "Hello", :content_type => "text/plain")
+
+This sets the "Content-Type" header in the response.
== More info
View
@@ -104,6 +104,11 @@ class NetConnectNotAllowedError < StandardError; end;
# specified URL is requested. Any +Exception+ class is valid. Example:
# FakeWeb.register_uri('http://www.example.com/', :exception => Net::HTTPError)
#
+ # If you're using the <tt>:string</tt> or <tt>:file</tt> response type, you can
+ # pass additional options to specify the HTTP headers to be used in the response.
+ # Example:
+ #
+ # FakeWeb.register_uri(:get, "http://example.com/index.txt", :string => "Hello", :content_type => "text/plain")
def self.register_uri(*args)
case args.length
when 3
@@ -2,6 +2,7 @@ module FakeWeb
class Responder #:nodoc:
attr_accessor :method, :uri, :options, :times
+ KNOWN_OPTIONS = [:exception, :file, :response, :status, :string].freeze
def initialize(method, uri, options, times)
self.method = method
@@ -17,6 +18,7 @@ def response(&block)
code, msg = meta_information
response = Net::HTTPResponse.send(:response_class, code.to_s).new("1.0", code.to_s, msg)
response.instance_variable_set(:@body, content)
+ headers_extracted_from_options.each { |name, value| response[name] = value }
end
response.instance_variable_set(:@read, true)
@@ -31,6 +33,12 @@ def response(&block)
private
+ def headers_extracted_from_options
+ options.reject {|name, _| KNOWN_OPTIONS.include?(name) }.map { |name, value|
+ [name.to_s.split("_").map { |segment| segment.capitalize }.join("-"), value]
+ }
+ end
+
def content
[ :file, :string ].each do |map_option|
next unless options.has_key?(map_option)
@@ -0,0 +1,71 @@
+require File.join(File.dirname(__FILE__), "test_helper")
+
+class TestResponseHeaders < Test::Unit::TestCase
+
+ def setup
+ FakeWeb.clean_registry
+ end
+
+ def test_content_type_when_registering_with_string_and_content_type_header
+ FakeWeb.register_uri(:get, "http://example.com/users.json", :string => '[{"username": "chrisk"}]', :content_type => "application/json")
+ response = Net::HTTP.start("example.com") { |query| query.get("/users.json") }
+ assert_equal '[{"username": "chrisk"}]', response.body
+ assert_equal "application/json", response['Content-Type']
+ end
+
+ def test_content_type_when_registering_with_string_only
+ FakeWeb.register_uri(:get, "http://example.com/users.json", :string => '[{"username": "chrisk"}]')
+ response = Net::HTTP.start("example.com") { |query| query.get("/users.json") }
+ assert_equal '[{"username": "chrisk"}]', response.body
+ assert_nil response['Content-Type']
+ end
+
+ def test_cookies_when_registering_with_file_and_set_cookie_header
+ FakeWeb.register_uri(:get, "http://example.com/", :file => File.dirname(__FILE__) + '/fixtures/test_example.txt',
+ :set_cookie => "user_id=1; example=yes")
+ response = Net::HTTP.start("example.com") { |query| query.get("/") }
+ assert_equal "test example content", response.body
+ assert_equal "user_id=1; example=yes", response['Set-Cookie']
+ end
+
+ def test_registering_with_baked_response_ignores_header_options
+ fake_response = Net::HTTPOK.new('1.1', '200', 'OK')
+ fake_response["Server"] = "Apache/1.3.27 (Unix)"
+ FakeWeb.register_uri(:get, "http://example.com/", :response => fake_response,
+ :server => "FakeWeb/1.2.3 (Ruby)")
+ response = Net::HTTP.start("example.com") { |query| query.get("/") }
+ assert_equal "200", response.code
+ assert_equal "OK", response.message
+ assert_equal "Apache/1.3.27 (Unix)", response["Server"]
+ end
+
+ def test_headers_are_rotated_when_registering_with_response_rotation
+ FakeWeb.register_uri(:get, "http://example.com",
+ [{:string => 'test1', :expires => "Thu, 14 Jun 2009 16:00:00 GMT",
+ :content_type => "text/plain"},
+ {:string => 'test2', :expires => "Thu, 14 Jun 2009 16:00:01 GMT"}])
+
+ Net::HTTP.start("example.com") do |query|
+ response = query.get("/")
+ assert_equal 'test1', response.body
+ assert_equal "Thu, 14 Jun 2009 16:00:00 GMT", response['Expires']
+ assert_equal "text/plain", response['Content-Type']
+
+ response = query.get("/")
+ assert_equal 'test2', response.body
+ assert_equal "Thu, 14 Jun 2009 16:00:01 GMT", response['Expires']
+ assert_nil response['Content-Type']
+ end
+ end
+
+ def test_registering_with_status_option_and_response_headers
+ FakeWeb.register_uri(:get, "http://example.com", :status => ["301", "Moved Permanently"],
+ :location => "http://www.example.com")
+
+ response = Net::HTTP.start("example.com") { |query| query.get("/") }
+ assert_equal "301", response.code
+ assert_equal "Moved Permanently", response.message
+ assert_equal "http://www.example.com", response["Location"]
+ end
+
+end

0 comments on commit b8fbab4

Please sign in to comment.