Skip to content

Instantly share code, notes, and snippets.

@JoeDupuis
Created June 12, 2023 02:58
Show Gist options
  • Save JoeDupuis/ae3733b265f07d505890568b7c1601a2 to your computer and use it in GitHub Desktop.
Save JoeDupuis/ae3733b265f07d505890568b7c1601a2 to your computer and use it in GitHub Desktop.
Streaming reponse rack test
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails", path: '../../rails'
gem "rack", "~> 2.0"
end
require "action_controller/railtie"
ENV["RAILS_ENV"] ||= "test"
class TestApp < Rails::Application
config.root = __dir__
config.hosts << "example.org"
config.host_authorization = { exclude: ->(request) { request.path =~ /.*/ } }
secrets.secret_key_base = "secret_key_base"
config.logger = Logger.new($stdout)
Rails.logger = config.logger
routes.draw do
get "/streaming" => "streaming#index"
end
end
class StreamingController < ActionController::Base
include ActionController::Live
def index
response.headers["Last-Modified"] = Time.now.httpdate
response.stream.write "hello world\n"
ensure
response.stream.close
end
end
require "minitest/autorun"
require "rack/test"
class BugTest < Minitest::Test
def test_streaming
status, headers, body = app.call(env)
debugger
assert status == 200
refute body.respond_to?(:to_ary)
buffer = []
body.each {|chunk| buffer << chunk }
assert buffer.join.include? "hello world"
end
private
def env
{
"REQUEST_METHOD"=>"GET",
"HTTP_VERSION"=>"HTTP/1.1",
"PATH_INFO"=>"/streaming",
"REQUEST_URI"=>"/streaming",
"HTTP_HOST"=>"www.example.com",
'REMOTE_ADDR' => '127.0.0.1',
'rack.input' => StringIO.new
}
end
def app
Rails.application
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment