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