mirror of
https://github.com/discourse/discourse.git
synced 2025-05-28 23:49:34 +08:00
DEV: Improve multisite testing (#14884)
This commit adds the RailsMultisite middleware in test mode when Rails.configuration.multisite is true. This allows for much more realistic integration testing. The `multisite_spec.rb` file is rewritten to avoid needing to simulate a middleware stack.
This commit is contained in:
@ -2,62 +2,33 @@
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
describe 'multisite', type: :multisite do
|
||||
class DBNameMiddleware
|
||||
def initialize(app, config = {})
|
||||
@app = app
|
||||
end
|
||||
|
||||
def call(env)
|
||||
# note current_db is already being ruined on boot cause its not multisite
|
||||
[200, {}, [RailsMultisite::ConnectionManagement.current_hostname]]
|
||||
end
|
||||
end
|
||||
|
||||
let :session do
|
||||
stack = ActionDispatch::MiddlewareStack.new
|
||||
stack.use RailsMultisite::Middleware, RailsMultisite::DiscoursePatches.config
|
||||
stack.use DBNameMiddleware
|
||||
|
||||
routes = ActionDispatch::Routing::RouteSet.new
|
||||
stack.build(routes)
|
||||
end
|
||||
|
||||
describe 'multisite', type: [:multisite, :request] do
|
||||
it "should always allow /srv/status through" do
|
||||
headers = {
|
||||
"HTTP_HOST" => "unknown.com",
|
||||
"REQUEST_METHOD" => "GET",
|
||||
"PATH_INFO" => "/srv/status",
|
||||
"rack.input" => StringIO.new
|
||||
}
|
||||
|
||||
code, _, body = session.call(headers)
|
||||
expect(code).to eq(200)
|
||||
expect(body.join).to eq("test.localhost")
|
||||
get "http://unknown.com/srv/status"
|
||||
expect(response.status).to eq(200)
|
||||
expect(request.env["HTTP_HOST"]).to eq("test.localhost") # Rewritten by EnforceHostname middleware
|
||||
end
|
||||
|
||||
it "should 404 on unknown routes" do
|
||||
headers = {
|
||||
"HTTP_HOST" => "unknown.com",
|
||||
"REQUEST_METHOD" => "GET",
|
||||
"PATH_INFO" => "/topics",
|
||||
"rack.input" => StringIO.new
|
||||
}
|
||||
|
||||
code, _ = session.call(headers)
|
||||
expect(code).to eq(404)
|
||||
it "should 404 for unknown domains" do
|
||||
get "http://unknown.com/about.json"
|
||||
expect(response.status).to eq(404)
|
||||
end
|
||||
|
||||
it "should hit correct site elsewise" do
|
||||
headers = {
|
||||
"HTTP_HOST" => "test2.localhost",
|
||||
"REQUEST_METHOD" => "GET",
|
||||
"PATH_INFO" => "/topics",
|
||||
"rack.input" => StringIO.new
|
||||
}
|
||||
it "should hit correct site otherwise" do
|
||||
site_1_url = Fabricate(:topic, title: "Site 1 Topic Title", user: Discourse.system_user).relative_url
|
||||
|
||||
code, _, body = session.call(headers)
|
||||
expect(code).to eq(200)
|
||||
expect(body.join).to eq("test2.localhost")
|
||||
test_multisite_connection('second') do
|
||||
site_2_url = Fabricate(:topic, title: "Site 2 Topic Title", user: Discourse.system_user).relative_url
|
||||
|
||||
get "http://test.localhost/#{site_1_url}.json"
|
||||
expect(request.env["RAILS_MULTISITE_HOST"]).to eq("test.localhost")
|
||||
expect(response.status).to eq(200)
|
||||
expect(response.parsed_body["title"]).to eq("Site 1 Topic Title")
|
||||
|
||||
get "http://test2.localhost/#{site_2_url}.json"
|
||||
expect(response.status).to eq(200)
|
||||
expect(request.env["RAILS_MULTISITE_HOST"]).to eq("test2.localhost")
|
||||
expect(response.parsed_body["title"]).to eq("Site 2 Topic Title")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user