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