github.com/cookieai-jar/moby@v17.12.1-ce-rc2+incompatible/api/server/middleware/version_test.go (about)

     1  package middleware
     2  
     3  import (
     4  	"net/http"
     5  	"net/http/httptest"
     6  	"runtime"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/docker/docker/api/server/httputils"
    11  	"github.com/stretchr/testify/assert"
    12  	"golang.org/x/net/context"
    13  )
    14  
    15  func TestVersionMiddleware(t *testing.T) {
    16  	handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
    17  		if httputils.VersionFromContext(ctx) == "" {
    18  			t.Fatal("Expected version, got empty string")
    19  		}
    20  		return nil
    21  	}
    22  
    23  	defaultVersion := "1.10.0"
    24  	minVersion := "1.2.0"
    25  	m := NewVersionMiddleware(defaultVersion, defaultVersion, minVersion)
    26  	h := m.WrapHandler(handler)
    27  
    28  	req, _ := http.NewRequest("GET", "/containers/json", nil)
    29  	resp := httptest.NewRecorder()
    30  	ctx := context.Background()
    31  	if err := h(ctx, resp, req, map[string]string{}); err != nil {
    32  		t.Fatal(err)
    33  	}
    34  }
    35  
    36  func TestVersionMiddlewareVersionTooOld(t *testing.T) {
    37  	handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
    38  		if httputils.VersionFromContext(ctx) == "" {
    39  			t.Fatal("Expected version, got empty string")
    40  		}
    41  		return nil
    42  	}
    43  
    44  	defaultVersion := "1.10.0"
    45  	minVersion := "1.2.0"
    46  	m := NewVersionMiddleware(defaultVersion, defaultVersion, minVersion)
    47  	h := m.WrapHandler(handler)
    48  
    49  	req, _ := http.NewRequest("GET", "/containers/json", nil)
    50  	resp := httptest.NewRecorder()
    51  	ctx := context.Background()
    52  
    53  	vars := map[string]string{"version": "0.1"}
    54  	err := h(ctx, resp, req, vars)
    55  
    56  	if !strings.Contains(err.Error(), "client version 0.1 is too old. Minimum supported API version is 1.2.0") {
    57  		t.Fatalf("Expected too old client error, got %v", err)
    58  	}
    59  }
    60  
    61  func TestVersionMiddlewareVersionTooNew(t *testing.T) {
    62  	handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
    63  		if httputils.VersionFromContext(ctx) == "" {
    64  			t.Fatal("Expected version, got empty string")
    65  		}
    66  		return nil
    67  	}
    68  
    69  	defaultVersion := "1.10.0"
    70  	minVersion := "1.2.0"
    71  	m := NewVersionMiddleware(defaultVersion, defaultVersion, minVersion)
    72  	h := m.WrapHandler(handler)
    73  
    74  	req, _ := http.NewRequest("GET", "/containers/json", nil)
    75  	resp := httptest.NewRecorder()
    76  	ctx := context.Background()
    77  
    78  	vars := map[string]string{"version": "9999.9999"}
    79  	err := h(ctx, resp, req, vars)
    80  
    81  	if !strings.Contains(err.Error(), "client version 9999.9999 is too new. Maximum supported API version is 1.10.0") {
    82  		t.Fatalf("Expected too new client error, got %v", err)
    83  	}
    84  }
    85  
    86  func TestVersionMiddlewareWithErrorsReturnsHeaders(t *testing.T) {
    87  	handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
    88  		if httputils.VersionFromContext(ctx) == "" {
    89  			t.Fatal("Expected version, got empty string")
    90  		}
    91  		return nil
    92  	}
    93  
    94  	defaultVersion := "1.10.0"
    95  	minVersion := "1.2.0"
    96  	m := NewVersionMiddleware(defaultVersion, defaultVersion, minVersion)
    97  	h := m.WrapHandler(handler)
    98  
    99  	req, _ := http.NewRequest("GET", "/containers/json", nil)
   100  	resp := httptest.NewRecorder()
   101  	ctx := context.Background()
   102  
   103  	vars := map[string]string{"version": "0.1"}
   104  	err := h(ctx, resp, req, vars)
   105  
   106  	assert.Error(t, err)
   107  	hdr := resp.Result().Header
   108  	assert.Contains(t, hdr.Get("Server"), "Docker/"+defaultVersion)
   109  	assert.Contains(t, hdr.Get("Server"), runtime.GOOS)
   110  	assert.Equal(t, hdr.Get("API-Version"), defaultVersion)
   111  	assert.Equal(t, hdr.Get("OSType"), runtime.GOOS)
   112  }