1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
|
# frozen_string_literal: true
require "test_helper"
module WebConsole
class MiddlewareTest < ActionDispatch::IntegrationTest
class Application
def initialize(options = {})
@response_content_type = options.fetch(:response_content_type, "text/html")
@response_content_length = options.fetch(:response_content_length, nil)
end
def call(env)
[ status, headers, body ]
end
def body
@body ||= StringIO.new(<<-HTML.strip_heredoc)
<html>
<head>
<title>Hello world</title>
</head>
<body>
<p id="hello-world">Hello world</p>
</body>
</html>
HTML
end
private
def status
500
end
def headers
Hash.new.tap do |header_hash|
header_hash[Rack::CONTENT_TYPE] = "#{@response_content_type}; charset=utf-8" unless @response_content_type.nil?
header_hash[Rack::CONTENT_LENGTH] = @response_content_length unless @response_content_length.nil?
end
end
end
setup do
Thread.current[:__web_console_exception] = nil
Thread.current[:__web_console_binding] = nil
Rails.stubs(:root).returns Pathname(__FILE__).parent
Request.stubs(:permissions).returns(IPAddr.new("0.0.0.0/0"))
Middleware.mount_point = ""
@app = Middleware.new(Application.new)
end
test "render console in an html application from web_console.binding" do
Thread.current[:__web_console_binding] = binding
get "/", params: nil
assert_select "#console"
end
test "render console in an html application from web_console.exception" do
Thread.current[:__web_console_exception] = raise_exception
get "/", params: nil
assert_select "body > #console"
end
test "render error_page.js from web_console.exception" do
Thread.current[:__web_console_exception] = raise_exception
get "/", params: nil
assert_select "body > script[data-template=error_page]"
end
test "render console if response format is HTML" do
Thread.current[:__web_console_binding] = binding
@app = Middleware.new(Application.new(response_content_type: "text/html"))
get "/", params: nil
assert_select "#console"
end
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[Rack::CONTENT_LENGTH].to_i)
end
test "it closes original body if rendering console" do
Thread.current[:__web_console_binding] = binding
inner_app = Application.new(response_content_type: "text/html")
@app = Middleware.new(inner_app)
get "/", params: nil
assert(inner_app.body.closed?, "body should be closed")
end
test "does not render console if response format is empty" do
Thread.current[:__web_console_binding] = binding
@app = Middleware.new(Application.new(response_content_type: nil))
get "/", params: nil
assert_select "#console", 0
end
test "does not render console if response format is not HTML" do
Thread.current[:__web_console_binding] = binding
@app = Middleware.new(Application.new(response_content_type: "application/json"))
get "/", params: nil
assert_select "#console", 0
end
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"]
assert_not Session.find(session_id).nil?
end
test "doesn't render console in non html response" do
Thread.current[:__web_console_binding] = binding
@app = Middleware.new(Application.new(response_content_type: "application/json"))
get "/", params: nil
assert_select "#console", 0
end
test "doesn't render console from not allowed IP" do
Thread.current[:__web_console_binding] = binding
Request.stubs(:permissions).returns(IPAddr.new("127.0.0.1"))
silence(:stderr) do
get "/", params: nil, headers: { "REMOTE_ADDR" => "1.1.1.1" }
end
assert_select "#console", 0
end
test "doesn't render console without a web_console.binding or web_console.exception" do
get "/", params: nil
assert_select "#console", 0
end
test "can evaluate code and return it as a JSON" do
session, line = Session.new([[binding]]), __LINE__
Session.stubs(:from).returns(session)
get "/", params: nil
put "/repl_sessions/#{session.id}", xhr: true, params: { input: "line" }
assert_equal("=> #{line}\n", JSON.parse(response.body)["output"])
end
test "can switch bindings on error pages" do
session = Session.new([WebConsole::ExceptionMapper.new(raise_exception)])
Session.stubs(:from).returns(session)
get "/", params: nil
post "/repl_sessions/#{session.id}/trace", xhr: true, params: { frame_id: 1 }
assert_equal({ ok: true }.to_json, response.body)
end
test "can switch to the cause on error pages" do
nested_error = begin
raise "First error"
rescue
raise "Second Error" rescue $!
end
session = Session.new(WebConsole::ExceptionMapper.follow(nested_error))
Session.stubs(:from).returns(session)
get "/", params: nil
post "/repl_sessions/#{session.id}/trace", xhr: true, params: { frame_id: 1, exception_object_id: nested_error.cause.object_id }
assert_equal({ ok: true }.to_json, response.body)
end
test "can be changed mount point" do
Middleware.mount_point = "/customized/path"
session, value = Session.new([[binding]]), __LINE__
put "/customized/path/repl_sessions/#{session.id}", params: { input: "value" }, xhr: true
assert_equal("=> #{value}\n", JSON.parse(response.body)["output"])
end
test "can return context information by passing a context param" do
hello = hello = "world"
session = Session.new([[binding]])
Session.stubs(:from).returns(session)
get "/"
put "/repl_sessions/#{session.id}", xhr: true, params: { context: "" }
assert_includes(JSON.parse(response.body)["context"], local_variables.map(&:to_s))
end
test "unavailable sessions respond to the user with a message" do
put "/repl_sessions/no_such_session", xhr: true, params: { input: "__LINE__" }
assert_equal(404, response.status)
end
test "unavailable sessions can occur on binding switch" do
post "/repl_sessions/no_such_session/trace", xhr: true, params: { frame_id: 1 }
assert_equal(404, response.status)
end
test "reraises application errors" do
@app = proc { raise }
assert_raises(RuntimeError) { get "/" }
end
test "logs internal errors with Rails.logger" do
io = StringIO.new
logger = ActiveSupport::Logger.new(io)
old_logger, Rails.logger = Rails.logger, logger
begin
@app.stubs(:call_app).raises("whoops")
get "/"
rescue RuntimeError
output = io.rewind && io.read
lines = output.lines
assert_equal ["\n", "RuntimeError: whoops\n"], lines.slice!(0, 2)
ensure
Rails.logger = old_logger
end
end
private
def raise_exception
raise
rescue => exc
exc
end
end
end
|