forked from rails/web-console
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rely only on methods defined by the Rack body spec while injecting
I cannot be sure that every body can be coerced as an array, the only thing we can rely on is that all the bodies will respond to each. This fixes rails#252.
- Loading branch information
1 parent
2a05188
commit f9c348f
Showing
5 changed files
with
60 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
module WebConsole | ||
# Injects content into a Rack body. | ||
class Injector | ||
def initialize(body) | ||
@body = "".dup | ||
|
||
body.each { |part| @body << part } | ||
body.close if body.respond_to?(:close) | ||
end | ||
|
||
def inject(content) | ||
if position = @body.rindex("</body>") | ||
[ @body.insert(position, content) ] | ||
else | ||
[ @body << content ] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
module WebConsole | ||
class InjectorTest < ActiveSupport::TestCase | ||
test "closes body if closable" do | ||
closed = false | ||
|
||
body = [ "foo" ] | ||
body.define_singleton_method(:close) { closed = true } | ||
|
||
assert_equal [ "foobar" ], Injector.new(body).inject("bar") | ||
assert closed | ||
end | ||
|
||
test "support fancy bodies like Rack::BodyProxy" do | ||
closed = false | ||
body = Rack::BodyProxy.new([ "foo" ]) { closed = true } | ||
|
||
assert_equal [ "foobar" ], Injector.new(body).inject("bar") | ||
assert closed | ||
end | ||
|
||
test "support fancy bodies like ActionDispatch::Response::RackBody" do | ||
body = ActionDispatch::Response.create(200, {}, [ "foo" ]).to_a.last | ||
|
||
assert_equal [ "foobar" ], Injector.new(body).inject("bar") | ||
end | ||
end | ||
end |