Skip to content

Commit

Permalink
Merge pull request #91 from rage-rb/multi-rails-internal
Browse files Browse the repository at this point in the history
Improve `Rage.multi_application`
  • Loading branch information
rsamoilov authored Jul 14, 2024
2 parents 3e638ed + 617f0bc commit e0e7c1c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/rage/router/util.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class Rage::Router::Util
class << self
# converts controller name in a path form into a class
Expand Down Expand Up @@ -40,9 +42,13 @@ def initialize(rage_app, rails_app)

def call(env)
result = @rage_app.call(env)
return result if result[0] == :__http_defer__ || result[1]["X-Cascade".freeze] != "pass".freeze
return result if result[0] == :__http_defer__

@rails_app.call(env)
if result[1]["X-Cascade"] == "pass" || env["PATH_INFO"].start_with?("/rails/")
@rails_app.call(env)
else
result
end
end
end
end
53 changes: 53 additions & 0 deletions spec/multi_application_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# frozen_string_literal: true

RSpec.describe "Rage Multi App" do
subject { Rage.multi_application.call(env) }

let(:env) { { "PATH_INFO"=> "/" } }
let(:rails_verifier) { double }
let(:rage_verifier) { double }

before do
stub_const("Rails", double(application: rails_verifier))
allow(Rage).to receive(:application).and_return(rage_verifier)
end

context "with a 200 response" do
it "calls Rage app" do
expect(rage_verifier).to receive(:call).with(env).and_return([200, {}, []])
subject
end
end

context "with a 404 response" do
it "calls Rage app" do
expect(rage_verifier).to receive(:call).with(env).and_return([404, {}, []])
subject
end
end

context "with an async response" do
it "calls Rage app" do
expect(rage_verifier).to receive(:call).with(env).and_return([:__http_defer__, Fiber.new {}])
subject
end
end

context "with an X-Cascade response" do
it "calls both Rage and Rails apps" do
expect(rage_verifier).to receive(:call).with(env).and_return([200, { "X-Cascade" => "pass" }, []])
expect(rails_verifier).to receive(:call).with(env)
subject
end
end

context "with Rails internal request" do
let(:env) { { "PATH_INFO"=> "/rails/action_mailbox" } }

it "calls both Rage and Rails apps" do
expect(rage_verifier).to receive(:call).with(env).and_return([200, {}, []])
expect(rails_verifier).to receive(:call).with(env)
subject
end
end
end

0 comments on commit e0e7c1c

Please sign in to comment.