2017-07-23 11:36:41 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-02-21 13:41:17 -05:00
|
|
|
require "rack"
|
2016-11-25 23:42:29 -05:00
|
|
|
require "rails"
|
|
|
|
require "action_controller/railtie"
|
|
|
|
require "action_view/railtie"
|
|
|
|
require "blade"
|
|
|
|
require "json"
|
|
|
|
|
|
|
|
module UJS
|
|
|
|
class Server < Rails::Application
|
|
|
|
routes.append do
|
|
|
|
get "/rails-ujs.js" => Blade::Assets.environment
|
|
|
|
get "/" => "tests#index"
|
|
|
|
match "/echo" => "tests#echo", via: :all
|
2016-12-28 21:53:51 -05:00
|
|
|
get "/error" => proc { |env| [403, {}, []] }
|
2016-11-25 23:42:29 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
config.cache_classes = false
|
|
|
|
config.eager_load = false
|
|
|
|
config.secret_key_base = "59d7a4dbd349fa3838d79e330e39690fc22b931e7dc17d9162f03d633d526fbb92dfdb2dc9804c8be3e199631b9c1fbe43fc3e4fc75730b515851849c728d5c7"
|
2017-02-21 13:41:17 -05:00
|
|
|
config.paths["app/views"].unshift("#{Rails.root}/views")
|
2016-11-25 23:42:29 -05:00
|
|
|
config.public_file_server.enabled = true
|
|
|
|
config.logger = Logger.new(STDOUT)
|
|
|
|
config.log_level = :error
|
2018-02-16 08:21:48 -05:00
|
|
|
|
|
|
|
config.content_security_policy do |policy|
|
|
|
|
policy.default_src :self, :https
|
|
|
|
policy.font_src :self, :https, :data
|
|
|
|
policy.img_src :self, :https, :data
|
|
|
|
policy.object_src :none
|
|
|
|
policy.script_src :self, :https
|
|
|
|
policy.style_src :self, :https
|
|
|
|
end
|
|
|
|
|
|
|
|
config.content_security_policy_nonce_generator = ->(req) { SecureRandom.base64(16) }
|
2016-11-25 23:42:29 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module TestsHelper
|
2016-12-28 21:53:51 -05:00
|
|
|
def test_to(*names)
|
2018-02-16 08:21:48 -05:00
|
|
|
names = names.map { |name| "/test/#{name}.js" }
|
|
|
|
names = %w[/vendor/qunit.js /test/settings.js] + names
|
2016-11-25 23:42:29 -05:00
|
|
|
|
2018-02-16 08:21:48 -05:00
|
|
|
capture do
|
|
|
|
names.each do |name|
|
|
|
|
concat(javascript_include_tag(name))
|
|
|
|
end
|
|
|
|
end
|
2016-11-25 23:42:29 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class TestsController < ActionController::Base
|
|
|
|
helper TestsHelper
|
|
|
|
layout "application"
|
|
|
|
|
|
|
|
def index
|
|
|
|
render :index
|
|
|
|
end
|
|
|
|
|
|
|
|
def echo
|
2016-12-28 21:53:51 -05:00
|
|
|
data = { params: params.to_unsafe_h }.update(request.env)
|
2016-11-25 23:42:29 -05:00
|
|
|
|
2016-12-28 21:53:51 -05:00
|
|
|
if params[:content_type] && params[:content]
|
2016-11-25 23:42:29 -05:00
|
|
|
render inline: params[:content], content_type: params[:content_type]
|
|
|
|
elsif request.xhr?
|
2017-12-14 04:14:24 -05:00
|
|
|
if params[:with_xhr_redirect]
|
|
|
|
response.set_header("X-Xhr-Redirect", "http://example.com/")
|
|
|
|
render inline: %{Turbolinks.clearCache()\nTurbolinks.visit("http://example.com/", {"action":"replace"})}
|
|
|
|
else
|
|
|
|
render json: JSON.generate(data)
|
|
|
|
end
|
2016-11-25 23:42:29 -05:00
|
|
|
elsif params[:iframe]
|
2016-12-28 21:53:51 -05:00
|
|
|
payload = JSON.generate(data).gsub("<", "<").gsub(">", ">")
|
2016-11-25 23:42:29 -05:00
|
|
|
html = <<-HTML
|
2018-02-16 08:21:48 -05:00
|
|
|
<script nonce="#{request.content_security_policy_nonce}">
|
2016-11-25 23:42:29 -05:00
|
|
|
if (window.top && window.top !== window)
|
|
|
|
window.top.jQuery.event.trigger('iframe:loaded', #{payload})
|
|
|
|
</script>
|
|
|
|
<p>You shouldn't be seeing this. <a href="#{request.env['HTTP_REFERER']}">Go back</a></p>
|
|
|
|
HTML
|
|
|
|
|
|
|
|
render html: html.html_safe
|
|
|
|
else
|
2016-12-03 01:28:59 -05:00
|
|
|
render plain: "ERROR: #{request.path} requested without ajax", status: 404
|
2016-11-25 23:42:29 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Blade.initialize!
|
|
|
|
UJS::Server.initialize!
|