Skip to content

Commit 7a01a41

Browse files
Fix default application options.
Assisted-By: devx/b3414b50-d642-461b-95a8-8f773c4d087b
1 parent f6152f6 commit 7a01a41

2 files changed

Lines changed: 46 additions & 5 deletions

File tree

lib/utopia/application.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,9 @@ def self.build(default_app = Response::NotFound, &block)
3737
end
3838

3939
# Build the default Utopia application.
40-
# @parameter options [Hash] Options passed to the application constructor.
4140
# @returns [Application] The default protocol-facing Utopia application.
42-
def self.default(**options)
43-
self.build(**options)
41+
def self.default
42+
self.build
4443
end
4544

4645
# Load a Utopia application from a conventional configuration file.
@@ -69,7 +68,7 @@ def self.load(path = PATH, **options)
6968
end
7069
end
7170

72-
return self.default(**options)
71+
return self.default
7372
end
7473

7574
# Process a protocol HTTP request.

test/utopia/application.rb

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ def response_object.to_response
5959
expect(response.status).to be == 404
6060
end
6161

62+
it "rejects options for the default application" do
63+
expect do
64+
subject.default(unexpected: true)
65+
end.to raise_exception(ArgumentError)
66+
end
67+
6268
it "loads a top-level application constant" do
6369
Dir.mktmpdir do |directory|
6470
path = File.join(directory, "application.rb")
@@ -80,6 +86,42 @@ def response_object.to_response
8086
end
8187
end
8288

89+
it "passes options to application classes" do
90+
Dir.mktmpdir do |directory|
91+
path = File.join(directory, "application.rb")
92+
93+
File.write(path, <<~RUBY)
94+
require "utopia/application"
95+
96+
class Application < Utopia::Application
97+
def initialize(message:)
98+
delegate = Protocol::HTTP::Middleware.for do |request|
99+
Utopia::Response.text(message)
100+
end
101+
102+
super(delegate)
103+
end
104+
end
105+
RUBY
106+
107+
application = subject.load(path, message: "Hello")
108+
response = application.call(http_request)
109+
110+
expect(response.status).to be == 200
111+
expect(response.read).to be == "Hello"
112+
end
113+
end
114+
115+
it "uses the default application if the configuration file does not exist" do
116+
Dir.mktmpdir do |directory|
117+
path = File.join(directory, "missing.rb")
118+
application = subject.load(path, ignored: true)
119+
response = application.call(http_request)
120+
121+
expect(response.status).to be == 404
122+
end
123+
end
124+
83125
it "uses the default application if no application constant is defined" do
84126
Dir.mktmpdir do |directory|
85127
path = File.join(directory, "application.rb")
@@ -88,7 +130,7 @@ def response_object.to_response
88130
require "utopia/application"
89131
RUBY
90132

91-
application = subject.load(path)
133+
application = subject.load(path, ignored: true)
92134
response = application.call(http_request)
93135

94136
expect(response.status).to be == 404

0 commit comments

Comments
 (0)