1
1
import unittest
2
2
3
+ import pytest
3
4
from requests .models import PreparedRequest , Response
4
5
5
6
from huggingface_hub .utils ._errors import (
7
+ REPO_API_REGEX ,
6
8
BadRequestError ,
7
9
EntryNotFoundError ,
8
10
HfHubHTTPError ,
@@ -23,17 +25,30 @@ def test_hf_raise_for_status_repo_not_found(self) -> None:
23
25
self .assertEqual (context .exception .response .status_code , 404 )
24
26
self .assertIn ("Request ID: 123" , str (context .exception ))
25
27
26
- def test_hf_raise_for_status_repo_not_found_without_error_code (self ) -> None :
28
+ def test_hf_raise_for_status_401_repo_url (self ) -> None :
27
29
response = Response ()
28
30
response .headers = {"X-Request-Id" : 123 }
29
31
response .status_code = 401
30
32
response .request = PreparedRequest ()
33
+ response .request .url = "https://huggingface.co/api/models/username/reponame"
31
34
with self .assertRaisesRegex (RepositoryNotFoundError , "Repository Not Found" ) as context :
32
35
hf_raise_for_status (response )
33
36
34
37
self .assertEqual (context .exception .response .status_code , 401 )
35
38
self .assertIn ("Request ID: 123" , str (context .exception ))
36
39
40
+ def test_hf_raise_for_status_401_not_repo_url (self ) -> None :
41
+ response = Response ()
42
+ response .headers = {"X-Request-Id" : 123 }
43
+ response .status_code = 401
44
+ response .request = PreparedRequest ()
45
+ response .request .url = "https://huggingface.co/api/collections"
46
+ with self .assertRaises (HfHubHTTPError ) as context :
47
+ hf_raise_for_status (response )
48
+
49
+ self .assertEqual (context .exception .response .status_code , 401 )
50
+ self .assertIn ("Request ID: 123" , str (context .exception ))
51
+
37
52
def test_hf_raise_for_status_revision_not_found (self ) -> None :
38
53
response = Response ()
39
54
response .headers = {"X-Error-Code" : "RevisionNotFound" , "X-Request-Id" : 123 }
@@ -239,3 +254,44 @@ def test_hf_hub_http_error_init_with_error_message_duplicated_in_header_and_body
239
254
"this is a message\n \n Error message duplicated in headers and body." ,
240
255
)
241
256
self .assertEqual (error .server_message , "Error message duplicated in headers and body." )
257
+
258
+
259
+ @pytest .mark .parametrize (
260
+ ("url" , "should_match" ),
261
+ [
262
+ # Listing endpoints => False
263
+ ("https://huggingface.co/api/models" , False ),
264
+ ("https://huggingface.co/api/datasets" , False ),
265
+ ("https://huggingface.co/api/spaces" , False ),
266
+ # Create repo endpoint => False
267
+ ("https://huggingface.co/api/repos/create" , False ),
268
+ # Collection endpoints => False
269
+ ("https://huggingface.co/api/collections" , False ),
270
+ ("https://huggingface.co/api/collections/foo/bar" , False ),
271
+ # Repo endpoints => True
272
+ ("https://huggingface.co/api/models/repo_id" , True ),
273
+ ("https://huggingface.co/api/datasets/repo_id" , True ),
274
+ ("https://huggingface.co/api/spaces/repo_id" , True ),
275
+ ("https://huggingface.co/api/models/username/repo_name/refs/main" , True ),
276
+ ("https://huggingface.co/api/datasets/username/repo_name/refs/main" , True ),
277
+ ("https://huggingface.co/api/spaces/username/repo_name/refs/main" , True ),
278
+ # Inference Endpoint => False
279
+ ("https://api.endpoints.huggingface.cloud/v2/endpoint/namespace" , False ),
280
+ # Staging Endpoint => True
281
+ ("https://hub-ci.huggingface.co/api/models/repo_id" , True ),
282
+ ("https://hub-ci.huggingface.co/api/datasets/repo_id" , True ),
283
+ ("https://hub-ci.huggingface.co/api/spaces/repo_id" , True ),
284
+ # /resolve Endpoint => True
285
+ ("https://huggingface.co/gpt2/resolve/main/README.md" , True ),
286
+ ("https://huggingface.co/datasets/google/fleurs/resolve/revision/README.md" , True ),
287
+ # Regression tests
288
+ ("https://huggingface.co/bert-base/resolve/main/pytorch_model.bin" , True ),
289
+ ("https://hub-ci.huggingface.co/__DUMMY_USER__/repo-1470b5/resolve/main/file.txt" , True ),
290
+ ],
291
+ )
292
+ def test_repo_api_regex (url : str , should_match : bool ) -> None :
293
+ """Test the regex used to match repo API URLs."""
294
+ if should_match :
295
+ assert REPO_API_REGEX .match (url )
296
+ else :
297
+ assert REPO_API_REGEX .match (url ) is None
0 commit comments