@@ -60,31 +60,94 @@ defmodule Supabase.Fetcher.Adapter.FinchTest do
60
60
describe "dealing with streams" do
61
61
test "streams a response successfully" , % { builder: builder } do
62
62
@ mock
63
- |> expect ( :stream , fn % Request { } , on_response , _opts ->
63
+ |> expect ( :stream , fn % Request { } , _opts ->
64
64
status = 200
65
65
headers = [ { "content-length" , 80_543 } ]
66
66
stream = Stream . cycle ( [ "chunk1" , "chunk2" ] )
67
- on_response . ( { status , headers , stream } )
67
+ body = Enum . take ( stream , 2 ) |> Enum . to_list ( ) |> Enum . join ( "," )
68
+ { :ok , % Finch.Response { status: status , headers: headers , body: body } }
68
69
end )
69
70
70
- on_response = fn { status , headers , body } ->
71
- body = Enum . take ( body , 2 ) |> Enum . to_list ( ) |> Enum . join ( "," )
72
-
73
- { :ok ,
74
- [ status: status , headers: headers , body: body ]
75
- |> then ( & struct ( Finch.Response , & 1 ) ) }
76
- end
77
-
78
71
builder =
79
72
Request . with_body_decoder ( builder , fn % { body: body } , _opts ->
80
73
{ :ok , String . split ( body , "," , trim: true ) }
81
74
end )
82
75
83
- assert { :ok , % Response { } = resp } = Fetcher . stream ( builder , on_response )
76
+ assert { :ok , % Response { } = resp } = Fetcher . stream ( builder )
84
77
assert resp . status == 200
85
78
assert Response . get_header ( resp , "content-length" ) == 80_543
86
79
assert resp . body == [ "chunk1" , "chunk2" ]
87
80
end
81
+
82
+ test "streams a response successfully with custom on_response handler with no body" , % {
83
+ builder: builder
84
+ } do
85
+ @ mock
86
+ |> expect ( :stream , fn % Request { } , on_response , _opts ->
87
+ status = 200
88
+ headers = [ { "content-length" , 80_543 } ]
89
+ stream = Stream . cycle ( [ "chunk1" , "chunk2" ] )
90
+ on_response . ( { status , headers , stream } )
91
+ end )
92
+
93
+ on_response = fn { status , headers , body } ->
94
+ assert status == 200
95
+ assert List . keyfind ( headers , "content-length" , 0 ) == { "content-length" , 80_543 }
96
+
97
+ # very expensing body consuming
98
+ _ = Enum . take ( body , 2 ) |> Enum . to_list ( )
99
+
100
+ :ok
101
+ end
102
+
103
+ assert :ok = Fetcher . stream ( builder , on_response )
104
+ end
105
+
106
+ test "streams a response successfully with custom on_response handler returning body" , % {
107
+ builder: builder
108
+ } do
109
+ @ mock
110
+ |> expect ( :stream , fn % Request { } , on_response , _opts ->
111
+ status = 200
112
+ headers = [ { "content-length" , 80_543 } ]
113
+ stream = Stream . cycle ( [ "chunk1" , "chunk2" ] )
114
+ on_response . ( { status , headers , stream } )
115
+ end )
116
+
117
+ on_response = fn { status , headers , body } ->
118
+ assert status == 200
119
+ assert List . keyfind ( headers , "content-length" , 0 ) == { "content-length" , 80_543 }
120
+
121
+ { :ok ,
122
+ body
123
+ |> Enum . take ( 2 )
124
+ |> Enum . to_list ( ) }
125
+ end
126
+
127
+ assert { :ok , body } = Fetcher . stream ( builder , on_response )
128
+ assert body == [ "chunk1" , "chunk2" ]
129
+ end
130
+
131
+ test "returns an error from response streaming with custom on_response handler" , % {
132
+ builder: builder
133
+ } do
134
+ @ mock
135
+ |> expect ( :stream , fn % Request { } , on_response , _opts ->
136
+ status = 404
137
+ headers = [ { "content-length" , 80_543 } ]
138
+ stream = Stream . cycle ( [ ] )
139
+ on_response . ( { status , headers , stream } )
140
+ end )
141
+
142
+ on_response = fn { status , headers , _body } ->
143
+ assert status == 404
144
+ assert List . keyfind ( headers , "content-length" , 0 ) == { "content-length" , 80_543 }
145
+
146
+ { :error , Error . new ( code: :not_found , metadata: % { status: status } ) }
147
+ end
148
+
149
+ assert { :error , % Error { } } = Fetcher . stream ( builder , on_response )
150
+ end
88
151
end
89
152
90
153
describe "dealing with errors" do
0 commit comments