Browse code

Merge pull request #36013 from thaJeztah/improve-version-middleware-test

Improve API version-middleware test

Vincent Demeester authored on 2018/01/23 07:59:06
Showing 1 changed files
... ...
@@ -4,7 +4,6 @@ import (
4 4
 	"net/http"
5 5
 	"net/http/httptest"
6 6
 	"runtime"
7
-	"strings"
8 7
 	"testing"
9 8
 
10 9
 	"github.com/docker/docker/api/server/httputils"
... ...
@@ -12,37 +11,16 @@ import (
12 12
 	"golang.org/x/net/context"
13 13
 )
14 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
-
15
+func TestVersionMiddlewareVersion(t *testing.T) {
23 16
 	defaultVersion := "1.10.0"
24 17
 	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) {
18
+	expectedVersion := defaultVersion
37 19
 	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
-		}
20
+		v := httputils.VersionFromContext(ctx)
21
+		assert.Equal(t, expectedVersion, v)
41 22
 		return nil
42 23
 	}
43 24
 
44
-	defaultVersion := "1.10.0"
45
-	minVersion := "1.2.0"
46 25
 	m := NewVersionMiddleware(defaultVersion, defaultVersion, minVersion)
47 26
 	h := m.WrapHandler(handler)
48 27
 
... ...
@@ -50,44 +28,45 @@ func TestVersionMiddlewareVersionTooOld(t *testing.T) {
50 50
 	resp := httptest.NewRecorder()
51 51
 	ctx := context.Background()
52 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)
53
+	tests := []struct {
54
+		reqVersion      string
55
+		expectedVersion string
56
+		errString       string
57
+	}{
58
+		{
59
+			expectedVersion: "1.10.0",
60
+		},
61
+		{
62
+			reqVersion:      "1.9.0",
63
+			expectedVersion: "1.9.0",
64
+		},
65
+		{
66
+			reqVersion: "0.1",
67
+			errString:  "client version 0.1 is too old. Minimum supported API version is 1.2.0, please upgrade your client to a newer version",
68
+		},
69
+		{
70
+			reqVersion: "9999.9999",
71
+			errString:  "client version 9999.9999 is too new. Maximum supported API version is 1.10.0",
72
+		},
58 73
 	}
59
-}
60 74
 
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
-	}
75
+	for _, test := range tests {
76
+		expectedVersion = test.expectedVersion
68 77
 
69
-	defaultVersion := "1.10.0"
70
-	minVersion := "1.2.0"
71
-	m := NewVersionMiddleware(defaultVersion, defaultVersion, minVersion)
72
-	h := m.WrapHandler(handler)
78
+		err := h(ctx, resp, req, map[string]string{"version": test.reqVersion})
73 79
 
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)
80
+		if test.errString != "" {
81
+			assert.EqualError(t, err, test.errString)
82
+		} else {
83
+			assert.NoError(t, err)
84
+		}
83 85
 	}
84 86
 }
85 87
 
86 88
 func TestVersionMiddlewareWithErrorsReturnsHeaders(t *testing.T) {
87 89
 	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
-		}
90
+		v := httputils.VersionFromContext(ctx)
91
+		assert.NotEmpty(t, v)
91 92
 		return nil
92 93
 	}
93 94
 
... ...
@@ -102,8 +81,8 @@ func TestVersionMiddlewareWithErrorsReturnsHeaders(t *testing.T) {
102 102
 
103 103
 	vars := map[string]string{"version": "0.1"}
104 104
 	err := h(ctx, resp, req, vars)
105
-
106 105
 	assert.Error(t, err)
106
+
107 107
 	hdr := resp.Result().Header
108 108
 	assert.Contains(t, hdr.Get("Server"), "Docker/"+defaultVersion)
109 109
 	assert.Contains(t, hdr.Get("Server"), runtime.GOOS)