diff --git a/.gitignore b/.gitignore
index 2d29fe59..c35bc033 100644
--- a/.gitignore
+++ b/.gitignore
@@ -9,5 +9,6 @@ test/dummy/tmp/
test/dummy/.sass-cache
Gemfile.lock
node_modules/
+package-lock.json
dist/
tmp/
diff --git a/extensions/chrome/js/background.js b/extensions/chrome/js/background.js
index 11054243..b7e62a24 100644
--- a/extensions/chrome/js/background.js
+++ b/extensions/chrome/js/background.js
@@ -84,10 +84,10 @@ function initHttpListener() {
function onResponse(details) {
var headers = getHeaders(details);
var sessionId;
- if (sessionId = headers['X-Web-Console-Session-Id']) {
+ if (sessionId = headers['x-web-console-session-id']) {
sessions[details.tabId] = {
sessionId: sessionId,
- mountPoint: headers['X-Web-Console-Mount-Point'],
+ mountPoint: headers['x-web-console-mount-point'],
remoteHost: details.url.match(/([^:]+:\/\/[^\/]+)\/?/)[1]
};
}
diff --git a/lib/web_console.rb b/lib/web_console.rb
index 3f3f88af..38526094 100644
--- a/lib/web_console.rb
+++ b/lib/web_console.rb
@@ -26,7 +26,7 @@ module WebConsole
end
def self.logger
- Rails.logger || (@logger ||= ActiveSupport::Logger.new($stderr))
+ (defined?(Rails.logger) && Rails.logger) || (@logger ||= ActiveSupport::Logger.new($stderr))
end
end
diff --git a/lib/web_console/injector.rb b/lib/web_console/injector.rb
index d3bb3253..a82cd051 100644
--- a/lib/web_console/injector.rb
+++ b/lib/web_console/injector.rb
@@ -13,10 +13,10 @@ def initialize(body, headers)
end
def inject(content)
- # Set Content-Length header to the size of the current body
+ # Set content-length header to the size of the current body
# + the extra content. Otherwise the response will be truncated.
- if @headers["Content-Length"]
- @headers["Content-Length"] = (@body.bytesize + content.bytesize).to_s
+ if @headers["content-length"]
+ @headers["content-length"] = (@body.bytesize + content.bytesize).to_s
end
[
diff --git a/lib/web_console/middleware.rb b/lib/web_console/middleware.rb
index 93320ee0..256e1514 100644
--- a/lib/web_console/middleware.rb
+++ b/lib/web_console/middleware.rb
@@ -28,8 +28,8 @@ def call(env)
status, headers, body = call_app(env)
if (session = Session.from(Thread.current)) && acceptable_content_type?(headers)
- headers["X-Web-Console-Session-Id"] = session.id
- headers["X-Web-Console-Mount-Point"] = mount_point
+ headers["x-web-console-session-id"] = session.id
+ headers["x-web-console-mount-point"] = mount_point
template = Template.new(env, session)
body, headers = Injector.new(body, headers).inject(template.render("index"))
@@ -52,12 +52,12 @@ def call(env)
private
def acceptable_content_type?(headers)
- headers["Content-Type"].to_s.include?("html")
+ headers["content-type"].to_s.include?("html")
end
def json_response(opts = {})
status = opts.fetch(:status, 200)
- headers = { "Content-Type" => "application/json; charset = utf-8" }
+ headers = { "content-type" => "application/json; charset = utf-8" }
body = yield.to_json
[ status, headers, [ body ] ]
diff --git a/lib/web_console/tasks/templates.rake b/lib/web_console/tasks/templates.rake
index f51ae659..9090baaa 100644
--- a/lib/web_console/tasks/templates.rake
+++ b/lib/web_console/tasks/templates.rake
@@ -1,5 +1,7 @@
# frozen_string_literal: true
+require "net/http"
+
namespace :templates do
desc "Run tests for templates"
task test: [ :daemonize, :npm, :rackup, :wait, :mocha, :kill, :exit ]
@@ -10,7 +12,6 @@ namespace :templates do
runner = URI.parse("http://#{ENV['IP'] || '127.0.0.1'}:#{ENV['PORT'] || 29292}/html/test_runner.html")
rackup = "rackup --host #{runner.host} --port #{runner.port}"
result = nil
- browser = "phantomjs"
def need_to_wait?(uri)
Net::HTTP.start(uri.host, uri.port) { |http| http.get(uri.path) }
@@ -22,17 +23,10 @@ namespace :templates do
rackup += " -D --pid #{pid}"
end
- task npm: [ :phantomjs ] do
+ task :npm do
Dir.chdir(workdir) { system "npm install --silent" }
end
- task :phantomjs do
- unless system("which #{browser} >/dev/null")
- browser = "./node_modules/.bin/phantomjs"
- Dir.chdir(workdir) { system("test -f #{browser} || npm install --silent phantomjs-prebuilt") }
- end
- end
-
task :rackup do
Dir.chdir(workdir) { system rackup }
end
@@ -43,7 +37,7 @@ namespace :templates do
end
task :mocha do
- Dir.chdir(workdir) { result = system("#{browser} ./node_modules/mocha-phantomjs-core/mocha-phantomjs-core.js #{runner} dot") }
+ Dir.chdir(workdir) { result = system("npx mocha-headless-chrome -f #{runner} -r dot") }
end
task :kill do
diff --git a/lib/web_console/templates/console.js.erb b/lib/web_console/templates/console.js.erb
index 108f91ff..6b8021bc 100644
--- a/lib/web_console/templates/console.js.erb
+++ b/lib/web_console/templates/console.js.erb
@@ -935,8 +935,8 @@ REPLConsole.request = function request(method, url, params, callback) {
var xhr = new REPLConsole.XMLHttpRequest();
xhr.open(method, url, true);
- xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
- xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
+ xhr.setRequestHeader("content-type", "application/x-www-form-urlencoded");
+ xhr.setRequestHeader("x-requested-with", "XMLHttpRequest");
xhr.send(params);
xhr.onreadystatechange = function() {
diff --git a/lib/web_console/testing/fake_middleware.rb b/lib/web_console/testing/fake_middleware.rb
index 144fc292..9e3488d5 100644
--- a/lib/web_console/testing/fake_middleware.rb
+++ b/lib/web_console/testing/fake_middleware.rb
@@ -1,6 +1,7 @@
# frozen_string_literal: true
require "action_view"
+require "action_dispatch" # This is needed to use Mime::Type
require "web_console"
require "web_console/testing/helper"
@@ -9,7 +10,7 @@ module Testing
class FakeMiddleware
I18n.load_path.concat(Dir[Helper.gem_root.join("lib/web_console/locales/*.yml")])
- DEFAULT_HEADERS = { "Content-Type" => "application/javascript" }
+ DEFAULT_HEADERS = { "content-type" => "application/javascript" }
def initialize(opts)
@headers = opts.fetch(:headers, DEFAULT_HEADERS)
@@ -18,18 +19,21 @@ def initialize(opts)
end
def call(env)
- [ 200, @headers, [ render(req_path(env)) ] ]
+ body = render(req_path(env))
+ @headers["content-length"] = body.bytesize.to_s
+
+ [ 200, @headers, [ body ] ]
end
def view
- @view = View.new(@view_path)
+ @view = View.with_empty_template_cache.with_view_paths(@view_path)
end
private
# extract target path from REQUEST_PATH
def req_path(env)
- env["REQUEST_PATH"].match(@req_path_regex)[1]
+ File.basename(env["REQUEST_PATH"].match(@req_path_regex)[1], ".*")
end
def render(template)
diff --git a/test/templates/config.ru b/test/templates/config.ru
index 2dc93d1a..b89f8376 100644
--- a/test/templates/config.ru
+++ b/test/templates/config.ru
@@ -15,18 +15,11 @@ end
map "/html" do
run WebConsole::Testing::FakeMiddleware.new(
req_path_regex: %r{^/html/(.*)},
- headers: {"Content-Type" => "text/html"},
+ headers: {"content-type" => "text/html"},
view_path: TEST_ROOT.join("html"),
)
end
-map "/spec" do
- run WebConsole::Testing::FakeMiddleware.new(
- req_path_regex: %r{^/spec/(.*)},
- view_path: TEST_ROOT.join("spec"),
- )
-end
-
map "/test" do
run WebConsole::Testing::FakeMiddleware.new(
req_path_regex: %r{^/test/(.*)},
@@ -42,19 +35,19 @@ map "/templates" do
end
map "/mock/repl_sessions/result" do
- headers = { 'Content-Type' => 'application/json' }
+ headers = { 'content-type' => 'application/json' }
body = [ { output: '=> "fake-result"\n', context: [ [ :something, :somewhat, :somewhere ] ] }.to_json ]
run lambda { |env| [ 200, headers, body ] }
end
map "/mock/repl_sessions/error" do
- headers = { 'Content-Type' => 'application/json' }
+ headers = { 'content-type' => 'application/json' }
body = [ { output: 'fake-error-message' }.to_json ]
run lambda { |env| [ 400, headers, body ] }
end
map "/mock/repl_sessions/error.txt" do
- headers = { 'Content-Type' => 'plain/text' }
+ headers = { 'content-type' => 'plain/text' }
body = [ 'error message' ]
run lambda { |env| [ 400, headers, body ] }
end
diff --git a/test/templates/html/test_runner.html.erb b/test/templates/html/test_runner.html.erb
index f82809a1..a4ccb647 100644
--- a/test/templates/html/test_runner.html.erb
+++ b/test/templates/html/test_runner.html.erb
@@ -12,7 +12,6 @@
@@ -26,11 +25,7 @@