github.com/rumpl/bof@v23.0.0-rc.2+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 cfg := &Config{ 17 Version: "0.1omega2", 18 } 19 srv := &Server{ 20 cfg: cfg, 21 } 22 23 srv.UseMiddleware(middleware.NewVersionMiddleware("0.1omega2", api.DefaultVersion, api.MinVersion)) 24 25 req, _ := http.NewRequest(http.MethodGet, "/containers/json", nil) 26 resp := httptest.NewRecorder() 27 ctx := context.Background() 28 29 localHandler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error { 30 if httputils.VersionFromContext(ctx) == "" { 31 t.Fatal("Expected version, got empty string") 32 } 33 34 if sv := w.Header().Get("Server"); !strings.Contains(sv, "Docker/0.1omega2") { 35 t.Fatalf("Expected server version in the header `Docker/0.1omega2`, got %s", sv) 36 } 37 38 return nil 39 } 40 41 handlerFunc := srv.handlerWithGlobalMiddlewares(localHandler) 42 if err := handlerFunc(ctx, resp, req, map[string]string{}); err != nil { 43 t.Fatal(err) 44 } 45 }