github.com/hms58/moby@v1.13.1/api/server/middleware/version_test.go (about) 1 package middleware 2 3 import ( 4 "net/http" 5 "net/http/httptest" 6 "strings" 7 "testing" 8 9 "github.com/docker/docker/api/server/httputils" 10 "golang.org/x/net/context" 11 ) 12 13 func TestVersionMiddleware(t *testing.T) { 14 handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error { 15 if httputils.VersionFromContext(ctx) == "" { 16 t.Fatalf("Expected version, got empty string") 17 } 18 return nil 19 } 20 21 defaultVersion := "1.10.0" 22 minVersion := "1.2.0" 23 m := NewVersionMiddleware(defaultVersion, defaultVersion, minVersion) 24 h := m.WrapHandler(handler) 25 26 req, _ := http.NewRequest("GET", "/containers/json", nil) 27 resp := httptest.NewRecorder() 28 ctx := context.Background() 29 if err := h(ctx, resp, req, map[string]string{}); err != nil { 30 t.Fatal(err) 31 } 32 } 33 34 func TestVersionMiddlewareWithErrors(t *testing.T) { 35 handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error { 36 if httputils.VersionFromContext(ctx) == "" { 37 t.Fatalf("Expected version, got empty string") 38 } 39 return nil 40 } 41 42 defaultVersion := "1.10.0" 43 minVersion := "1.2.0" 44 m := NewVersionMiddleware(defaultVersion, defaultVersion, minVersion) 45 h := m.WrapHandler(handler) 46 47 req, _ := http.NewRequest("GET", "/containers/json", nil) 48 resp := httptest.NewRecorder() 49 ctx := context.Background() 50 51 vars := map[string]string{"version": "0.1"} 52 err := h(ctx, resp, req, vars) 53 54 if !strings.Contains(err.Error(), "client version 0.1 is too old. Minimum supported API version is 1.2.0") { 55 t.Fatalf("Expected too old client error, got %v", err) 56 } 57 }