2017-11-24 12:31:23 +08:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe Hijack do
|
2017-11-27 14:43:24 +08:00
|
|
|
class Hijack::Tester < ApplicationController
|
2017-11-24 12:31:23 +08:00
|
|
|
attr_reader :io
|
|
|
|
|
|
|
|
include Hijack
|
2017-11-27 14:43:24 +08:00
|
|
|
|
2017-11-24 12:31:23 +08:00
|
|
|
def initialize
|
|
|
|
@io = StringIO.new
|
2017-11-27 14:43:24 +08:00
|
|
|
self.request = ActionController::TestRequest.new(
|
2017-11-24 12:31:23 +08:00
|
|
|
{ "rack.hijack" => lambda { @io } },
|
|
|
|
nil,
|
|
|
|
nil
|
|
|
|
)
|
2017-11-27 14:43:24 +08:00
|
|
|
# we need this for the 418
|
|
|
|
self.response = ActionDispatch::Response.new
|
2017-11-24 12:31:23 +08:00
|
|
|
end
|
|
|
|
|
2017-11-27 14:43:24 +08:00
|
|
|
def hijack_test(&blk)
|
|
|
|
hijack(&blk)
|
2017-11-24 12:31:23 +08:00
|
|
|
end
|
2017-11-27 14:43:24 +08:00
|
|
|
|
2017-11-24 12:31:23 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
let :tester do
|
|
|
|
Hijack::Tester.new
|
|
|
|
end
|
|
|
|
|
2017-11-27 14:43:24 +08:00
|
|
|
it "handles expires_in" do
|
|
|
|
tester.hijack_test do
|
|
|
|
expires_in 1.year
|
|
|
|
render body: "hello world", status: 402
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(tester.io.string).to include("max-age=31556952")
|
|
|
|
end
|
|
|
|
|
2017-11-24 12:31:23 +08:00
|
|
|
it "renders non 200 status if asked for" do
|
|
|
|
tester.hijack_test do
|
|
|
|
render body: "hello world", status: 402
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(tester.io.string).to include("402")
|
|
|
|
expect(tester.io.string).to include("world")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "renders stuff correctly if it works" do
|
|
|
|
tester.hijack_test do
|
|
|
|
render plain: "hello world"
|
|
|
|
end
|
|
|
|
|
2017-11-27 14:43:24 +08:00
|
|
|
result = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 11\r\nConnection: close\r\n\r\nhello world"
|
2017-11-24 12:31:23 +08:00
|
|
|
expect(tester.io.string).to eq(result)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns 500 by default" do
|
|
|
|
tester.hijack_test
|
|
|
|
|
2017-11-27 14:43:24 +08:00
|
|
|
expected = "HTTP/1.1 500 Internal Server Error\r\nContent-Type: text/html\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"
|
2017-11-24 12:31:23 +08:00
|
|
|
expect(tester.io.string).to eq(expected)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not run the block if io is closed" do
|
|
|
|
tester.io.close
|
|
|
|
|
|
|
|
ran = false
|
|
|
|
tester.hijack_test do
|
|
|
|
ran = true
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(ran).to eq(false)
|
|
|
|
end
|
|
|
|
end
|