server: send 405 instead of 404 for unallowed methods (#10275)

Fixes: #5483
This commit is contained in:
Devon Rifkin 2025-05-06 14:45:37 -07:00 committed by GitHub
parent 92ce438de0
commit 4090aca97b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 0 deletions

View File

@ -1170,6 +1170,7 @@ func (s *Server) GenerateRoutes(rc *ollama.Registry) (http.Handler, error) {
corsConfig.AllowOrigins = envconfig.AllowedOrigins()
r := gin.Default()
r.HandleMethodNotAllowed = true
r.Use(
cors.New(corsConfig),
allowedHostsMiddleware(s.addr),

View File

@ -485,6 +485,16 @@ func TestRoutes(t *testing.T) {
}
},
},
{
Name: "Method Not Allowed",
Method: http.MethodGet,
Path: "/api/show",
Expected: func(t *testing.T, resp *http.Response) {
if resp.StatusCode != 405 {
t.Errorf("expected status code 405, got %d", resp.StatusCode)
}
},
},
}
modelsDir := t.TempDir()