-
-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7d91f1f
commit 0e96640
Showing
4 changed files
with
243 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
require "../spec_helper" | ||
|
||
include ContextHelper | ||
|
||
abstract class BaseAction < Lucky::Action | ||
include Lucky::Subdomain | ||
accepted_formats [:html], default: :html | ||
end | ||
|
||
class Simple::Index < BaseAction | ||
require_subdomain | ||
|
||
get "/simple" do | ||
plain_text subdomain | ||
end | ||
end | ||
|
||
class OptionalSubdomain::Index < BaseAction | ||
get "/optional" do | ||
plain_text subdomain? || "none" | ||
end | ||
end | ||
|
||
class Specific::Index < BaseAction | ||
require_subdomain "foo" | ||
|
||
get "/specific" do | ||
plain_text subdomain | ||
end | ||
end | ||
|
||
class Regex::Index < BaseAction | ||
require_subdomain /www\d/ | ||
|
||
get "/regex" do | ||
plain_text subdomain | ||
end | ||
end | ||
|
||
class Multiple::Index < BaseAction | ||
require_subdomain ["test", "staging", /(prod|production)/] | ||
|
||
get "/multiple" do | ||
plain_text subdomain | ||
end | ||
end | ||
|
||
describe Lucky::Subdomain do | ||
it "handles general subdomain expectation" do | ||
request = build_request(host: "foo.example.com") | ||
response = Simple::Index.new(build_context(request), params).call | ||
response.body.should eq "foo" | ||
end | ||
|
||
it "handles optional subdomain" do | ||
request = build_request(host: "qa.example.com") | ||
response = OptionalSubdomain::Index.new(build_context(request), params).call | ||
response.body.should eq "qa" | ||
|
||
request = build_request(host: "example.com") | ||
response = OptionalSubdomain::Index.new(build_context(request), params).call | ||
response.body.should eq "none" | ||
end | ||
|
||
it "raises error if subdomain missing" do | ||
request = build_request(host: "example.com") | ||
expect_raises(Lucky::InvalidSubdomainError) do | ||
Simple::Index.new(build_context(request), params).call | ||
end | ||
end | ||
|
||
it "handles specific subdomain expectation" do | ||
request = build_request(host: "foo.example.com") | ||
response = Specific::Index.new(build_context(request), params).call | ||
response.body.should eq "foo" | ||
end | ||
|
||
it "raises error if subdomain does not match specific" do | ||
request = build_request(host: "admin.example.com") | ||
expect_raises(Lucky::InvalidSubdomainError) do | ||
Specific::Index.new(build_context(request), params).call | ||
end | ||
end | ||
|
||
it "handles regex subdomain expectation" do | ||
request = build_request(host: "www4.example.com") | ||
response = Regex::Index.new(build_context(request), params).call | ||
response.body.should eq "www4" | ||
end | ||
|
||
it "raises error if subdomain does not match regex" do | ||
request = build_request(host: "4www.example.com") | ||
expect_raises(Lucky::InvalidSubdomainError) do | ||
Regex::Index.new(build_context(request), params).call | ||
end | ||
end | ||
|
||
it "handles multiple options for expectation" do | ||
request = build_request(host: "test.example.com") | ||
response = Multiple::Index.new(build_context(request), params).call | ||
response.body.should eq "test" | ||
|
||
request = build_request(host: "staging.example.com") | ||
response = Multiple::Index.new(build_context(request), params).call | ||
response.body.should eq "staging" | ||
|
||
request = build_request(host: "prod.example.com") | ||
response = Multiple::Index.new(build_context(request), params).call | ||
response.body.should eq "prod" | ||
|
||
request = build_request(host: "production.example.com") | ||
response = Multiple::Index.new(build_context(request), params).call | ||
response.body.should eq "production" | ||
end | ||
|
||
it "raises error if subdomain does not match any expectations" do | ||
request = build_request(host: "development.example.com") | ||
expect_raises(Lucky::InvalidSubdomainError) do | ||
Multiple::Index.new(build_context(request), params).call | ||
end | ||
end | ||
|
||
it "has configuration for urls with larger tld length" do | ||
Lucky::Subdomain.temp_config(tld_length: 2) do | ||
request = build_request(host: "foo.example.co.uk") | ||
response = Simple::Index.new(build_context(request), params).call | ||
response.body.should eq "foo" | ||
end | ||
end | ||
|
||
it "will fail if using ip address" do | ||
request = build_request(host: "development.127.0.0.1:3000") | ||
expect_raises(Lucky::InvalidSubdomainError) do | ||
Simple::Index.new(build_context(request), params).call | ||
end | ||
end | ||
|
||
it "will not fail if using localhost and port with tld length set to 0" do | ||
Lucky::Subdomain.temp_config(tld_length: 0) do | ||
request = build_request(host: "foo.locahost:3000") | ||
response = Simple::Index.new(build_context(request), params).call | ||
response.body.should eq "foo" | ||
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 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,77 @@ | ||
module Lucky::Subdomain | ||
# Taken from https://github.com/rails/rails/blob/afc6abb674b51717dac39ea4d9e2252d7e40d060/actionpack/lib/action_dispatch/http/url.rb#L8 | ||
IP_HOST_REGEXP = /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/ | ||
|
||
Habitat.create do | ||
# tld_length is the number of Top Level Domain segments separated by periods | ||
# the default is 1 because most domains end in ".com" or ".org" | ||
# The tld_length should be changed to 2 when you have a ".co.uk" domain for example | ||
# It can also be changed to 0 for local development so that you can use `tenant.localhost:3000` | ||
setting tld_length : Int32 = 1 | ||
end | ||
|
||
alias Matcher = String | Regex | Bool | Array(String | Regex) | Array(String) | Array(Regex) | ||
|
||
# Sets up a subdomain requirement for an action | ||
# | ||
# ``` | ||
# require_subdomain # subdomain required but can be anything | ||
# require_subdomain "admin" # subdomain required and must equal "admin" | ||
# require_subdomain /(dev|qa|prod)/ # subdomain required and must match regex | ||
# require_subdomain ["tenant1", "tenant2", /tenant\d/] # subdomain required and must match one of the items in the array | ||
# ``` | ||
# | ||
# The subdomain can then be accessed from within the route block by calling `subdomain`. | ||
# | ||
# If you don't want to require a subdomain but still want to check if one is passed | ||
# you can still call `subdomain?` without using `require_subdomain`. | ||
# Just know that `subdomain?` is nilable. | ||
macro require_subdomain(matcher = true) | ||
before _match_subdomain | ||
|
||
private def subdomain : String | ||
subdomain?.not_nil! | ||
end | ||
|
||
private def _match_subdomain | ||
_match_subdomain({{ matcher }}) | ||
end | ||
end | ||
|
||
private def subdomain : String | ||
{% raise "No subdomain available without calling `require_subdomain` first." %} | ||
end | ||
|
||
private def subdomain? : String? | ||
host = request.hostname | ||
return if host.nil? || IP_HOST_REGEXP.matches?(host) | ||
|
||
parts = host.split('.') | ||
parts.pop(settings.tld_length + 1) | ||
|
||
parts.empty? ? nil : parts.join(".") | ||
end | ||
|
||
private def _match_subdomain(matcher : Matcher) | ||
expected = [matcher].flatten.compact | ||
return continue if expected.empty? | ||
|
||
actual = subdomain? | ||
result = expected.any? do |expected_subdomain| | ||
case expected_subdomain | ||
when true | ||
actual.present? | ||
when Symbol | ||
actual.to_s == expected_subdomain.to_s | ||
else | ||
expected_subdomain === actual | ||
end | ||
end | ||
|
||
if result | ||
continue | ||
else | ||
raise InvalidSubdomainError.new(host: request.hostname, expected: matcher) | ||
end | ||
end | ||
end |