From 0a3621e117bf40639a07b766fa43396bfd4db36c Mon Sep 17 00:00:00 2001 From: mishina Date: Wed, 8 Jun 2022 22:36:13 +0900 Subject: [PATCH 01/14] Require `net/http` --- lib/web_console/tasks/templates.rake | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/web_console/tasks/templates.rake b/lib/web_console/tasks/templates.rake index f51ae659..101feb5c 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 ] From 31659556a3f81fa8da41f42e115dc35b9f63c86b Mon Sep 17 00:00:00 2001 From: mishina Date: Wed, 8 Jun 2022 22:54:32 +0900 Subject: [PATCH 02/14] Address the change of ActionView::Base.new in Rails 6 Same change as rails/web-console#304 --- lib/web_console/testing/fake_middleware.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/web_console/testing/fake_middleware.rb b/lib/web_console/testing/fake_middleware.rb index 144fc292..360547c0 100644 --- a/lib/web_console/testing/fake_middleware.rb +++ b/lib/web_console/testing/fake_middleware.rb @@ -22,7 +22,7 @@ def call(env) end def view - @view = View.new(@view_path) + @view = View.with_empty_template_cache.with_view_paths(@view_path) end private From 5af8b65ace0f5fcefcb21dfaf112369c1b1ec444 Mon Sep 17 00:00:00 2001 From: mishina Date: Wed, 8 Jun 2022 22:55:03 +0900 Subject: [PATCH 03/14] Render templates without '.' in the name Similar change as rails/web-console#298 refs: - rails/rails#39164 - https://stackoverflow.com/questions/68836670/rails-6-1-4-deprecation-warning-rendering-actions-with --- lib/web_console/testing/fake_middleware.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/web_console/testing/fake_middleware.rb b/lib/web_console/testing/fake_middleware.rb index 360547c0..062b33c8 100644 --- a/lib/web_console/testing/fake_middleware.rb +++ b/lib/web_console/testing/fake_middleware.rb @@ -29,7 +29,7 @@ def view # 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) From 74aee15f3a2a0e73049c883c185a8c7eb00633bb Mon Sep 17 00:00:00 2001 From: mishina Date: Wed, 8 Jun 2022 22:55:24 +0900 Subject: [PATCH 04/14] Check `Rails.logger` is defined - ActionView is in use without Rails at `rake templates:test` --- lib/web_console.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From 2284e838af6dd1667bc1b70c3d48b0bfeae75edc Mon Sep 17 00:00:00 2001 From: mishina Date: Mon, 6 Jun 2022 00:10:48 +0900 Subject: [PATCH 05/14] `require "action_dispatch"` to use Mime::Type This fix the following error: ``` ActionView::Template::Error: undefined method `to_sym' for nil:NilClass @symbol = symbol.to_sym ^^^^^^^ ``` --- lib/web_console/testing/fake_middleware.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/web_console/testing/fake_middleware.rb b/lib/web_console/testing/fake_middleware.rb index 062b33c8..11eb3556 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" From 909140c7f17db04cb362d117ad81f45734e96179 Mon Sep 17 00:00:00 2001 From: mishina Date: Wed, 8 Jun 2022 17:00:16 +0900 Subject: [PATCH 06/14] Upgrade chai and mocha to the latest --- test/templates/package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/templates/package.json b/test/templates/package.json index e6994e9b..b0d51862 100644 --- a/test/templates/package.json +++ b/test/templates/package.json @@ -6,8 +6,8 @@ "url": "https://github.com/rails/web-console" }, "devDependencies": { - "chai": "^3.0.0", - "mocha": "^2.2.5", + "chai": "^4.3.6", + "mocha": "^10.0.0", "mocha-phantomjs-core": "^1.3.1" } } From ecd5d1d85420c651e637b6b0543250855521c9c4 Mon Sep 17 00:00:00 2001 From: mishina Date: Wed, 8 Jun 2022 22:58:12 +0900 Subject: [PATCH 07/14] Replace mocha-phantomjs-core with mocha-headless-chrome --- lib/web_console/tasks/templates.rake | 12 ++---------- test/templates/html/test_runner.html.erb | 6 +----- test/templates/package.json | 2 +- 3 files changed, 4 insertions(+), 16 deletions(-) diff --git a/lib/web_console/tasks/templates.rake b/lib/web_console/tasks/templates.rake index 101feb5c..9090baaa 100644 --- a/lib/web_console/tasks/templates.rake +++ b/lib/web_console/tasks/templates.rake @@ -12,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) } @@ -24,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 @@ -45,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/test/templates/html/test_runner.html.erb b/test/templates/html/test_runner.html.erb index f82809a1..888cc1f7 100644 --- a/test/templates/html/test_runner.html.erb +++ b/test/templates/html/test_runner.html.erb @@ -26,11 +26,7 @@ diff --git a/test/templates/package.json b/test/templates/package.json index b0d51862..c68574cb 100644 --- a/test/templates/package.json +++ b/test/templates/package.json @@ -8,6 +8,6 @@ "devDependencies": { "chai": "^4.3.6", "mocha": "^10.0.0", - "mocha-phantomjs-core": "^1.3.1" + "mocha-headless-chrome": "^4.0.0" } } From ae233a6ee3c2aeefb9140f10339821dea2a8f295 Mon Sep 17 00:00:00 2001 From: mishina Date: Wed, 8 Jun 2022 17:10:34 +0900 Subject: [PATCH 08/14] Stop pinning `mocha.reporter` to html This is workaround for direct-adv-interfaces/mocha-headless-chrome#52 --- test/templates/html/test_runner.html.erb | 1 - 1 file changed, 1 deletion(-) diff --git a/test/templates/html/test_runner.html.erb b/test/templates/html/test_runner.html.erb index 888cc1f7..a4ccb647 100644 --- a/test/templates/html/test_runner.html.erb +++ b/test/templates/html/test_runner.html.erb @@ -12,7 +12,6 @@ From 69a756e2e468ab5448d4e3d0fa1615a6c78cde7d Mon Sep 17 00:00:00 2001 From: mishina Date: Wed, 8 Jun 2022 23:05:39 +0900 Subject: [PATCH 09/14] Lowercase response headers The latest rack 3.0 (unreleased) does not accept uppercase headers. See rack/rack#1592 for more details. refs: - rack/rack#1812 - rails/sprockets#744 --- extensions/chrome/js/background.js | 4 ++-- lib/web_console/injector.rb | 6 +++--- lib/web_console/middleware.rb | 8 ++++---- lib/web_console/templates/console.js.erb | 4 ++-- lib/web_console/testing/fake_middleware.rb | 2 +- test/templates/config.ru | 8 ++++---- test/web_console/helper_test.rb | 2 +- test/web_console/injector_test.rb | 6 +++--- test/web_console/middleware_test.rb | 12 ++++++------ 9 files changed, 26 insertions(+), 26 deletions(-) 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/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/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 11eb3556..210dcf17 100644 --- a/lib/web_console/testing/fake_middleware.rb +++ b/lib/web_console/testing/fake_middleware.rb @@ -10,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) diff --git a/test/templates/config.ru b/test/templates/config.ru index 2dc93d1a..22890851 100644 --- a/test/templates/config.ru +++ b/test/templates/config.ru @@ -15,7 +15,7 @@ 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 @@ -42,19 +42,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/web_console/helper_test.rb b/test/web_console/helper_test.rb index 0a4cf1fb..ad901ba7 100644 --- a/test/web_console/helper_test.rb +++ b/test/web_console/helper_test.rb @@ -20,7 +20,7 @@ def status end def headers - { "Content-Type" => "text/html; charset=utf-8" } + { "content-type" => "text/html; charset=utf-8" } end def body diff --git a/test/web_console/injector_test.rb b/test/web_console/injector_test.rb index 277fa976..c522be03 100644 --- a/test/web_console/injector_test.rb +++ b/test/web_console/injector_test.rb @@ -28,11 +28,11 @@ class InjectorTest < ActiveSupport::TestCase assert_equal [ [ "foobar" ], {} ], Injector.new(body, {}).inject("bar") end - test "updates the Content-Length header" do + test "updates the content-length header" do body = [ "foo" ] - headers = { "Content-Length" => 3 } + headers = { "content-length" => 3 } - assert_equal [ [ "foobar" ], { "Content-Length" => "6" } ], Injector.new(body, headers).inject("bar") + assert_equal [ [ "foobar" ], { "content-length" => "6" } ], Injector.new(body, headers).inject("bar") end end end diff --git a/test/web_console/middleware_test.rb b/test/web_console/middleware_test.rb index b545e78c..c03774c1 100644 --- a/test/web_console/middleware_test.rb +++ b/test/web_console/middleware_test.rb @@ -35,8 +35,8 @@ def status def headers Hash.new.tap do |header_hash| - header_hash["Content-Type"] = "#{@response_content_type}; charset=utf-8" unless @response_content_type.nil? - header_hash["Content-Length"] = @response_content_length unless @response_content_length.nil? + header_hash["content-type"] = "#{@response_content_type}; charset=utf-8" unless @response_content_type.nil? + header_hash["content-length"] = @response_content_length unless @response_content_length.nil? end end end @@ -84,13 +84,13 @@ def headers assert_select "#console" end - test "sets correct Content-Length header" do + test "sets correct content-length header" do Thread.current[:__web_console_binding] = binding @app = Middleware.new(Application.new(response_content_length: 7)) get "/", params: nil - assert_equal(response.body.size, response.headers["Content-Length"].to_i) + assert_equal(response.body.size, response.headers["content-length"].to_i) end test "it closes original body if rendering console" do @@ -121,12 +121,12 @@ def headers assert_select "#console", 0 end - test "returns X-Web-Console-Session-Id as response header" do + test "returns x-web-console-session-id as response header" do Thread.current[:__web_console_binding] = binding get "/", params: nil - session_id = response.headers["X-Web-Console-Session-Id"] + session_id = response.headers["x-web-console-session-id"] assert_not Session.find(session_id).nil? end From a33dc8d568441a469e6f28de679de36c2bb64611 Mon Sep 17 00:00:00 2001 From: mishina Date: Wed, 8 Jun 2022 18:20:33 +0900 Subject: [PATCH 10/14] Set content-length header in the FakeMiddleware Similar change as rails/web-console#290 --- lib/web_console/testing/fake_middleware.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/web_console/testing/fake_middleware.rb b/lib/web_console/testing/fake_middleware.rb index 210dcf17..9e3488d5 100644 --- a/lib/web_console/testing/fake_middleware.rb +++ b/lib/web_console/testing/fake_middleware.rb @@ -19,7 +19,10 @@ 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 From 7e2dbe5d94a29bcd77d4605ddab8287a7fc92fe7 Mon Sep 17 00:00:00 2001 From: mishina Date: Tue, 24 May 2022 23:34:50 +0900 Subject: [PATCH 11/14] Remove mapping for /spec `test/templates/spec` is removed at rails/web-console#211 --- test/templates/config.ru | 7 ------- 1 file changed, 7 deletions(-) diff --git a/test/templates/config.ru b/test/templates/config.ru index 22890851..b89f8376 100644 --- a/test/templates/config.ru +++ b/test/templates/config.ru @@ -20,13 +20,6 @@ map "/html" do ) 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/(.*)}, From eb5f7a37f75f619258ec616a5f61e9b7c3e5ab03 Mon Sep 17 00:00:00 2001 From: mishina Date: Wed, 8 Jun 2022 23:13:30 +0900 Subject: [PATCH 12/14] Fix typo on test cases --- test/templates/test/repl_console_test.js | 4 ++-- test/web_console/exception_mapper_test.rb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/templates/test/repl_console_test.js b/test/templates/test/repl_console_test.js index 0a0c33ef..763cde06 100644 --- a/test/templates/test/repl_console_test.js +++ b/test/templates/test/repl_console_test.js @@ -1,4 +1,4 @@ -suite('REPLCosnole', function() { +suite('REPLConsole', function() { suiteSetup(function() { this.stage = document.createElement('div'); document.body.appendChild(this.stage); @@ -184,7 +184,7 @@ suite('REPLCosnole', function() { c.setInput('some'); setTimeout(function() { - c.onKeyDown(TestHelper.KeyDown(TestHelper.KEY_TAB)); + c.onKeyDown(TestHelper.keyDown(TestHelper.KEY_TAB)); assert.equal('something', c._input); }, 100); }); diff --git a/test/web_console/exception_mapper_test.rb b/test/web_console/exception_mapper_test.rb index 7e7947c2..f088fd2e 100644 --- a/test/web_console/exception_mapper_test.rb +++ b/test/web_console/exception_mapper_test.rb @@ -3,7 +3,7 @@ require "test_helper" module WebConsole - class ExcetionMapperTest < ActiveSupport::TestCase + class ExceptionMapperTest < ActiveSupport::TestCase test "#first tries to find the first application binding" do Rails.stubs(:root).returns Pathname(__FILE__).parent From 7e7fe9c0d4793ddd3a5aae23450c5891b66a445e Mon Sep 17 00:00:00 2001 From: mishina Date: Wed, 8 Jun 2022 16:45:00 +0900 Subject: [PATCH 13/14] Fix test that was not running We need `done` here --- test/templates/test/repl_console_test.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/templates/test/repl_console_test.js b/test/templates/test/repl_console_test.js index 763cde06..4d56f9e3 100644 --- a/test/templates/test/repl_console_test.js +++ b/test/templates/test/repl_console_test.js @@ -179,13 +179,14 @@ suite('REPLConsole', function() { }, 100); }); - test('inserts the current word if tab key is pressed', function() { + test('inserts the current word if tab key is pressed', function(done) { var c = this.console; c.setInput('some'); setTimeout(function() { c.onKeyDown(TestHelper.keyDown(TestHelper.KEY_TAB)); assert.equal('something', c._input); + done(); }, 100); }); }); From bdd06171a900928980e69fa01229554e8f660a39 Mon Sep 17 00:00:00 2001 From: mishina Date: Wed, 8 Jun 2022 19:08:29 +0900 Subject: [PATCH 14/14] Ignore `package-lock.json` --- .gitignore | 1 + 1 file changed, 1 insertion(+) 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/