From 4f45f39bc605288ccc6875a22d7d94046bbca55c Mon Sep 17 00:00:00 2001 From: ParthSareen Date: Fri, 3 Oct 2025 14:14:28 -0700 Subject: [PATCH] remove auth for tests --- server/routes.go | 13 ++----------- server/routes_test.go | 43 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 42 insertions(+), 14 deletions(-) diff --git a/server/routes.go b/server/routes.go index e18881816..e95badc69 100644 --- a/server/routes.go +++ b/server/routes.go @@ -51,17 +51,6 @@ import ( const signinURLStr = "https://ollama.com/connect?name=%s&key=%s" -var ( - webServiceBase = func() *url.URL { - u, err := url.Parse("https://ollama.com") - if err != nil { - panic(err) - } - return u - }() - webServiceClient = api.NewClient(webServiceBase, http.DefaultClient) -) - func shouldUseHarmony(model *Model) bool { if slices.Contains([]string{"gptoss", "gpt-oss"}, model.Config.ModelFamily) { // heuristic to check whether the template expects to be parsed via harmony: @@ -799,6 +788,7 @@ func (s *Server) WebSearchHandler(c *gin.Context) { return } + webServiceClient := api.NewClient(&url.URL{Scheme: "https", Host: "ollama.com"}, http.DefaultClient) resp, err := webServiceClient.WebSearch(c.Request.Context(), &req) if err != nil { var authError api.AuthorizationError @@ -845,6 +835,7 @@ func (s *Server) WebFetchHandler(c *gin.Context) { return } + webServiceClient := api.NewClient(&url.URL{Scheme: "https", Host: "ollama.com"}, http.DefaultClient) resp, err := webServiceClient.WebFetch(c.Request.Context(), &req) if err != nil { var authError api.AuthorizationError diff --git a/server/routes_test.go b/server/routes_test.go index c8947ac6e..68b34e418 100644 --- a/server/routes_test.go +++ b/server/routes_test.go @@ -92,7 +92,14 @@ func (t *panicTransport) RoundTrip(r *http.Request) (*http.Response, error) { var panicOnRoundTrip = &http.Client{Transport: &panicTransport{}} +type roundTripFunc func(*http.Request) (*http.Response, error) + +func (f roundTripFunc) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) } + func TestRoutes(t *testing.T) { + // Disable authentication for tests to avoid issues with missing private keys + t.Setenv("OLLAMA_AUTH", "false") + type testCase struct { Name string Method string @@ -613,9 +620,39 @@ func TestRoutes(t *testing.T) { t.Fatalf("parse remote server URL: %v", err) } - origWebClient := webServiceClient - webServiceClient = api.NewClient(remoteURL, remoteSrv.Client()) - t.Cleanup(func() { webServiceClient = origWebClient }) + origTransport := http.DefaultTransport + remoteClient := remoteSrv.Client() + remoteTransport := remoteClient.Transport + if remoteTransport == nil { + t.Fatalf("remote client transport is nil") + } + + http.DefaultTransport = roundTripFunc(func(req *http.Request) (*http.Response, error) { + if req.URL == nil { + panic("unexpected nil request URL") + } + if req.URL.Host != "ollama.com" { + panic(fmt.Sprintf("unexpected outbound request to %s", req.URL)) + } + + clone := req.Clone(req.Context()) + cloneURL := remoteURL.ResolveReference(&url.URL{ + Path: req.URL.Path, + RawPath: req.URL.RawPath, + RawQuery: req.URL.RawQuery, + }) + clone.URL = cloneURL + clone.Host = cloneURL.Host + + return remoteTransport.RoundTrip(clone) + }) + + t.Cleanup(func() { + if closer, ok := remoteTransport.(interface{ CloseIdleConnections() }); ok { + closer.CloseIdleConnections() + } + http.DefaultTransport = origTransport + }) s := &Server{} router, err := s.GenerateRoutes(rc)