Skip to content

Commit

Permalink
Merge pull request #250 from wagenet/close-spec
Browse files Browse the repository at this point in the history
Close original body to comply with Rack SPEC
  • Loading branch information
guilleiguaran authored Dec 11, 2017
2 parents ec96734 + 1074dbf commit 986c069
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
3 changes: 3 additions & 0 deletions lib/web_console/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ class Response < Struct.new(:body, :status, :headers)
def write(content)
raw_body = Array(body).first.to_s

# We're done with the original body object, so make sure to close it to comply with the Rack SPEC
body.close if body.respond_to?(:close)

if position = raw_body.rindex('</body>')
raw_body.insert(position, content)
else
Expand Down
36 changes: 23 additions & 13 deletions test/web_console/middleware_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ 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
Expand All @@ -26,19 +39,6 @@ def headers
{}
end
end

def body
Array(<<-HTML.strip_heredoc)
<html>
<head>
<title>Hello world</title>
</head>
<body>
<p id="hello-world">Hello world</p>
</body>
</html>
HTML
end
end

setup do
Expand Down Expand Up @@ -84,6 +84,16 @@ def body
assert_select '#console'
end

test 'it closes original body if rendering console' do
Thread.current[:__web_console_binding] = binding
inner_app = Application.new(response_content_type: Mime[: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))
Expand Down

0 comments on commit 986c069

Please sign in to comment.