@@ -23,32 +23,78 @@ import (
2323 "testing"
2424)
2525
26+ func confirmStatusCode (t * testing.T , got , want int ) {
27+ t .Helper ()
28+ if got == want {
29+ return
30+ }
31+ if gotName := http .StatusText (got ); len (gotName ) > 0 {
32+ if wantName := http .StatusText (want ); len (wantName ) > 0 {
33+ t .Fatalf ("response status code: got %d (%s), want %d (%s)" , got , gotName , want , wantName )
34+ }
35+ }
36+ t .Fatalf ("response status code: got %d, want %d" , got , want )
37+ }
38+
39+ func confirmRequestValidationCode (t * testing.T , method , contentType , body string , expectedStatusCode int ) {
40+ t .Helper ()
41+ request := httptest .NewRequest (method , "http://url.com" , strings .NewReader (body ))
42+ if len (contentType ) > 0 {
43+ request .Header .Set ("Content-Type" , contentType )
44+ }
45+ code , err := validateRequest (request )
46+ if code == 0 {
47+ if err != nil {
48+ t .Errorf ("validation: got error %v, expected nil" , err )
49+ }
50+ } else if err == nil {
51+ t .Errorf ("validation: code %d: got nil, expected error" , code )
52+ }
53+ confirmStatusCode (t , code , expectedStatusCode )
54+ }
55+
2656func TestHTTPErrorResponseWithDelete (t * testing.T ) {
27- testHTTPErrorResponse (t , http .MethodDelete , contentType , "" , http .StatusMethodNotAllowed )
57+ confirmRequestValidationCode (t , http .MethodDelete , contentType , "" , http .StatusMethodNotAllowed )
2858}
2959
3060func TestHTTPErrorResponseWithPut (t * testing.T ) {
31- testHTTPErrorResponse (t , http .MethodPut , contentType , "" , http .StatusMethodNotAllowed )
61+ confirmRequestValidationCode (t , http .MethodPut , contentType , "" , http .StatusMethodNotAllowed )
3262}
3363
3464func TestHTTPErrorResponseWithMaxContentLength (t * testing.T ) {
3565 body := make ([]rune , maxRequestContentLength + 1 )
36- testHTTPErrorResponse (t ,
66+ confirmRequestValidationCode (t ,
3767 http .MethodPost , contentType , string (body ), http .StatusRequestEntityTooLarge )
3868}
3969
4070func TestHTTPErrorResponseWithEmptyContentType (t * testing.T ) {
41- testHTTPErrorResponse (t , http .MethodPost , "" , "" , http .StatusUnsupportedMediaType )
71+ confirmRequestValidationCode (t , http .MethodPost , "" , "" , http .StatusUnsupportedMediaType )
4272}
4373
4474func TestHTTPErrorResponseWithValidRequest (t * testing.T ) {
45- testHTTPErrorResponse (t , http .MethodPost , contentType , "" , 0 )
75+ confirmRequestValidationCode (t , http .MethodPost , contentType , "" , 0 )
4676}
4777
48- func testHTTPErrorResponse (t * testing.T , method , contentType , body string , expected int ) {
49- request := httptest .NewRequest (method , "http://url.com" , strings .NewReader (body ))
50- request .Header .Set ("content-type" , contentType )
51- if code , _ := validateRequest (request ); code != expected {
52- t .Fatalf ("response code should be %d not %d" , expected , code )
78+ func confirmHTTPRequestYieldsStatusCode (t * testing.T , method , contentType , body string , expectedStatusCode int ) {
79+ t .Helper ()
80+ s := Server {}
81+ ts := httptest .NewServer (& s )
82+ defer ts .Close ()
83+
84+ request , err := http .NewRequest (method , ts .URL , strings .NewReader (body ))
85+ if err != nil {
86+ t .Fatalf ("failed to create a valid HTTP request: %v" , err )
87+ }
88+ if len (contentType ) > 0 {
89+ request .Header .Set ("Content-Type" , contentType )
5390 }
91+ resp , err := http .DefaultClient .Do (request )
92+ if err != nil {
93+ t .Fatalf ("request failed: %v" , err )
94+ }
95+ confirmStatusCode (t , resp .StatusCode , expectedStatusCode )
96+ }
97+
98+ func TestHTTPResponseWithEmptyGet (t * testing.T ) {
99+ confirmHTTPRequestYieldsStatusCode (t , http .MethodGet , "" , "" , http .StatusOK )
54100}
0 commit comments