@@ -115,6 +115,28 @@ defmodule Bumblebee.HuggingFace.HubTest do
115
115
assert File . read! ( path ) == << 0 , 1 >>
116
116
end
117
117
118
+ @ tag :tmp_dir
119
+ test "follows relative redirect before checking etag" , % { bypass: bypass , tmp_dir: tmp_dir } do
120
+ Bypass . expect_once ( bypass , "HEAD" , "/repo/file.json" , fn conn ->
121
+ conn
122
+ |> Plug.Conn . put_resp_header ( "location" , "/repo-renamed/file.json" )
123
+ |> Plug.Conn . resp ( 307 , "" )
124
+ end )
125
+
126
+ Bypass . expect_once ( bypass , "HEAD" , "/repo-renamed/file.json" , fn conn ->
127
+ serve_with_etag ( conn , ~s/ "hash"/ , "{}" )
128
+ end )
129
+
130
+ Bypass . expect_once ( bypass , "GET" , "/repo-renamed/file.json" , fn conn ->
131
+ serve_with_etag ( conn , ~s/ "hash"/ , "{}" )
132
+ end )
133
+
134
+ url = url ( bypass . port ) <> "/repo/file.json"
135
+
136
+ assert { :ok , path } = Hub . cached_download ( url , cache_dir: tmp_dir , offline: false )
137
+ assert File . read! ( path ) == "{}"
138
+ end
139
+
118
140
@ tag :tmp_dir
119
141
test "returns an error on missing etag header" , % { bypass: bypass , tmp_dir: tmp_dir } do
120
142
Bypass . expect_once ( bypass , "HEAD" , "/file.json" , fn conn ->
@@ -183,6 +205,62 @@ defmodule Bumblebee.HuggingFace.HubTest do
183
205
"could not find file in local cache and outgoing traffic is disabled, url: " <> _ } =
184
206
Hub . cached_download ( url , cache_dir: tmp_dir , offline: true )
185
207
end
208
+
209
+ @ tag :tmp_dir
210
+ test "includes authorization header when :auth_token is given" ,
211
+ % { bypass: bypass , tmp_dir: tmp_dir } do
212
+ Bypass . expect_once ( bypass , "HEAD" , "/file.json" , fn conn ->
213
+ assert { "authorization" , "Bearer token" } in conn . req_headers
214
+
215
+ serve_with_etag ( conn , ~s/ "hash"/ , "" )
216
+ end )
217
+
218
+ Bypass . expect_once ( bypass , "GET" , "/file.json" , fn conn ->
219
+ assert { "authorization" , "Bearer token" } in conn . req_headers
220
+
221
+ serve_with_etag ( conn , ~s/ "hash"/ , "{}" )
222
+ end )
223
+
224
+ url = url ( bypass . port ) <> "/file.json"
225
+
226
+ assert { :ok , _path } =
227
+ Hub . cached_download ( url , auth_token: "token" , cache_dir: tmp_dir , offline: false )
228
+ end
229
+
230
+ @ tag :tmp_dir
231
+ test "skips authentication header for redirected requests" ,
232
+ % { bypass: bypass , tmp_dir: tmp_dir } do
233
+ # Context: HuggingFace Hub returns redirects for files stored
234
+ # in LFS and the redirect location already has S3 signature in
235
+ # URL params. If the location points to S3 directly, which is
236
+ # the case within HF spaces, passing the Authorization header
237
+ # leads to failure, presumably because it takes precedence over
238
+ # the params signature
239
+
240
+ Bypass . expect_once ( bypass , "HEAD" , "/file.bin" , fn conn ->
241
+ assert { "authorization" , "Bearer token" } in conn . req_headers
242
+
243
+ url = Plug.Conn . request_url ( conn )
244
+
245
+ conn
246
+ |> Plug.Conn . put_resp_header ( "x-linked-etag" , ~s/ "hash"/ )
247
+ |> Plug.Conn . put_resp_header ( "location" , url <> "/storage" )
248
+ |> Plug.Conn . resp ( 302 , "" )
249
+ end )
250
+
251
+ Bypass . expect_once ( bypass , "GET" , "/file.bin/storage" , fn conn ->
252
+ assert { "authorization" , "Bearer token" } not in conn . req_headers
253
+
254
+ conn
255
+ |> Plug.Conn . put_resp_header ( "etag" , ~s/ "hash"/ )
256
+ |> Plug.Conn . resp ( 200 , << 0 , 1 >> )
257
+ end )
258
+
259
+ url = url ( bypass . port ) <> "/file.bin"
260
+
261
+ assert { :ok , _path } =
262
+ Hub . cached_download ( url , auth_token: "token" , cache_dir: tmp_dir , offline: false )
263
+ end
186
264
end
187
265
188
266
defp url ( port ) , do: "http://localhost:#{ port } "
0 commit comments