Commit 68764479 authored by Pierre Prinetti's avatar Pierre Prinetti Committed by Brad Fitzpatrick

net/http/httptest: table-test using named subtests

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: 's avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 33b2b172
...@@ -16,15 +16,17 @@ import ( ...@@ -16,15 +16,17 @@ import (
) )
func TestNewRequest(t *testing.T) { func TestNewRequest(t *testing.T) {
tests := [...]struct { for _, tt := range [...]struct {
name string
method, uri string method, uri string
body io.Reader body io.Reader
want *http.Request want *http.Request
wantBody string wantBody string
}{ }{
// Empty method means GET: {
0: { name: "Empty method means GET",
method: "", method: "",
uri: "/", uri: "/",
body: nil, body: nil,
...@@ -42,8 +44,8 @@ func TestNewRequest(t *testing.T) { ...@@ -42,8 +44,8 @@ func TestNewRequest(t *testing.T) {
wantBody: "", wantBody: "",
}, },
// GET with full URL: {
1: { name: "GET with full URL",
method: "GET", method: "GET",
uri: "http://foo.com/path/%2f/bar/", uri: "http://foo.com/path/%2f/bar/",
body: nil, body: nil,
...@@ -66,8 +68,8 @@ func TestNewRequest(t *testing.T) { ...@@ -66,8 +68,8 @@ func TestNewRequest(t *testing.T) {
wantBody: "", wantBody: "",
}, },
// GET with full https URL: {
2: { name: "GET with full https URL",
method: "GET", method: "GET",
uri: "https://foo.com/path/", uri: "https://foo.com/path/",
body: nil, body: nil,
...@@ -94,8 +96,8 @@ func TestNewRequest(t *testing.T) { ...@@ -94,8 +96,8 @@ func TestNewRequest(t *testing.T) {
wantBody: "", wantBody: "",
}, },
// Post with known length {
3: { name: "Post with known length",
method: "POST", method: "POST",
uri: "/", uri: "/",
body: strings.NewReader("foo"), body: strings.NewReader("foo"),
...@@ -114,8 +116,8 @@ func TestNewRequest(t *testing.T) { ...@@ -114,8 +116,8 @@ func TestNewRequest(t *testing.T) {
wantBody: "foo", wantBody: "foo",
}, },
// Post with unknown length {
4: { name: "Post with unknown length",
method: "POST", method: "POST",
uri: "/", uri: "/",
body: struct{ io.Reader }{strings.NewReader("foo")}, body: struct{ io.Reader }{strings.NewReader("foo")},
...@@ -134,8 +136,8 @@ func TestNewRequest(t *testing.T) { ...@@ -134,8 +136,8 @@ func TestNewRequest(t *testing.T) {
wantBody: "foo", wantBody: "foo",
}, },
// OPTIONS * {
5: { name: "OPTIONS *",
method: "OPTIONS", method: "OPTIONS",
uri: "*", uri: "*",
want: &http.Request{ want: &http.Request{
...@@ -150,28 +152,29 @@ func TestNewRequest(t *testing.T) { ...@@ -150,28 +152,29 @@ func TestNewRequest(t *testing.T) {
RequestURI: "*", RequestURI: "*",
}, },
}, },
} } {
for i, tt := range tests { t.Run(tt.name, func(t *testing.T) {
got := NewRequest(tt.method, tt.uri, tt.body) got := NewRequest(tt.method, tt.uri, tt.body)
slurp, err := ioutil.ReadAll(got.Body) slurp, err := ioutil.ReadAll(got.Body)
if err != nil { if err != nil {
t.Errorf("%d. ReadAll: %v", i, err) t.Errorf("ReadAll: %v", err)
} }
if string(slurp) != tt.wantBody { if string(slurp) != tt.wantBody {
t.Errorf("%d. Body = %q; want %q", i, slurp, tt.wantBody) t.Errorf("Body = %q; want %q", slurp, tt.wantBody)
} }
got.Body = nil // before DeepEqual got.Body = nil // before DeepEqual
if !reflect.DeepEqual(got.URL, tt.want.URL) { 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) t.Errorf("Request.URL mismatch:\n got: %#v\nwant: %#v", got.URL, tt.want.URL)
} }
if !reflect.DeepEqual(got.Header, tt.want.Header) { 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) t.Errorf("Request.Header mismatch:\n got: %#v\nwant: %#v", got.Header, tt.want.Header)
} }
if !reflect.DeepEqual(got.TLS, tt.want.TLS) { 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) t.Errorf("Request.TLS mismatch:\n got: %#v\nwant: %#v", got.TLS, tt.want.TLS)
} }
if !reflect.DeepEqual(got, tt.want) { if !reflect.DeepEqual(got, tt.want) {
t.Errorf("%d. Request mismatch:\n got: %#v\nwant: %#v", i, got, tt.want) t.Errorf("Request mismatch:\n got: %#v\nwant: %#v", got, tt.want)
} }
})
} }
} }
...@@ -111,7 +111,7 @@ func TestRecorder(t *testing.T) { ...@@ -111,7 +111,7 @@ func TestRecorder(t *testing.T) {
} }
} }
tests := []struct { for _, tt := range [...]struct {
name string name string
h func(w http.ResponseWriter, r *http.Request) h func(w http.ResponseWriter, r *http.Request)
checks []checkFunc checks []checkFunc
...@@ -273,16 +273,17 @@ func TestRecorder(t *testing.T) { ...@@ -273,16 +273,17 @@ func TestRecorder(t *testing.T) {
}, },
check(hasStatus(200), hasContents("Some body"), hasContentLength(9)), check(hasStatus(200), hasContents("Some body"), hasContentLength(9)),
}, },
} } {
t.Run(tt.name, func(t *testing.T) {
r, _ := http.NewRequest("GET", "http://foo.com/", nil) r, _ := http.NewRequest("GET", "http://foo.com/", nil)
for _, tt := range tests {
h := http.HandlerFunc(tt.h) h := http.HandlerFunc(tt.h)
rec := NewRecorder() rec := NewRecorder()
h.ServeHTTP(rec, r) h.ServeHTTP(rec, r)
for _, check := range tt.checks { for _, check := range tt.checks {
if err := check(rec); err != nil { if err := check(rec); err != nil {
t.Errorf("%s: %v", tt.name, err) t.Error(err)
} }
} }
})
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment