Skip to content
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
10 changes: 10 additions & 0 deletions lib/utopia/redirection/directory_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ def initialize(app, index: "index")
super(app)
end

# Freeze this object and its internal state.
# @returns [self] This object.
def freeze
return self if frozen?

@index.freeze

return super
end

# Redirect a directory path to its index path.
# @parameter path [String] The normalized request path.
# @returns [Protocol::HTTP::Response | Nil] The redirect response when the path ends with `/`.
Expand Down
12 changes: 12 additions & 0 deletions lib/utopia/redirection/moved.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ def initialize(app, pattern, prefix, status: 301, flatten: false)
super(app, status: status)
end

# Freeze this object and its internal state.
# @returns [self] This object.
def freeze
return self if frozen?

@pattern.freeze
@prefix.freeze
@flatten.freeze

return super
end

# Redirect a matching path to the configured prefix.
# @parameter path [String] The normalized request path.
# @returns [Protocol::HTTP::Response | Nil] The redirect response when the pattern matches.
Expand Down
10 changes: 10 additions & 0 deletions lib/utopia/redirection/rewrite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ def initialize(app, patterns, status: 301)
super(app, status: status)
end

# Freeze this object and its internal state.
# @returns [self] This object.
def freeze
return self if frozen?

@patterns.freeze

return super
end

# Redirect a path found in the rewrite map.
# @parameter path [String] The normalized request path.
# @returns [Protocol::HTTP::Response | Nil] The redirect response when the path is mapped.
Expand Down
32 changes: 32 additions & 0 deletions test/utopia/redirection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,38 @@ def tracked_body(name, events)
return body
end

it "freezes directory index configuration" do
index = +"index"
middleware = Utopia::Redirection::DirectoryIndex.new(Protocol::HTTP::Middleware::NotFound, index: index)

middleware.freeze

expect(middleware).to be(:frozen?)
expect(index).to be(:frozen?)
end

it "freezes rewrite configuration" do
patterns = {"/old" => "/new"}
middleware = Utopia::Redirection::Rewrite.new(Protocol::HTTP::Middleware::NotFound, patterns)

middleware.freeze

expect(middleware).to be(:frozen?)
expect(patterns).to be(:frozen?)
end

it "freezes moved configuration" do
pattern = +"/old"
prefix = +"/new"
middleware = Utopia::Redirection::Moved.new(Protocol::HTTP::Middleware::NotFound, pattern, prefix)

middleware.freeze

expect(middleware).to be(:frozen?)
expect(pattern).to be(:frozen?)
expect(prefix).to be(:frozen?)
end

let(:middleware) do
Utopia::Application.build(Protocol::HTTP::Middleware.for{|request|
case request.path_info
Expand Down
Loading