Skip to content
Browse files

cleanup

  • Loading branch information...
1 parent ce92643 commit c7fbf228e209d9ebf2eca075b36fcbabe79c95e0 @assembler committed Feb 15, 2012
Showing with 23 additions and 56 deletions.
  1. +16 −23 lib/browsernizer/router.rb
  2. +7 −33 spec/browsernizer/router_spec.rb
View
39 lib/browsernizer/router.rb
@@ -21,13 +21,18 @@ def call(env)
private
def handle_request
- if !html_request? || path_excluded?
- propagate_request
- elsif !on_redirection_path? && unsupported?
- handle_unsupported
- elsif on_redirection_path? && !unsupported?
- handle_visits_by_accident
- else
+ @env["browsernizer"]["supported"] = false if unsupported?
+
+ catch(:response) do
+ if !path_excluded?
+ if unsupported?
+ if !on_redirection_path? && @config.get_location
+ throw :response, redirect_to_specified
+ end
+ elsif on_redirection_path?
+ throw :response, redirect_to_root
+ end
+ end
propagate_request
end
end
@@ -36,32 +41,20 @@ def propagate_request
@app.call(@env)
end
- def handle_unsupported
- @env["browsernizer"]["supported"] = false
-
- if @config.get_location
- [307, {"Content-Type" => "text/plain", "Location" => @config.get_location}, []]
- else
- propagate_request
- end
+ def redirect_to_specified
+ [307, {"Content-Type" => "text/plain", "Location" => @config.get_location}, []]
end
- def handle_visits_by_accident
+ def redirect_to_root
[303, {"Content-Type" => "text/plain", "Location" => "/"}, []]
end
- # if exclusions are defined, we'll be ignoring HTTP_ACCEPT header
- def html_request?
- return true if @config.exclusions_defined?
- @env["HTTP_ACCEPT"] && @env["HTTP_ACCEPT"].include?("text/html")
- end
-
def path_excluded?
@config.excluded? @env["PATH_INFO"]
end
def on_redirection_path?
- @env["PATH_INFO"] && @env["PATH_INFO"] == @config.get_location
+ @config.get_location && @config.get_location == @env["PATH_INFO"]
end
def agent
View
40 spec/browsernizer/router_spec.rb
@@ -14,7 +14,6 @@
let(:default_env) do
{
"HTTP_USER_AGENT" => chrome_agent("7.1.1"),
- "HTTP_ACCEPT" => "text/html",
"PATH_INFO" => "/index"
}
end
@@ -76,45 +75,20 @@
subject.call(@env)
end
end
- end
- context "Non-html request" do
- before do
- @env = @env.merge({
- "HTTP_ACCEPT" => "text/css"
- })
- end
- it "propagates request" do
- app.should_receive(:call).with(@env)
- subject.call(@env)
- end
-
- context "exclusions defined" do
+ context "Already on /browser.html page" do
before do
- subject.config.exclude %r{^/assets}
- subject.config.location "/browser.html"
+ @env = @env.merge({
+ "PATH_INFO" => "/browser.html"
+ })
end
- it "handles the request" do
- app.should_not_receive(:call).with(@env)
- response = subject.call(@env)
- response[0].should == 307
- response[1]["Location"].should == "/browser.html"
+ it "propagates request" do
+ app.should_receive(:call).with(@env)
+ subject.call(@env)
end
end
end
-
- context "Already on /browser.html page" do
- before do
- @env = @env.merge({
- "PATH_INFO" => "/browser.html"
- })
- end
- it "propagates request" do
- app.should_receive(:call).with(@env)
- subject.call(@env)
- end
- end
end

0 comments on commit c7fbf22

Please sign in to comment.
Something went wrong with that request. Please try again.