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