@@ -1195,3 +1195,83 @@ async def test_arbitrary_roles_accepted_in_message_request_async(monkeypatch: py
11951195 client = AsyncClient ()
11961196
11971197 await client .chat (model = 'llama3.1' , messages = [{'role' : 'somerandomrole' , 'content' : "I'm ok with you adding any role message now!" }, {'role' : 'user' , 'content' : 'Hello world!' }])
1198+
1199+
1200+ def test_client_web_search_requires_bearer_auth_header (monkeypatch : pytest .MonkeyPatch ):
1201+ monkeypatch .delenv ('OLLAMA_API_KEY' , raising = False )
1202+
1203+ client = Client ()
1204+
1205+ with pytest .raises (ValueError , match = 'Authorization header with Bearer token is required for web search' ):
1206+ client .web_search (['test query' ])
1207+
1208+
1209+ def test_client_web_crawl_requires_bearer_auth_header (monkeypatch : pytest .MonkeyPatch ):
1210+ monkeypatch .delenv ('OLLAMA_API_KEY' , raising = False )
1211+
1212+ client = Client ()
1213+
1214+ with pytest .raises (ValueError , match = 'Authorization header with Bearer token is required for web fetch' ):
1215+ client .web_crawl (['https://example.com' ])
1216+
1217+
1218+ def _mock_request_web_search (self , cls , method , url , json = None , ** kwargs ):
1219+ assert method == 'POST'
1220+ assert url == 'https://ollama.com/api/web_search'
1221+ assert json is not None and 'queries' in json and 'max_results' in json
1222+ return httpxResponse (status_code = 200 , content = '{"results": {}, "success": true}' )
1223+
1224+
1225+ def _mock_request_web_crawl (self , cls , method , url , json = None , ** kwargs ):
1226+ assert method == 'POST'
1227+ assert url == 'https://ollama.com/api/web_crawl'
1228+ assert json is not None and 'urls' in json
1229+ return httpxResponse (status_code = 200 , content = '{"results": {}, "success": true}' )
1230+
1231+
1232+ def test_client_web_search_with_env_api_key (monkeypatch : pytest .MonkeyPatch ):
1233+ monkeypatch .setenv ('OLLAMA_API_KEY' , 'test-key' )
1234+ monkeypatch .setattr (Client , '_request' , _mock_request_web_search )
1235+
1236+ client = Client ()
1237+ client .web_search (['what is ollama?' ], max_results = 2 )
1238+
1239+
1240+ def test_client_web_crawl_with_env_api_key (monkeypatch : pytest .MonkeyPatch ):
1241+ monkeypatch .setenv ('OLLAMA_API_KEY' , 'test-key' )
1242+ monkeypatch .setattr (Client , '_request' , _mock_request_web_crawl )
1243+
1244+ client = Client ()
1245+ client .web_crawl (['https://example.com' ])
1246+
1247+
1248+ def test_client_web_search_with_explicit_bearer_header (monkeypatch : pytest .MonkeyPatch ):
1249+ monkeypatch .delenv ('OLLAMA_API_KEY' , raising = False )
1250+ monkeypatch .setattr (Client , '_request' , _mock_request_web_search )
1251+
1252+ client = Client (headers = {'Authorization' : 'Bearer custom-token' })
1253+ client .web_search (['what is ollama?' ], max_results = 1 )
1254+
1255+
1256+ def test_client_web_crawl_with_explicit_bearer_header (monkeypatch : pytest .MonkeyPatch ):
1257+ monkeypatch .delenv ('OLLAMA_API_KEY' , raising = False )
1258+ monkeypatch .setattr (Client , '_request' , _mock_request_web_crawl )
1259+
1260+ client = Client (headers = {'Authorization' : 'Bearer custom-token' })
1261+ client .web_crawl (['https://example.com' ])
1262+
1263+
1264+ def test_client_bearer_header_from_env (monkeypatch : pytest .MonkeyPatch ):
1265+ monkeypatch .setenv ('OLLAMA_API_KEY' , 'env-token' )
1266+
1267+ client = Client ()
1268+ assert client ._client .headers ['authorization' ] == 'Bearer env-token'
1269+
1270+
1271+ def test_client_explicit_bearer_header_overrides_env (monkeypatch : pytest .MonkeyPatch ):
1272+ monkeypatch .setenv ('OLLAMA_API_KEY' , 'env-token' )
1273+ monkeypatch .setattr (Client , '_request' , _mock_request_web_search )
1274+
1275+ client = Client (headers = {'Authorization' : 'Bearer explicit-token' })
1276+ assert client ._client .headers ['authorization' ] == 'Bearer explicit-token'
1277+ client .web_search (['override check' ])
0 commit comments