)
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,
wantBody: "",
},
- // GET with full URL:
- 1: {
+ {
+ name: "GET with full URL",
method: "GET",
uri: "http://foo.com/path/%2f/bar/",
body: nil,
wantBody: "",
},
- // GET with full https URL:
- 2: {
+ {
+ name: "GET with full https URL",
method: "GET",
uri: "https://foo.com/path/",
body: nil,
wantBody: "",
},
- // Post with known length
- 3: {
+ {
+ name: "Post with known length",
method: "POST",
uri: "/",
body: strings.NewReader("foo"),
wantBody: "foo",
},
- // Post with unknown length
- 4: {
+ {
+ name: "Post with unknown length",
method: "POST",
uri: "/",
body: struct{ io.Reader }{strings.NewReader("foo")},
wantBody: "foo",
},
- // OPTIONS *
- 5: {
+ {
+ name: "OPTIONS *",
method: "OPTIONS",
uri: "*",
want: &http.Request{
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)
+ }
+ })
}
}
}
}
- tests := []struct {
+ for _, tt := range [...]struct {
name string
h func(w http.ResponseWriter, r *http.Request)
checks []checkFunc
},
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)
+ }
}
- }
+ })
}
}