From 6cda9c79b47d9fd7893184472c98c1e097419fa1 Mon Sep 17 00:00:00 2001 From: Simon Rozet Date: Fri, 26 Jun 2009 01:39:22 +0200 Subject: [PATCH 1/3] Implement Webrat::MIME on top of Rack::Mime --- lib/webrat/core/mime.rb | 35 ++++++++++++------------------- spec/private/core/session_spec.rb | 5 +++++ 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/lib/webrat/core/mime.rb b/lib/webrat/core/mime.rb index 9c55b22..ccff3ec 100644 --- a/lib/webrat/core/mime.rb +++ b/lib/webrat/core/mime.rb @@ -1,29 +1,20 @@ +require "rack/mime" + module Webrat #:nodoc: module MIME #:nodoc: + MIME_TYPES = Rack::Mime::MIME_TYPES.dup.merge( + ".multipart_form" => "multipart/form-data", + ".url_encoded_form" => "application/x-www-form-urlencoded" + ).freeze - def self.mime_type(string_or_symbol) #:nodoc: - 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 + def mime_type(type) + return type if type.nil? || type.to_s.include?("/") + type = ".#{type}" unless type.to_s[0] == ?. + MIME_TYPES.fetch(type) { |type| + raise ArgumentError.new("Invalid Mime type: #{type}") + } end + module_function :mime_type end end diff --git a/spec/private/core/session_spec.rb b/spec/private/core/session_spec.rb index 39f4a1c..c5c0ae9 100644 --- a/spec/private/core/session_spec.rb +++ b/spec/private/core/session_spec.rb @@ -62,6 +62,11 @@ describe Webrat::Session do it "should raise an error if a symbol Mime type is passed that does not exist" do lambda { webrat_session.http_accept(:oogabooga) }.should raise_error(ArgumentError) end + + it "should recognize a couple of webrat-specific formats" do + webrat_session.http_accept(:multipart_form).should == "multipart/form-data" + webrat_session.http_accept(:url_encoded_form).should == "application/x-www-form-urlencoded" + end end describe "#request_page" do From 0a2b77c9c4fe0085adc3ef20ee49daddb00044e4 Mon Sep 17 00:00:00 2001 From: Simon Rozet Date: Fri, 26 Jun 2009 01:41:42 +0200 Subject: [PATCH 2/3] Require rack in lib/webrat.rb --- lib/webrat.rb | 1 + lib/webrat/core/mime.rb | 2 -- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/webrat.rb b/lib/webrat.rb index dd4ba8f..04826d3 100644 --- a/lib/webrat.rb +++ b/lib/webrat.rb @@ -1,4 +1,5 @@ require "rubygems" +require "rack" $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__))) unless $LOAD_PATH.include?(File.expand_path(File.dirname(__FILE__))) diff --git a/lib/webrat/core/mime.rb b/lib/webrat/core/mime.rb index ccff3ec..873f729 100644 --- a/lib/webrat/core/mime.rb +++ b/lib/webrat/core/mime.rb @@ -1,5 +1,3 @@ -require "rack/mime" - module Webrat #:nodoc: module MIME #:nodoc: MIME_TYPES = Rack::Mime::MIME_TYPES.dup.merge( From 195f9c35449aafe0cacb2354d5bbde77e2f07d88 Mon Sep 17 00:00:00 2001 From: Simon Rozet Date: Fri, 26 Jun 2009 01:43:12 +0200 Subject: [PATCH 3/3] Add Rack >= 1.0 as a dependency to gemspec --- Rakefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Rakefile b/Rakefile index f4d2ec1..031ab2b 100644 --- a/Rakefile +++ b/Rakefile @@ -30,6 +30,7 @@ spec = Gem::Specification.new do |s| # Dependencies s.add_dependency "nokogiri", ">= 1.2.0" + s.add_dependency "rack", ">= 1.0" s.rubyforge_project = "webrat" end