@@ -134,16 +134,42 @@ def run_startup_tasks name, logger: nil, lenient: false
134134 #
135135 def call_http name , request , globals : nil , logger : nil
136136 globals ||= run_startup_tasks name , logger : logger , lenient : true
137- function = Testing . current_registry [ name ]
138- case function &.type
139- when :http
137+ Testing . call :http , name , readable_name : "HTTP" do |function |
140138 Testing . interpret_response do
141139 function . call request , globals : globals , logger : logger
142140 end
143- when nil
144- raise "Unknown function name #{ name } "
145- else
146- raise "Function #{ name } is not an HTTP function"
141+ end
142+ end
143+
144+ ##
145+ # Call the given Typed function for testing. The underlying function must
146+ # be of type `:typed`. Returns the Rack response.
147+ #
148+ # By default, the startup tasks will be run for the given function if they
149+ # have not already been run. You can, however, disable running startup
150+ # tasks by providing an explicit globals hash.
151+ #
152+ # By default, the {FunctionsFramework.logger} will be used, but you can
153+ # override that by providing your own logger. In particular, to disable
154+ # logging, you can pass `Logger.new(nil)`.
155+ #
156+ # @param name [String] The name of the function to call
157+ # @param request [Rack::Request] The Rack request to send
158+ # @param globals [Hash] Do not run startup tasks, and instead provide the
159+ # globals directly. Optional.
160+ # @param logger [Logger] Use the given logger instead of the Functions
161+ # Framework's global logger. Optional.
162+ # @return [Rack::Response]
163+ #
164+ def call_typed name , request , globals : nil , logger : nil
165+ globals ||= run_startup_tasks name , logger : logger , lenient : true
166+ Testing . call :typed , name , readable_name : "Typed" do |function |
167+ Testing . interpret_response do
168+ config = FunctionsFramework ::Server ::Config . new
169+ config . logger = logger
170+ app = FunctionsFramework ::Server ::TypedApp . new function , globals , config
171+ app . call request . env
172+ end
147173 end
148174 end
149175
@@ -169,15 +195,9 @@ def call_http name, request, globals: nil, logger: nil
169195 #
170196 def call_event name , event , globals : nil , logger : nil
171197 globals ||= run_startup_tasks name , logger : logger , lenient : true
172- function = Testing . current_registry [ name ]
173- case function &.type
174- when :cloud_event
198+ Testing . call :cloud_event , name , readable_name : "CloudEvent" do |function |
175199 function . call event , globals : globals , logger : logger
176200 nil
177- when nil
178- raise "Unknown function name #{ name } "
179- else
180- raise "Function #{ name } is not a CloudEvent function"
181201 end
182202 end
183203
@@ -316,6 +336,20 @@ def current_globals name, globals = nil
316336 end
317337 end
318338
339+ ## @private
340+ def call type , name , readable_name : nil
341+ readable_name ||= type
342+ function = Testing . current_registry [ name ]
343+ case function &.type
344+ when type
345+ yield function
346+ when nil
347+ raise "Unknown function name #{ name } "
348+ else
349+ raise "Function #{ name } is not a #{ readable_name } function"
350+ end
351+ end
352+
319353 ## @private
320354 def interpret_response
321355 response =
0 commit comments