diff --git a/lib/webrat/core/mime.rb b/lib/webrat/core/mime.rb new file mode 100644 index 0000000..7851e4d --- /dev/null +++ b/lib/webrat/core/mime.rb @@ -0,0 +1,29 @@ +module Webrat + module MIME + + def self.mime_type(string_or_symbol) + if string_or_symbol.is_a?(String) + string_or_symbol + else + case string_or_symbol + when :text then "text/plain" + when :html then "text/html" + when :js then "text/javascript" + when :css then "text/css" + when :ics then "text/calendar" + when :csv then "text/csv" + when :xml then "application/xml" + when :rss then "application/rss+xml" + when :atom then "application/atom+xml" + when :yaml then "application/x-yaml" + when :multipart_form then "multipart/form-data" + when :url_encoded_form then "application/x-www-form-urlencoded" + when :json then "application/json" + else + raise ArgumentError.new("Invalid Mime type: #{string_or_symbol.inspect}") + end + end + end + + end +end \ No newline at end of file diff --git a/lib/webrat/core/session.rb b/lib/webrat/core/session.rb index 56fdb4d..b33c30b 100644 --- a/lib/webrat/core/session.rb +++ b/lib/webrat/core/session.rb @@ -1,6 +1,8 @@ require "forwardable" require "ostruct" +require "webrat/core/mime" + module Webrat class Session extend Forwardable @@ -58,28 +60,8 @@ module Webrat @custom_headers[key] = value end - def http_accept(mime_type_as_string_or_symbol) - if String === mime_type_as_string_or_symbol - header('Accept', mime_type_as_string_or_symbol) - elsif Symbol === mime_type_as_string_or_symbol - mime_type_as_string = case mime_type_as_string_or_symbol - when :text then "text/plain" - when :html then "text/html" - when :js then "text/javascript" - when :css then "text/css" - when :ics then "text/calendar" - when :csv then "text/csv" - when :xml then "application/xml" - when :rss then "application/rss+xml" - when :atom then "application/atom+xml" - when :yaml then "application/x-yaml" - when :multipart_form then "multipart/form-data" - when :url_encoded_form then "application/x-www-form-urlencoded" - when :json then "application/json" - end - raise ArgumentError.new("Invalid Mime type: #{mime_type_as_string_or_symbol.inspect}") unless mime_type_as_string - header('Accept', mime_type_as_string) - end + def http_accept(mime_type) + header('Accept', Webrat::MIME.mime_type(mime_type)) end def basic_auth(user, pass) diff --git a/spec/webrat/core/session_spec.rb b/spec/webrat/core/session_spec.rb index de11c8b..a0790e8 100644 --- a/spec/webrat/core/session_spec.rb +++ b/spec/webrat/core/session_spec.rb @@ -19,7 +19,7 @@ describe Webrat::Session do it "should open the page in the browser" do session = Webrat::Session.new - session.should_receive(:`).with("open path") #`) + session.should_receive(:`).with("open path") session.open_in_browser("path") end