Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return 404 HEAD response by default #7

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,14 @@ StrongRoutes.config.allowed_routes = [ /\A\//i, /\A\/posts/i ]
Any routes that aren't allowed will return a 404 by default:

```
[ 404, { "Content-Type" => "text/html", "Content-Length" => "18" }, [ "Resource Not Found" ] ]
[ 404, {}, [] ]
```

The message that is returned can be specified using the `message` config option:
The content that is returned can be specified using the `content` config option:

```Ruby
StrongRoutes.config.message = File.read(Rails.root.join('public/404.html'))
StrongRoutes.config.content = File.read(Rails.root.join('public/404.html'))
StrongRoutes.config.content_type = "text/html"
```

## Contributing
Expand Down
4 changes: 2 additions & 2 deletions lib/strong_routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ def self.config
@config ||= Config.new
end

def self.enabled?
config.enabled?
def self.response
Rack::Response[config.status, config.headers, config.content]
end

# Initialize the config
Expand Down
2 changes: 1 addition & 1 deletion lib/strong_routes/allow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def call(env)
if StrongRoutes.allowed?(env[Rack::PATH_INFO].to_s)
@app.call(env)
else
[404, {"Content-Type" => "text/html", "Content-Length" => config.message.length.to_s}, [config.message]]
StrongRoutes.response
end
end

Expand Down
21 changes: 18 additions & 3 deletions lib/strong_routes/config.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
# frozen_string_literal: true

module StrongRoutes
class Config
attr_accessor :enabled,
:content,
:content_type,
:insert_after,
:insert_before,
:message
:insert_before

attr_reader :allowed_routes, :route_matchers
attr_writer :status

def initialize
@allowed_routes = Set.new
@enabled = true
@message = "Resource Not Found"
@route_matchers = []
end

Expand All @@ -24,12 +27,24 @@ def enabled?
!!enabled
end

def headers
if content.present?
{Rack::CONTENT_TYPE => content_type || "text/plain"}
else
{}
end
end

def insert_after?
!!insert_after
end

def insert_before?
!!insert_before
end

def status
@status ||= 404
end
end
end
54 changes: 50 additions & 4 deletions test/strong_routes/config_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
_(subject.respond_to?(:allowed_routes)).must_equal true
end

it "supports :content" do
_(subject.respond_to?(:content)).must_equal true
end

it "supports :content_type" do
_(subject.respond_to?(:content_type)).must_equal true
end

it "supports :enabled" do
_(subject.respond_to?(:enabled)).must_equal true
end
Expand All @@ -27,11 +35,9 @@
it "supports :insert_before?" do
_(subject.respond_to?(:insert_before?)).must_equal true
end
end

describe "#enabled" do
it "is enabled by default" do
_(subject).must_be :enabled
it "supports :status=" do
_(subject.respond_to?(:status=)).must_equal true
end
end

Expand All @@ -41,4 +47,44 @@
_(subject.route_matchers).must_equal [StrongRoutes::RouteMatcher.new("/users")]
end
end

describe "#enabled?" do
it "is true" do
_(subject).must_be :enabled
end
end

describe "#headers" do
it "is an empty hash" do
_(subject.headers).must_equal({})
end

context "with content_type: 'text/plain'" do
it "is an empty hash" do
subject.content_type = "text/plain"
_(subject.headers).must_equal({})
end
end

context "with content: 'Nope' and content_type: nil" do
it "contains Content-Type headers" do
subject.content = "Nope"
_(subject.headers).must_equal({"content-type" => "text/plain"})
end
end

context "with content: 'Nope' and content_type: 'text/plain'" do
it "contains Content-Type headers" do
subject.content = "Nope"
subject.content_type = "text/plain"
_(subject.headers).must_equal({"content-type" => "text/plain"})
end
end
end

describe "#status" do
it "is 404" do
_(subject.status).must_equal 404
end
end
end
Loading