Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
assembler committed Feb 15, 2012
1 parent ce92643 commit c7fbf22
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 56 deletions.
39 changes: 16 additions & 23 deletions lib/browsernizer/router.rb
Expand Up @@ -21,13 +21,18 @@ def call(env)


private private
def handle_request def handle_request
if !html_request? || path_excluded? @env["browsernizer"]["supported"] = false if unsupported?
propagate_request
elsif !on_redirection_path? && unsupported? catch(:response) do
handle_unsupported if !path_excluded?
elsif on_redirection_path? && !unsupported? if unsupported?
handle_visits_by_accident if !on_redirection_path? && @config.get_location
else throw :response, redirect_to_specified
end
elsif on_redirection_path?
throw :response, redirect_to_root
end
end
propagate_request propagate_request
end end
end end
Expand All @@ -36,32 +41,20 @@ def propagate_request
@app.call(@env) @app.call(@env)
end end


def handle_unsupported def redirect_to_specified
@env["browsernizer"]["supported"] = false [307, {"Content-Type" => "text/plain", "Location" => @config.get_location}, []]

if @config.get_location
[307, {"Content-Type" => "text/plain", "Location" => @config.get_location}, []]
else
propagate_request
end
end end


def handle_visits_by_accident def redirect_to_root
[303, {"Content-Type" => "text/plain", "Location" => "/"}, []] [303, {"Content-Type" => "text/plain", "Location" => "/"}, []]
end 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? def path_excluded?
@config.excluded? @env["PATH_INFO"] @config.excluded? @env["PATH_INFO"]
end end


def on_redirection_path? def on_redirection_path?
@env["PATH_INFO"] && @env["PATH_INFO"] == @config.get_location @config.get_location && @config.get_location == @env["PATH_INFO"]
end end


def agent def agent
Expand Down
40 changes: 7 additions & 33 deletions spec/browsernizer/router_spec.rb
Expand Up @@ -14,7 +14,6 @@
let(:default_env) do let(:default_env) do
{ {
"HTTP_USER_AGENT" => chrome_agent("7.1.1"), "HTTP_USER_AGENT" => chrome_agent("7.1.1"),
"HTTP_ACCEPT" => "text/html",
"PATH_INFO" => "/index" "PATH_INFO" => "/index"
} }
end end
Expand Down Expand Up @@ -76,45 +75,20 @@
subject.call(@env) subject.call(@env)
end end
end end
end


context "Non-html request" do context "Already on /browser.html page" 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
before do before do
subject.config.exclude %r{^/assets} @env = @env.merge({
subject.config.location "/browser.html" "PATH_INFO" => "/browser.html"
})
end end
it "handles the request" do it "propagates request" do
app.should_not_receive(:call).with(@env) app.should_receive(:call).with(@env)
response = subject.call(@env) subject.call(@env)
response[0].should == 307
response[1]["Location"].should == "/browser.html"
end end
end 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 end




Expand Down

0 comments on commit c7fbf22

Please sign in to comment.