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