github.com/khulnasoft-lab/khulnasoft@v26.0.1-0.20240328202558-330a6f959fe0+incompatible/api/server/server_test.go (about) 1 package server // import "github.com/docker/docker/api/server" 2 3 import ( 4 "context" 5 "net/http" 6 "net/http/httptest" 7 "strings" 8 "testing" 9 10 "github.com/docker/docker/api" 11 "github.com/docker/docker/api/server/httputils" 12 "github.com/docker/docker/api/server/middleware" 13 ) 14 15 func TestMiddlewares(t *testing.T) { 16 srv := &Server{} 17 18 m, err := middleware.NewVersionMiddleware("0.1omega2", api.DefaultVersion, api.MinSupportedAPIVersion) 19 if err != nil { 20 t.Fatal(err) 21 } 22 srv.UseMiddleware(*m) 23 24 req, _ := http.NewRequest(http.MethodGet, "/containers/json", nil) 25 resp := httptest.NewRecorder() 26 ctx := context.Background() 27 28 localHandler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error { 29 if httputils.VersionFromContext(ctx) == "" { 30 t.Fatal("Expected version, got empty string") 31 } 32 33 if sv := w.Header().Get("Server"); !strings.Contains(sv, "Docker/0.1omega2") { 34 t.Fatalf("Expected server version in the header `Docker/0.1omega2`, got %s", sv) 35 } 36 37 return nil 38 } 39 40 handlerFunc := srv.handlerWithGlobalMiddlewares(localHandler) 41 if err := handlerFunc(ctx, resp, req, map[string]string{}); err != nil { 42 t.Fatal(err) 43 } 44 }