1
+ import { describe , it , expect , vi } from "vitest" ;
2
+ import { createFetch } from "./createFetch" ;
3
+ import { USER_AGENT } from "../consts" ;
4
+
5
+ describe ( "createFetch" , ( ) => {
6
+ it ( "should add user-agent header to fetch requests" , async ( ) => {
7
+ // Create a mock fetch function
8
+ const mockFetch = vi . fn ( ) . mockResolvedValue ( {
9
+ ok : true ,
10
+ } ) ;
11
+
12
+ // Create a wrapped fetch with our utility
13
+ const wrappedFetch = createFetch ( mockFetch ) ;
14
+
15
+ // Call the wrapped fetch
16
+ const url = "https://huggingface.co/api/test" ;
17
+ await wrappedFetch ( url ) ;
18
+
19
+ // Check if the mock was called with the correct headers
20
+ expect ( mockFetch ) . toHaveBeenCalledWith ( url , expect . objectContaining ( {
21
+ headers : expect . objectContaining ( {
22
+ get : expect . any ( Function ) ,
23
+ has : expect . any ( Function ) ,
24
+ } )
25
+ } ) ) ;
26
+
27
+ // Get the headers from the mock call
28
+ const headers = mockFetch . mock . calls [ 0 ] [ 1 ] . headers ;
29
+ expect ( headers . get ( "user-agent" ) ) . toBe ( USER_AGENT ) ;
30
+ } ) ;
31
+
32
+ it ( "should not override existing user-agent header" , async ( ) => {
33
+ // Create a mock fetch function
34
+ const mockFetch = vi . fn ( ) . mockResolvedValue ( {
35
+ ok : true ,
36
+ } ) ;
37
+
38
+ // Create a wrapped fetch with our utility
39
+ const wrappedFetch = createFetch ( mockFetch ) ;
40
+
41
+ // Call the wrapped fetch with a custom user-agent header
42
+ const url = "https://huggingface.co/api/test" ;
43
+ const customUserAgent = "custom-user-agent" ;
44
+ await wrappedFetch ( url , {
45
+ headers : {
46
+ "user-agent" : customUserAgent ,
47
+ } ,
48
+ } ) ;
49
+
50
+ // Get the headers from the mock call
51
+ const headers = mockFetch . mock . calls [ 0 ] [ 1 ] . headers ;
52
+ expect ( headers . get ( "user-agent" ) ) . toBe ( customUserAgent ) ;
53
+ } ) ;
54
+
55
+ it ( "should preserve other headers and request options" , async ( ) => {
56
+ // Create a mock fetch function
57
+ const mockFetch = vi . fn ( ) . mockResolvedValue ( {
58
+ ok : true ,
59
+ } ) ;
60
+
61
+ // Create a wrapped fetch with our utility
62
+ const wrappedFetch = createFetch ( mockFetch ) ;
63
+
64
+ // Call the wrapped fetch with additional headers and options
65
+ const url = "https://huggingface.co/api/test" ;
66
+ const options = {
67
+ method : "POST" ,
68
+ headers : {
69
+ "Content-Type" : "application/json" ,
70
+ "Authorization" : "token-value" ,
71
+ } ,
72
+ body : JSON . stringify ( { data : "test" } ) ,
73
+ } ;
74
+
75
+ await wrappedFetch ( url , options ) ;
76
+
77
+ // Check if the mock was called with all the options preserved
78
+ expect ( mockFetch ) . toHaveBeenCalledWith ( url , expect . objectContaining ( {
79
+ method : "POST" ,
80
+ body : JSON . stringify ( { data : "test" } ) ,
81
+ headers : expect . objectContaining ( {
82
+ get : expect . any ( Function ) ,
83
+ has : expect . any ( Function ) ,
84
+ } )
85
+ } ) ) ;
86
+
87
+ // Get the headers from the mock call
88
+ const headers = mockFetch . mock . calls [ 0 ] [ 1 ] . headers ;
89
+ expect ( headers . get ( "Content-Type" ) ) . toBe ( "application/json" ) ;
90
+ expect ( headers . get ( "Authorization" ) ) . toBe ( "token-value" ) ;
91
+ expect ( headers . get ( "user-agent" ) ) . toBe ( USER_AGENT ) ;
92
+ } ) ;
93
+ } ) ;
0 commit comments