@@ -34,7 +34,7 @@ defmodule Supabase.FunctionsTest do
3434 end
3535
3636 test "handles text response content type" , % { client: client } do
37- expect ( @ mock , :stream , fn _request , _ ->
37+ expect ( @ mock , :stream , fn _request , _opts ->
3838 { :ok ,
3939 % Finch.Response {
4040 status: 200 ,
@@ -50,7 +50,7 @@ defmodule Supabase.FunctionsTest do
5050 test "sets appropriate content-type for binary data" , % { client: client } do
5151 binary_data = << 0 , 1 , 2 , 3 >>
5252
53- expect ( @ mock , :stream , fn request , _ ->
53+ expect ( @ mock , :stream , fn request , _opts ->
5454 assert Request . get_header ( request , "content-type" ) == "application/octet-stream"
5555 assert request . body == binary_data
5656
@@ -71,7 +71,7 @@ defmodule Supabase.FunctionsTest do
7171 test "sets appropriate content-type for JSON data" , % { client: client } do
7272 json_data = % { test: "data" }
7373
74- expect ( @ mock , :stream , fn request , _ ->
74+ expect ( @ mock , :stream , fn request , _opts ->
7575 assert Request . get_header ( request , "content-type" ) == "application/json"
7676 # fetcher will io encode it
7777 assert { :ok , _ } = Jason . decode ( request . body )
@@ -93,7 +93,7 @@ defmodule Supabase.FunctionsTest do
9393 test "handles custom headers" , % { client: client } do
9494 custom_headers = % { "x-custom-header" => "test-value" }
9595
96- expect ( @ mock , :stream , fn request , _ ->
96+ expect ( @ mock , :stream , fn request , _opts ->
9797 assert Request . get_header ( request , "x-custom-header" ) == "test-value"
9898
9999 { :ok ,
@@ -116,7 +116,7 @@ defmodule Supabase.FunctionsTest do
116116 test "handles streaming responses with custom handler" , % { client: client } do
117117 chunks = [ "chunk1" , "chunk2" , "chunk3" ]
118118
119- expect ( @ mock , :stream , fn _request , on_response , _ ->
119+ expect ( @ mock , :stream , fn _request , on_response , _opts ->
120120 Enum . each ( chunks , fn chunk ->
121121 on_response . ( { 200 , % { "content-type" => "text/plain" } , [ chunk ] } )
122122 end )
@@ -141,7 +141,7 @@ defmodule Supabase.FunctionsTest do
141141 end
142142
143143 test "handles error responses" , % { client: client } do
144- expect ( @ mock , :stream , fn _request , _ ->
144+ expect ( @ mock , :stream , fn _request , _opts ->
145145 { :ok ,
146146 % Finch.Response {
147147 status: 404 ,
@@ -156,7 +156,7 @@ defmodule Supabase.FunctionsTest do
156156 end
157157
158158 test "uses custom HTTP method when specified" , % { client: client } do
159- expect ( @ mock , :stream , fn request , _ ->
159+ expect ( @ mock , :stream , fn request , _opts ->
160160 assert request . method == :get
161161
162162 { :ok ,
@@ -174,7 +174,7 @@ defmodule Supabase.FunctionsTest do
174174 end
175175
176176 test "handles relay errors" , % { client: client } do
177- expect ( @ mock , :stream , fn _request , _ ->
177+ expect ( @ mock , :stream , fn _request , _opts ->
178178 { :ok ,
179179 % Finch.Response {
180180 status: 200 ,
@@ -188,5 +188,73 @@ defmodule Supabase.FunctionsTest do
188188
189189 assert error . code == :relay_error
190190 end
191+
192+ test "passes timeout option to underlying HTTP client" , % { client: client } do
193+ expect ( @ mock , :stream , fn _request , opts ->
194+ assert Keyword . get ( opts , :receive_timeout ) == 5_000
195+ assert Keyword . get ( opts , :request_timeout ) == 5_000
196+
197+ { :ok ,
198+ % Finch.Response {
199+ status: 200 ,
200+ headers: % { "content-type" => "application/json" } ,
201+ body: ~s( {"success": true})
202+ } }
203+ end )
204+
205+ assert { :ok , response } =
206+ Functions . invoke ( client , "test-function" , timeout: 5_000 , http_client: @ mock )
207+
208+ assert response . body == % { "success" => true }
209+ end
210+
211+ test "uses default timeout when not specified" , % { client: client } do
212+ expect ( @ mock , :stream , fn _request , opts ->
213+ assert Keyword . get ( opts , :receive_timeout ) == 15_000
214+ assert Keyword . get ( opts , :request_timeout ) == 15_000
215+
216+ { :ok ,
217+ % Finch.Response {
218+ status: 200 ,
219+ headers: % { "content-type" => "application/json" } ,
220+ body: ~s( {"success": true})
221+ } }
222+ end )
223+
224+ assert { :ok , response } =
225+ Functions . invoke ( client , "test-function" , http_client: @ mock )
226+
227+ assert response . body == % { "success" => true }
228+ end
229+
230+ test "timeout works with streaming response" , % { client: client } do
231+ chunks = [ "chunk1" , "chunk2" ]
232+
233+ expect ( @ mock , :stream , fn _request , on_response , opts ->
234+ assert Keyword . get ( opts , :receive_timeout ) == 2_000
235+ assert Keyword . get ( opts , :request_timeout ) == 2_000
236+
237+ Enum . each ( chunks , fn chunk ->
238+ on_response . ( { 200 , % { "content-type" => "text/plain" } , [ chunk ] } )
239+ end )
240+
241+ { :ok , Enum . join ( chunks ) }
242+ end )
243+
244+ on_response = fn { status , headers , body } ->
245+ assert status == 200
246+ assert headers [ "content-type" ] == "text/plain"
247+ { :ok , body }
248+ end
249+
250+ assert { :ok , response } =
251+ Functions . invoke ( client , "test-function" ,
252+ on_response: on_response ,
253+ timeout: 2_000 ,
254+ http_client: @ mock
255+ )
256+
257+ assert response == "chunk1chunk2"
258+ end
191259 end
192260end
0 commit comments