]> Cypherpunks repositories - gostls13.git/commitdiff
net/http/httptest: table-test using named subtests
authorPierre Prinetti <pierreprinetti@gmail.com>
Sun, 17 Dec 2017 00:41:29 +0000 (01:41 +0100)
committerBrad Fitzpatrick <bradfitz@golang.org>
Sat, 12 May 2018 16:26:36 +0000 (16:26 +0000)
Use Go 1.7 Run method of testing.T to run the table-driven tests into
separate, named subtests. The behaviour of the tests is not modified.

Change-Id: Ia88fa59a3534e79e3f0731e948b5f8a9919b339d
Reviewed-on: https://go-review.googlesource.com/84478
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/net/http/httptest/httptest_test.go
src/net/http/httptest/recorder_test.go

index 4f9ecbd8bbc465b36aa6037601288f27edb5b602..ef7d94383770085f125e7050d591127f35bcd2de 100644 (file)
@@ -16,15 +16,17 @@ import (
 )
 
 func TestNewRequest(t *testing.T) {
-       tests := [...]struct {
+       for _, tt := range [...]struct {
+               name string
+
                method, uri string
                body        io.Reader
 
                want     *http.Request
                wantBody string
        }{
-               // Empty method means GET:
-               0: {
+               {
+                       name:   "Empty method means GET",
                        method: "",
                        uri:    "/",
                        body:   nil,
@@ -42,8 +44,8 @@ func TestNewRequest(t *testing.T) {
                        wantBody: "",
                },
 
-               // GET with full URL:
-               1: {
+               {
+                       name:   "GET with full URL",
                        method: "GET",
                        uri:    "http://foo.com/path/%2f/bar/",
                        body:   nil,
@@ -66,8 +68,8 @@ func TestNewRequest(t *testing.T) {
                        wantBody: "",
                },
 
-               // GET with full https URL:
-               2: {
+               {
+                       name:   "GET with full https URL",
                        method: "GET",
                        uri:    "https://foo.com/path/",
                        body:   nil,
@@ -94,8 +96,8 @@ func TestNewRequest(t *testing.T) {
                        wantBody: "",
                },
 
-               // Post with known length
-               3: {
+               {
+                       name:   "Post with known length",
                        method: "POST",
                        uri:    "/",
                        body:   strings.NewReader("foo"),
@@ -114,8 +116,8 @@ func TestNewRequest(t *testing.T) {
                        wantBody: "foo",
                },
 
-               // Post with unknown length
-               4: {
+               {
+                       name:   "Post with unknown length",
                        method: "POST",
                        uri:    "/",
                        body:   struct{ io.Reader }{strings.NewReader("foo")},
@@ -134,8 +136,8 @@ func TestNewRequest(t *testing.T) {
                        wantBody: "foo",
                },
 
-               // OPTIONS *
-               5: {
+               {
+                       name:   "OPTIONS *",
                        method: "OPTIONS",
                        uri:    "*",
                        want: &http.Request{
@@ -150,28 +152,29 @@ func TestNewRequest(t *testing.T) {
                                RequestURI: "*",
                        },
                },
-       }
-       for i, tt := range tests {
-               got := NewRequest(tt.method, tt.uri, tt.body)
-               slurp, err := ioutil.ReadAll(got.Body)
-               if err != nil {
-                       t.Errorf("%d. ReadAll: %v", i, err)
-               }
-               if string(slurp) != tt.wantBody {
-                       t.Errorf("%d. Body = %q; want %q", i, slurp, tt.wantBody)
-               }
-               got.Body = nil // before DeepEqual
-               if !reflect.DeepEqual(got.URL, tt.want.URL) {
-                       t.Errorf("%d. Request.URL mismatch:\n got: %#v\nwant: %#v", i, got.URL, tt.want.URL)
-               }
-               if !reflect.DeepEqual(got.Header, tt.want.Header) {
-                       t.Errorf("%d. Request.Header mismatch:\n got: %#v\nwant: %#v", i, got.Header, tt.want.Header)
-               }
-               if !reflect.DeepEqual(got.TLS, tt.want.TLS) {
-                       t.Errorf("%d. Request.TLS mismatch:\n got: %#v\nwant: %#v", i, got.TLS, tt.want.TLS)
-               }
-               if !reflect.DeepEqual(got, tt.want) {
-                       t.Errorf("%d. Request mismatch:\n got: %#v\nwant: %#v", i, got, tt.want)
-               }
+       } {
+               t.Run(tt.name, func(t *testing.T) {
+                       got := NewRequest(tt.method, tt.uri, tt.body)
+                       slurp, err := ioutil.ReadAll(got.Body)
+                       if err != nil {
+                               t.Errorf("ReadAll: %v", err)
+                       }
+                       if string(slurp) != tt.wantBody {
+                               t.Errorf("Body = %q; want %q", slurp, tt.wantBody)
+                       }
+                       got.Body = nil // before DeepEqual
+                       if !reflect.DeepEqual(got.URL, tt.want.URL) {
+                               t.Errorf("Request.URL mismatch:\n got: %#v\nwant: %#v", got.URL, tt.want.URL)
+                       }
+                       if !reflect.DeepEqual(got.Header, tt.want.Header) {
+                               t.Errorf("Request.Header mismatch:\n got: %#v\nwant: %#v", got.Header, tt.want.Header)
+                       }
+                       if !reflect.DeepEqual(got.TLS, tt.want.TLS) {
+                               t.Errorf("Request.TLS mismatch:\n got: %#v\nwant: %#v", got.TLS, tt.want.TLS)
+                       }
+                       if !reflect.DeepEqual(got, tt.want) {
+                               t.Errorf("Request mismatch:\n got: %#v\nwant: %#v", got, tt.want)
+                       }
+               })
        }
 }
index a6259ebac71ab81a1786adf86792538bd19c7114..b5f82d23e6037793e958d40303e6a3c7e439ef72 100644 (file)
@@ -111,7 +111,7 @@ func TestRecorder(t *testing.T) {
                }
        }
 
-       tests := []struct {
+       for _, tt := range [...]struct {
                name   string
                h      func(w http.ResponseWriter, r *http.Request)
                checks []checkFunc
@@ -273,16 +273,17 @@ func TestRecorder(t *testing.T) {
                        },
                        check(hasStatus(200), hasContents("Some body"), hasContentLength(9)),
                },
-       }
-       r, _ := http.NewRequest("GET", "http://foo.com/", nil)
-       for _, tt := range tests {
-               h := http.HandlerFunc(tt.h)
-               rec := NewRecorder()
-               h.ServeHTTP(rec, r)
-               for _, check := range tt.checks {
-                       if err := check(rec); err != nil {
-                               t.Errorf("%s: %v", tt.name, err)
+       } {
+               t.Run(tt.name, func(t *testing.T) {
+                       r, _ := http.NewRequest("GET", "http://foo.com/", nil)
+                       h := http.HandlerFunc(tt.h)
+                       rec := NewRecorder()
+                       h.ServeHTTP(rec, r)
+                       for _, check := range tt.checks {
+                               if err := check(rec); err != nil {
+                                       t.Error(err)
+                               }
                        }
-               }
+               })
        }
 }