Skip to content
This repository has been archived by the owner on Jul 28, 2018. It is now read-only.

Commit

Permalink
Merge pull request #470 from Thibaut/ruby-tests
Browse files Browse the repository at this point in the history
Add Ruby tests
  • Loading branch information
arthurnn committed Feb 28, 2015
2 parents 00b012c + 395c3ca commit 91d8cc8
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 24 deletions.
39 changes: 19 additions & 20 deletions lib/turbolinks/redirection.rb
Expand Up @@ -31,28 +31,27 @@ def redirect_via_turbolinks_to(url = {}, response_status = {})
end

private
def _extract_turbolinks_options!(options)
turbolinks = options.delete(:turbolinks)
options = options.extract!(:keep, :change, :flush)
raise ArgumentError, "cannot combine :keep, :change and :flush options" if options.size > 1
[turbolinks, options]
end

def _extract_turbolinks_options!(options)
turbolinks = options.delete(:turbolinks)
options = options.extract!(:keep, :change, :flush)
raise ArgumentError, "cannot combine :keep, :change and :flush options" if options.size > 1
[turbolinks, options]
end

def _perform_turbolinks_response(body)
self.status = 200
self.response_body = body
response.content_type = Mime::JS
end
def _perform_turbolinks_response(body)
self.status = 200
self.response_body = body
response.content_type = Mime::JS
end

def _turbolinks_js_options(options)
if options[:change]
", { change: ['#{Array(options[:change]).join("', '")}'] }"
elsif options[:keep]
", { keep: ['#{Array(options[:keep]).join("', '")}'] }"
elsif options[:flush]
", { flush: true }"
def _turbolinks_js_options(options)
if options[:change]
", { change: ['#{Array(options[:change]).join("', '")}'] }"
elsif options[:keep]
", { keep: ['#{Array(options[:keep]).join("', '")}'] }"
elsif options[:flush]
", { flush: true }"
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/turbolinks/x_domain_blocker.rb
Expand Up @@ -18,4 +18,4 @@ def abort_xdomain_redirect
end
end
end
end
end
6 changes: 3 additions & 3 deletions lib/turbolinks/xhr_url_for.rb
@@ -1,12 +1,12 @@
module Turbolinks
# Corrects the behavior of url_for (and link_to, which uses url_for) with the :back
# option by using the X-XHR-Referer request header instead of the standard Referer
# Corrects the behavior of url_for (and link_to, which uses url_for) with the :back
# option by using the X-XHR-Referer request header instead of the standard Referer
# request header.
module XHRUrlFor
def self.included(base)
base.alias_method_chain :url_for, :xhr_referer
end

def url_for_with_xhr_referer(options = {})
options = (controller.request.headers["X-XHR-Referer"] || options) if options == :back
url_for_without_xhr_referer options
Expand Down
97 changes: 97 additions & 0 deletions test/turbolinks/turbolinks_test.rb
@@ -0,0 +1,97 @@
require_relative 'test_helper'

class TurbolinksController < ActionController::Base
def simple_action
render text: ' '
end

def redirect_to_same_origin
redirect_to "#{request.protocol}#{request.host}/path"
end

def redirect_to_different_host
redirect_to "#{request.protocol}foo.#{request.host}/path"
end

def redirect_to_different_protocol
redirect_to "#{request.protocol == 'http://' ? 'https://' : 'http://'}#{request.host}/path"
end

def redirect_to_back
redirect_to :back
end
end

class TurbolinksTest < ActionController::TestCase
tests TurbolinksController

def test_request_referer_returns_xhr_referer_or_standard_referer
@request.headers['Referer'] = 'referer'
assert_equal 'referer', @request.referer

@request.headers['X-XHR-Referer'] = 'xhr-referer'
assert_equal 'xhr-referer', @request.referer
end

def test_url_for_with_back_uses_xhr_referer_when_available
@request.headers['Referer'] = 'referer'
assert_equal 'referer', @controller.view_context.url_for(:back)

@request.headers['X-XHR-Referer'] = 'xhr-referer'
assert_equal 'xhr-referer', @controller.view_context.url_for(:back)
end

def test_redirect_to_back_uses_xhr_referer_when_available
@request.headers['Referer'] = 'http://test.host/referer'
get :redirect_to_back
assert_redirected_to 'http://test.host/referer'

@request.headers['X-XHR-Referer'] = 'http://test.host/xhr-referer'
get :redirect_to_back
assert_redirected_to 'http://test.host/xhr-referer'
end

def test_sets_request_method_cookie_on_non_get_requests
post :simple_action
assert_equal 'POST', cookies[:request_method]
patch :simple_action
assert_equal 'PATCH', cookies[:request_method]
end

def test_pops_request_method_cookie_on_get_request
cookies[:request_method] = 'TEST'
get :simple_action
assert_nil cookies[:request_method]
end

def test_sets_xhr_redirected_to_header_on_redirect_requests_coming_from_turbolinks
get :redirect_to_same_origin
get :simple_action
assert_nil @response.headers['X-XHR-Redirected-To']

@request.headers['X-XHR-Referer'] = 'http://test.host/'
get :redirect_to_same_origin
@request.headers['X-XHR-Referer'] = nil
get :simple_action
assert_equal 'http://test.host/path', @response.headers['X-XHR-Redirected-To']
end

def test_changes_status_to_403_on_turbolinks_requests_redirecting_to_different_origin
get :redirect_to_different_host
assert_response :redirect

get :redirect_to_different_protocol
assert_response :redirect

@request.headers['X-XHR-Referer'] = 'http://test.host'

get :redirect_to_different_host
assert_response :forbidden

get :redirect_to_different_protocol
assert_response :forbidden

get :redirect_to_same_origin
assert_response :redirect
end
end

0 comments on commit 91d8cc8

Please sign in to comment.