Move MIME type conversion into a separate module
This commit is contained in:
parent
ba24901dce
commit
2e542c1cf5
29
lib/webrat/core/mime.rb
Normal file
29
lib/webrat/core/mime.rb
Normal file
@ -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
|
@ -1,6 +1,8 @@
|
|||||||
require "forwardable"
|
require "forwardable"
|
||||||
require "ostruct"
|
require "ostruct"
|
||||||
|
|
||||||
|
require "webrat/core/mime"
|
||||||
|
|
||||||
module Webrat
|
module Webrat
|
||||||
class Session
|
class Session
|
||||||
extend Forwardable
|
extend Forwardable
|
||||||
@ -58,28 +60,8 @@ module Webrat
|
|||||||
@custom_headers[key] = value
|
@custom_headers[key] = value
|
||||||
end
|
end
|
||||||
|
|
||||||
def http_accept(mime_type_as_string_or_symbol)
|
def http_accept(mime_type)
|
||||||
if String === mime_type_as_string_or_symbol
|
header('Accept', Webrat::MIME.mime_type(mime_type))
|
||||||
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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def basic_auth(user, pass)
|
def basic_auth(user, pass)
|
||||||
|
@ -19,7 +19,7 @@ describe Webrat::Session do
|
|||||||
|
|
||||||
it "should open the page in the browser" do
|
it "should open the page in the browser" do
|
||||||
session = Webrat::Session.new
|
session = Webrat::Session.new
|
||||||
session.should_receive(:`).with("open path") #`)
|
session.should_receive(:`).with("open path")
|
||||||
session.open_in_browser("path")
|
session.open_in_browser("path")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user