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 (
)
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 {
} {
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("%d. ReadAll: %v", i, err)
t.Errorf("ReadAll: %v", err)
}
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
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) {
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) {
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) {
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) {
}
}
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)),
},
}
} {
t.Run(tt.name, func(t *testing.T) {
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.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