From ed0006446896c46b6a7b5a5b720c6d3236bae11e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=B4=8D=E1=B4=8F=E1=B4=8F=C9=B4D4=CA=80=E1=B4=8B?= Date: Sat, 2 Jul 2022 17:19:11 +0800 Subject: [PATCH] refactor: replace test case filename --- base_delete_test.go | 3 --- base_get_test.go | 57 +++++++++----------------------------------- base_head_test.go | 2 -- base_options_test.go | 2 -- base_patch_test.go | 3 --- base_post_test.go | 31 ++++-------------------- base_put_test.go | 8 ------- example_test.go | 29 ++++------------------ file_upload.go | 4 ---- file_upload_test.go | 3 --- request.go | 23 ++---------------- request_test.go | 3 --- response.go | 13 ---------- response_test.go | 1 - utils.go | 1 - 15 files changed, 21 insertions(+), 162 deletions(-) diff --git a/base_delete_test.go b/base_delete_test.go index ababe24..f1b8d6f 100644 --- a/base_delete_test.go +++ b/base_delete_test.go @@ -7,7 +7,6 @@ import ( func TestBasicDeleteRequest(t *testing.T) { resp, err := Delete("http://httpbin.org/delete", nil) - if err != nil { t.Error("Unable to make request", resp.Error) } @@ -21,7 +20,6 @@ func TestDeleteSession(t *testing.T) { session := NewSession(nil) resp, err := session.Get("http://httpbin.org/cookies/set", &RqOptions{Params: map[string]string{"one": "two"}}) - if err != nil { t.Fatal("Cannot set cookie: ", err) } @@ -87,7 +85,6 @@ func TestDeleteSession(t *testing.T) { t.Error("We should not have any other cookies: ", cookie) } } - } func TestDeleteInvalidURLSession(t *testing.T) { diff --git a/base_get_test.go b/base_get_test.go index 5a8a610..6b18735 100644 --- a/base_get_test.go +++ b/base_get_test.go @@ -301,11 +301,13 @@ func TestGetProxy(t *testing.T) { func TestGetSyncInvalidProxyScheme(t *testing.T) { resp, err := Get("http://httpbin.org/get", &RqOptions{ProxyURL: "gopher://httpbin.org"}) - if err != nil { - t.Error("Request failed: ", err) + if err == ErrInvalidProxyURL { + t.Log("Got expected error: ", err) + return + } + if err == nil { + verifyOkResponse(resp, t) } - - verifyOkResponse(resp, t) } func TestGetSyncNoOptions(t *testing.T) { @@ -339,7 +341,6 @@ func TestGetWithCookies(t *testing.T) { }, }, }) - if err != nil { t.Error("Unable to make request", err) } @@ -361,7 +362,6 @@ func TestGetWithCookies(t *testing.T) { if myJSONStruct.Cookies.AnotherCookie != "Some Value" { t.Errorf("Cookie value not set properly: %#v", myJSONStruct) } - } func TestGetWithCookiesCustomCookieJar(t *testing.T) { @@ -383,7 +383,6 @@ func TestGetWithCookiesCustomCookieJar(t *testing.T) { }, }, }) - if err != nil { t.Error("Unable to make request", err) } @@ -405,14 +404,12 @@ func TestGetWithCookiesCustomCookieJar(t *testing.T) { if myJSONStruct.Cookies.AnotherCookie != "Some Value" { t.Errorf("Cookie value not set properly: %#v", myJSONStruct) } - } func TestGetSession(t *testing.T) { session := NewSession(nil) resp, err := session.Get("http://httpbin.org/cookies/set", &RqOptions{Params: map[string]string{"one": "two"}}) - if err != nil { t.Fatal("Cannot set cookie: ", err) } @@ -470,7 +467,6 @@ func TestGetSession(t *testing.T) { } session.CloseIdleConnections() - } // func TestGetNoOptionsDeflate(t *testing.T) { @@ -509,7 +505,6 @@ func TestGetInvalidURLSession(t *testing.T) { func TestGetXMLSerialize(t *testing.T) { resp, err := Get("http://httpbin.org/xml", nil) - if err != nil { t.Error("Unable to make request", err) } @@ -531,7 +526,6 @@ func TestGetXMLSerialize(t *testing.T) { if err := resp.XML(123, nil); err == nil { t.Error("Still able to consume XML from used response") } - } func TestGetCustomUserAgent(t *testing.T) { @@ -547,7 +541,6 @@ func TestGetBasicAuth(t *testing.T) { ro := &RqOptions{Auth: []string{"Levi", "Bot"}} resp, err := Get("http://httpbin.org/get", ro) // Not the usual JSON so copy and paste from below - if err != nil { t.Error("Unable to make request", err) } @@ -566,15 +559,15 @@ func TestGetBasicAuth(t *testing.T) { if myJSONStruct.Headers.Authorization != "Basic TGV2aTpCb3Q=" { t.Error("Unable to set HTTP basic auth", myJSONStruct.Headers) } - } func TestGetCustomHeader(t *testing.T) { - ro := &RqOptions{UserAgent: "LeviBot 0.1", - Headers: map[string]string{"X-Wonderful-Header": "1"}} + ro := &RqOptions{ + UserAgent: "LeviBot 0.1", + Headers: map[string]string{"X-Wonderful-Header": "1"}, + } resp, err := Get("http://httpbin.org/get", ro) // Not the usual JSON so copy and paste from below - if err != nil { t.Error("Unable to make request", err) } @@ -610,7 +603,6 @@ func TestGetInvalidSSLCertNoVerify(t *testing.T) { t.Error("Request did not return OK") } } - } func TestGetInvalidSSLCertNoVerifyNoOptions(t *testing.T) { @@ -641,7 +633,6 @@ func TestGetInvalidSSLCertNoCompression(t *testing.T) { if resp.Ok == true { t.Error("Request did return OK") } - } func TestGetInvalidSSLCertWithCompression(t *testing.T) { @@ -655,7 +646,6 @@ func TestGetInvalidSSLCertWithCompression(t *testing.T) { if resp.Ok == true { t.Error("Request did return OK") } - } func TestErrorResponseNOOP(t *testing.T) { @@ -711,13 +701,11 @@ func TestErrorResponseNOOP(t *testing.T) { if written, err := resp.Read(buf[:]); written != -1 && err == nil { t.Error("Somehow we were able to read from our error response") } - } func TestGetInvalidSSLCertNoCompressionNoVerify(t *testing.T) { ro := &RqOptions{UserAgent: "LeviBot 0.1", InsecureSkipVerify: true, DisableCompression: true} resp, err := Get("https://self-signed.badssl.com/", ro) - if err != nil { t.Error("SSL verification worked when it shouldn't of", err) } @@ -725,13 +713,11 @@ func TestGetInvalidSSLCertNoCompressionNoVerify(t *testing.T) { if resp.Ok != true { t.Error("Request did return OK") } - } func TestGetInvalidSSLCertWithCompressionNoVerify(t *testing.T) { ro := &RqOptions{UserAgent: "LeviBot 0.1", InsecureSkipVerify: true, DisableCompression: false} resp, err := Get("https://self-signed.badssl.com/", ro) - if err != nil { t.Error("SSL verification worked when it shouldn't of", err) } @@ -739,7 +725,6 @@ func TestGetInvalidSSLCertWithCompressionNoVerify(t *testing.T) { if resp.Ok != true { t.Error("Request did return OK") } - } func TestGetInvalidSSLCert(t *testing.T) { @@ -753,7 +738,6 @@ func TestGetInvalidSSLCert(t *testing.T) { if resp.Ok == true { t.Error("Request did return OK") } - } func TestGetBasicArgs(t *testing.T) { @@ -763,7 +747,6 @@ func TestGetBasicArgs(t *testing.T) { resp, _ := Get("http://httpbin.org/get?Goodbye=World", ro) verifyOkArgsResponse(resp, t) - } func TestGetBasicArgsQueryStruct(t *testing.T) { @@ -777,7 +760,6 @@ func TestGetBasicArgsQueryStruct(t *testing.T) { resp, _ := Get("http://httpbin.org/get?Goodbye=World", ro) verifyOkArgsResponse(resp, t) - } func TestGetBasicArgsQueryStructErr(t *testing.T) { @@ -793,7 +775,6 @@ func TestGetBasicArgsQueryStructErr(t *testing.T) { if resp.Ok == true { t.Error("Request did return OK") } - } func TestGetBasicArgsQueryStructUrlQueryErr(t *testing.T) { @@ -809,7 +790,6 @@ func TestGetBasicArgsQueryStructUrlQueryErr(t *testing.T) { if resp.Ok == true { t.Error("Request did return OK") } - } func TestGetBasicArgsQueryStructUrlErr(t *testing.T) { @@ -825,7 +805,6 @@ func TestGetBasicArgsQueryStructUrlErr(t *testing.T) { if resp.Ok == true { t.Error("Request did return OK") } - } func TestGetBasicArgsErr(t *testing.T) { @@ -841,7 +820,6 @@ func TestGetBasicArgsErr(t *testing.T) { if resp.Ok == true { t.Error("Request did return OK") } - } func TestGetBasicArgsParams(t *testing.T) { @@ -911,12 +889,10 @@ func TestGetFileDownload(t *testing.T) { if resp.StatusCode != 200 { t.Error("Response returned a non-200 code") } - } func TestJsonConsumedResponse(t *testing.T) { resp, err := Get("http://httpbin.org/get", nil) - if err != nil { t.Error("Unable to make request", err) } @@ -938,7 +914,6 @@ func TestJsonConsumedResponse(t *testing.T) { func TestDownloadConsumedResponse(t *testing.T) { resp, err := Get("http://httpbin.org/get", nil) - if err != nil { t.Error("Unable to make request", err) } @@ -962,7 +937,6 @@ func TestDownloadConsumedResponse(t *testing.T) { func TestGetBytes(t *testing.T) { resp, err := Get("http://httpbin.org/get", nil) - if err != nil { t.Error("Unable to make request", err) } @@ -982,7 +956,6 @@ func TestGetBytes(t *testing.T) { func TestGetBytesNoBuffer(t *testing.T) { resp, err := Get("http://httpbin.org/get", nil) - if err != nil { t.Error("Unable to make request", err) } @@ -1014,7 +987,6 @@ func TestGetBytesNoBuffer(t *testing.T) { func TestGetString(t *testing.T) { resp, err := Get("http://httpbin.org/get", nil) - if err != nil { t.Error("Unable to make request", err) } @@ -1042,7 +1014,6 @@ func TestGetString(t *testing.T) { if resp.String() != "" { t.Error("Internal Buffer not cleaned up") } - } func TestGetRedirectHeaderCopy(t *testing.T) { @@ -1054,7 +1025,6 @@ func TestGetRedirectHeaderCopy(t *testing.T) { } }) resp, err := Get(srv.URL+"/foo", &RqOptions{Headers: map[string]string{"X-Custom": "1"}}) - if err != nil { t.Error("Redirect request failed", err) } @@ -1064,7 +1034,6 @@ func TestGetRedirectHeaderCopy(t *testing.T) { } srv.Close() - } func TestGetRedirectSecretHeaderNoCopy(t *testing.T) { @@ -1078,7 +1047,6 @@ func TestGetRedirectSecretHeaderNoCopy(t *testing.T) { resp, err := Get(srv.URL+"/sec", &RqOptions{ Headers: map[string]string{"X-Custom": "1"}, SensitiveHTTPHeaders: map[string]struct{}{"X-Custom": {}}, }) - if err != nil { t.Error("Redirect request failed", err) } @@ -1088,14 +1056,13 @@ func TestGetRedirectSecretHeaderNoCopy(t *testing.T) { } srv.Close() - } func TestMassiveJSONFile(t *testing.T) { if testing.Short() { t.Skip("Skipping massive JSON file download because short was called") } - resp, err := Get("https://raw.githubusercontent.com/moond4rk/sf-city-lots-json/master/citylots.json", nil) + resp, err := Get("https://raw.githubusercontent.com/levigross/sf-city-lots-json/master/citylots.json", nil) if err != nil { t.Error("Request to massive JSON blob failed", err) } @@ -1169,7 +1136,6 @@ func TestAuthStripOnRedirect(t *testing.T) { Auth: []string{"one ", "two"}, Headers: map[string]string{"WWW-Authenticate": "foo", "Proxy-Authorization": "bar"}, }) - if err != nil { t.Error("Request had creds inside", err) } @@ -1202,7 +1168,6 @@ func TestNoRedirect(t *testing.T) { } srv.Close() - } func verifyOkArgsResponse(resp *Response, t *testing.T) *BasicGetResponseArgs { diff --git a/base_head_test.go b/base_head_test.go index ce86172..5ee339e 100644 --- a/base_head_test.go +++ b/base_head_test.go @@ -47,7 +47,6 @@ func TestHeadSession(t *testing.T) { session := NewSession(nil) resp, err := session.Head("http://httpbin.org/cookies/set", &RqOptions{Params: map[string]string{"one": "two"}}) - if err != nil { t.Fatal("Cannot set cookie: ", err) } @@ -103,7 +102,6 @@ func TestHeadSession(t *testing.T) { t.Error("We should not have any other cookies: ", cookie) } } - } func TestHeadInvalidURLSession(t *testing.T) { diff --git a/base_options_test.go b/base_options_test.go index 6b3be60..e195096 100644 --- a/base_options_test.go +++ b/base_options_test.go @@ -24,7 +24,6 @@ func TestOptionsSession(t *testing.T) { session := NewSession(nil) resp, err := session.Options("http://httpbin.org/cookies/set", &RqOptions{Params: map[string]string{"one": "two"}}) - if err != nil { t.Fatal("Cannot set cookie: ", err) } @@ -76,7 +75,6 @@ func TestOptionsSession(t *testing.T) { t.Error("We should not have any other cookies: ", cookie) } } - } func TestOptionsInvalidURLSession(t *testing.T) { diff --git a/base_patch_test.go b/base_patch_test.go index 6e95ce8..5ac27b0 100644 --- a/base_patch_test.go +++ b/base_patch_test.go @@ -7,7 +7,6 @@ import ( func TestBasicPatchRequest(t *testing.T) { resp, err := Patch("http://httpbin.org/patch", nil) - if err != nil { t.Error("Unable to make request", resp.Error) } @@ -21,7 +20,6 @@ func TestPatchSession(t *testing.T) { session := NewSession(nil) resp, err := session.Get("http://httpbin.org/cookies/set", &RqOptions{Params: map[string]string{"one": "two"}}) - if err != nil { t.Fatal("Cannot set cookie: ", err) } @@ -87,7 +85,6 @@ func TestPatchSession(t *testing.T) { t.Error("We should not have any other cookies: ", cookie) } } - } func TestPatchInvalidURLSession(t *testing.T) { diff --git a/base_post_test.go b/base_post_test.go index 23b27df..8a45fd7 100644 --- a/base_post_test.go +++ b/base_post_test.go @@ -115,30 +115,28 @@ func TestBasicPostRequest(t *testing.T) { resp, _ := Post("http://httpbin.org/post", &RqOptions{Data: map[string]string{"One": "Two"}}) verifyOkPostResponse(resp, t) - } func TestBasicRegularPostRequest(t *testing.T) { resp, err := Post("http://httpbin.org/post", &RqOptions{Data: map[string]string{"One": "Two"}}) - if err != nil { t.Error("Cannot post: ", err) } verifyOkPostResponse(resp, t) - } func TestBasicPostRequestInvalidURL(t *testing.T) { resp, _ := Post("%../dir/", - &RqOptions{Data: map[string]string{"One": "Two"}, - Params: map[string]string{"1": "2"}}) + &RqOptions{ + Data: map[string]string{"One": "Two"}, + Params: map[string]string{"1": "2"}, + }) if resp.Error == nil { t.Error("Somehow the request went through") } - } func TestBasicPostRequestInvalidURLNoParams(t *testing.T) { @@ -147,7 +145,6 @@ func TestBasicPostRequestInvalidURLNoParams(t *testing.T) { if resp.Error == nil { t.Error("Somehow the request went through") } - } func TestSessionPostRequestInvalidURLNoParams(t *testing.T) { @@ -156,7 +153,6 @@ func TestSessionPostRequestInvalidURLNoParams(t *testing.T) { if _, err := session.Post("%../dir/", &RqOptions{Data: map[string]string{"One": "Two"}}); err == nil { t.Error("Somehow the request went through") } - } func TestXMLPostRequestInvalidURL(t *testing.T) { @@ -256,9 +252,7 @@ func TestSessionPostJsonRequestInvalidXML(t *testing.T) { } func TestBasicPostRequestUploadInvalidURL(t *testing.T) { - fd, err := FileUploadFromDisk("testdata/mypassword") - if err != nil { t.Error("Unable to open file: ", err) } @@ -280,7 +274,6 @@ func TestSessionPostRequestUploadInvalidURL(t *testing.T) { session := NewSession(nil) fd, err := FileUploadFromDisk("testdata/mypassword") - if err != nil { t.Error("Unable to open file: ", err) } @@ -299,7 +292,6 @@ func TestSessionPostRequestUploadInvalidURL(t *testing.T) { } func TestBasicPostRequestUploadInvalidFileUpload(t *testing.T) { - resp, _ := Post("%../dir/", &RqOptions{ Files: []FileUpload{{FileName: `\x00%'"üfdsufhid\Ä\"D\\\"JS%25//'"H•\\\\'"¶•ªç∂\uf8\x8AKÔÓÔ`, FileContents: nil}}, @@ -349,7 +341,6 @@ func TestXMLPostRequest(t *testing.T) { if myXMLStruct.Age != 1 { t.Errorf("Unable to serialize XML response from within JSON %#v ", myXMLStruct) } - } func TestXMLPostRequestReaderBody(t *testing.T) { @@ -383,7 +374,6 @@ func TestXMLPostRequestReaderBody(t *testing.T) { if myXMLStruct.Age != 1 { t.Errorf("Unable to serialize XML response from within JSON %#v ", myXMLStruct) } - } func TestXMLMarshaledStringPostRequest(t *testing.T) { @@ -477,16 +467,13 @@ func TestBasicPostRequestUploadErrorEOFReader(t *testing.T) { Files: []FileUpload{{FileName: "Random.test", FileContents: rd}}, Data: map[string]string{"One": "Two"}, }) - if err != nil { t.Error("Somehow our test didn't fail... ", err) } } func TestBasicPostRequestUpload(t *testing.T) { - fd, err := FileUploadFromDisk("testdata/mypassword") - if err != nil { t.Error("Unable to open file: ", err) } @@ -541,9 +528,7 @@ func TestBasicPostRequestUpload(t *testing.T) { } func TestBasicPostRequestUploadWithMime(t *testing.T) { - fd, err := os.Open("testdata/mypassword") - if err != nil { t.Error("Unable to open file: ", err) } @@ -603,9 +588,7 @@ func TestBasicPostRequestUploadWithMime(t *testing.T) { } func TestBasicPostRequestUploadMultipleFiles(t *testing.T) { - fd, err := FileUploadFromGlob("testdata/*") - if err != nil { t.Error("Unable to glob file: ", err) } @@ -660,7 +643,6 @@ func TestBasicPostRequestUploadMultipleFiles(t *testing.T) { if myJSONStruct.Form.One != "Two" { t.Error("Unable to parse form properly", myJSONStruct.Form) } - } func TestBasicPostJsonBytesRequest(t *testing.T) { @@ -712,7 +694,6 @@ func TestBasicPostJsonBytesRequest(t *testing.T) { if myJSONStruct.Headers.XRequestedWith != "XMLHttpRequest" { t.Error("Invalid requested header: ", myJSONStruct.Headers.XRequestedWith) } - } func TestBasicPostJsonStringRequest(t *testing.T) { @@ -764,7 +745,6 @@ func TestBasicPostJsonStringRequest(t *testing.T) { if myJSONStruct.Headers.XRequestedWith != "XMLHttpRequest" { t.Error("Invalid requested header: ", myJSONStruct.Headers.XRequestedWith) } - } func TestBasicPostJsonRequest(t *testing.T) { @@ -816,14 +796,12 @@ func TestBasicPostJsonRequest(t *testing.T) { if myJSONStruct.Headers.XRequestedWith != "XMLHttpRequest" { t.Error("Invalid requested header: ", myJSONStruct.Headers.XRequestedWith) } - } func TestPostSession(t *testing.T) { session := NewSession(nil) resp, err := session.Get("http://httpbin.org/cookies/set", &RqOptions{Params: map[string]string{"one": "two"}}) - if err != nil { t.Fatal("Cannot set cookie: ", err) } @@ -889,7 +867,6 @@ func TestPostSession(t *testing.T) { t.Error("We should not have any other cookies: ", cookie) } } - } // verifyResponse will verify the following conditions diff --git a/base_put_test.go b/base_put_test.go index affd102..921feb1 100644 --- a/base_put_test.go +++ b/base_put_test.go @@ -7,7 +7,6 @@ import ( func TestBasicPutRequest(t *testing.T) { resp, err := Put("http://httpbin.org/put", nil) - if err != nil { t.Error("Unable to make request", resp.Error) } @@ -15,12 +14,10 @@ func TestBasicPutRequest(t *testing.T) { if resp.Ok != true { t.Error("Request did not return OK") } - } func TestBasicPutUploadRequest(t *testing.T) { fd, err := FileUploadFromDisk("testdata/mypassword") - if err != nil { t.Error("Unable to open file: ", err) } @@ -38,12 +35,10 @@ func TestBasicPutUploadRequest(t *testing.T) { if resp.Ok != true { t.Error("Request did not return OK") } - } func TestBasicPutUploadRequestInvalidURL(t *testing.T) { fd, err := FileUploadFromDisk("testdata/mypassword") - if err != nil { t.Error("Unable to open file: ", err) } @@ -61,7 +56,6 @@ func TestBasicPutUploadRequestInvalidURL(t *testing.T) { func TestSessionPutUploadRequestInvalidURL(t *testing.T) { fd, err := FileUploadFromDisk("testdata/mypassword") - if err != nil { t.Error("Unable to open file: ", err) } @@ -83,7 +77,6 @@ func TestPutSession(t *testing.T) { session := NewSession(nil) resp, err := session.Get("http://httpbin.org/cookies/set", &RqOptions{Params: map[string]string{"one": "two"}}) - if err != nil { t.Fatal("Cannot set cookie: ", err) } @@ -149,7 +142,6 @@ func TestPutSession(t *testing.T) { t.Error("We should not have any other cookies: ", cookie) } } - } func TestPutInvalidURLSession(t *testing.T) { diff --git a/example_test.go b/example_test.go index 01513d7..869b65f 100644 --- a/example_test.go +++ b/example_test.go @@ -13,7 +13,6 @@ import ( func Example_basicGet() { // This is a very basic GET request resp, err := req.Get("http://httpbin.org/get", nil) - if err != nil { log.Println(err) } @@ -28,7 +27,6 @@ func Example_basicGet() { func Example_basicGetCustomHTTPClient() { // This is a very basic GET request resp, err := req.Get("http://httpbin.org/get", &req.RqOptions{HTTPClient: http.DefaultClient}) - if err != nil { log.Println(err) } @@ -48,7 +46,6 @@ func Example_proxy() { resp, err := req.Get("http://www.moond4rk.com/", &req.RqOptions{ProxyURL: proxyURL.String()}) - if err != nil { log.Println(err) } @@ -77,7 +74,6 @@ func Example_cookies() { }, }, }) - if err != nil { log.Println("Unable to make request", err) } @@ -93,7 +89,6 @@ func Example_session() { session := req.NewSession(nil) resp, err := session.Get("http://httpbin.org/cookies/set", &req.RqOptions{Params: map[string]string{"one": "two"}}) - if err != nil { log.Fatal("Cannot set cookie: ", err) } @@ -103,7 +98,6 @@ func Example_session() { } log.Println(resp.String()) - } func Example_parse_XML() { @@ -119,7 +113,6 @@ func Example_parse_XML() { } resp, err := req.Get("http://httpbin.org/xml", nil) - if err != nil { log.Println("Unable to make request", err) } @@ -150,7 +143,6 @@ func Example_parse_XML() { func Example_customUserAgent() { ro := &req.RqOptions{UserAgent: "LeviBot 0.1"} resp, err := req.Get("http://httpbin.org/get", ro) - if err != nil { log.Fatal("Oops something went wrong: ", err) } @@ -166,7 +158,6 @@ func Example_basicAuth() { ro := &req.RqOptions{Auth: []string{"Levi", "Bot"}} resp, err := req.Get("http://httpbin.org/get", ro) // Not the usual JSON so copy and paste from below - if err != nil { log.Println("Unable to make request", err) } @@ -177,11 +168,12 @@ func Example_basicAuth() { } func Example_customHTTPHeader() { - ro := &req.RqOptions{UserAgent: "LeviBot 0.1", - Headers: map[string]string{"X-Wonderful-Header": "1"}} + ro := &req.RqOptions{ + UserAgent: "LeviBot 0.1", + Headers: map[string]string{"X-Wonderful-Header": "1"}, + } resp, err := req.Get("http://httpbin.org/get", ro) // Not the usual JSON so copy and paste from below - if err != nil { log.Println("Unable to make request", err) } @@ -194,7 +186,6 @@ func Example_customHTTPHeader() { func Example_acceptInvalidTLSCert() { ro := &req.RqOptions{InsecureSkipVerify: true} resp, err := req.Get("https://www.pcwebshop.co.uk/", ro) - if err != nil { log.Println("Unable to make request", err) } @@ -210,7 +201,6 @@ func Example_urlQueryParams() { } resp, err := req.Get("http://httpbin.org/get", ro) // url will now be http://httpbin.org/get?hello=world&goodbye=world - if err != nil { log.Println("Unable to make request", err) } @@ -222,7 +212,6 @@ func Example_urlQueryParams() { func Example_downloadFile() { resp, err := req.Get("http://httpbin.org/get", nil) - if err != nil { log.Println("Unable to make request", err) } @@ -238,15 +227,12 @@ func Example_downloadFile() { if err != nil { log.Println("Unable to download file", err) } - } func Example_postForm() { resp, err := req.Post("http://httpbin.org/post", &req.RqOptions{Data: map[string]string{"One": "Two"}}) - // This is the basic form POST. The request body will be `one=two` - if err != nil { log.Println("Cannot post: ", err) } @@ -257,7 +243,6 @@ func Example_postForm() { } func Example_postXML() { - type XMLPostMessage struct { Name string Age int @@ -267,7 +252,6 @@ func Example_postXML() { resp, err := req.Post("http://httpbin.org/post", &req.RqOptions{XML: XMLPostMessage{Name: "Human", Age: 1, Height: 1}}) // The request body will contain the XML generated by the `XMLPostMessage` struct - if err != nil { log.Println("Unable to make request", err.Error()) } @@ -278,9 +262,7 @@ func Example_postXML() { } func Example_postFileUpload() { - fd, err := req.FileUploadFromDisk("test_files/mypassword") - if err != nil { log.Println("Unable to open file: ", err) } @@ -291,7 +273,6 @@ func Example_postFileUpload() { Files: fd, Data: map[string]string{"One": "Two"}, }) - if err != nil { log.Println("Unable to make request", resp.Error) } @@ -307,7 +288,6 @@ func Example_postJSONAJAX() { JSON: map[string]string{"One": "Two"}, IsAjax: true, // this adds the X-Requested-With: XMLHttpRequest header }) - if err != nil { log.Println("Unable to make request", resp.Error) } @@ -315,7 +295,6 @@ func Example_postJSONAJAX() { if resp.Ok != true { log.Println("Request did not return OK") } - } func xmlASCIIDecoder(charset string, input io.Reader) (io.Reader, error) { diff --git a/file_upload.go b/file_upload.go index c534a1f..e3fd691 100644 --- a/file_upload.go +++ b/file_upload.go @@ -27,20 +27,17 @@ type FileUpload struct { // FileUploadFromDisk allows you to create a FileUpload struct slice by just specifying a location on the disk func FileUploadFromDisk(fileName string) ([]FileUpload, error) { fd, err := os.Open(fileName) - if err != nil { return nil, err } return []FileUpload{{FileContents: fd, FileName: fileName}}, nil - } // FileUploadFromGlob allows you to create a FileUpload struct slice by just specifying a glob location on the disk // this function will gloss over all errors in the files and only upload the files that don't return errors from the glob func FileUploadFromGlob(fileSystemGlob string) ([]FileUpload, error) { files, err := filepath.Glob(fileSystemGlob) - if err != nil { return nil, err } @@ -64,5 +61,4 @@ func FileUploadFromGlob(fileSystemGlob string) ([]FileUpload, error) { } return filesToUpload, nil - } diff --git a/file_upload_test.go b/file_upload_test.go index 0f975fb..7c3747f 100644 --- a/file_upload_test.go +++ b/file_upload_test.go @@ -15,7 +15,6 @@ func TestErrorOpenFile(t *testing.T) { func TestGLOBFiles(t *testing.T) { fd, err := FileUploadFromGlob("testdata/*") - if err != nil { t.Error("Got an invalid GLOB: ", err) } @@ -39,7 +38,6 @@ func TestNoGlobFiles(t *testing.T) { func TestGlobWithDir(t *testing.T) { fd, err := FileUploadFromGlob("*test*") - if err != nil { t.Error("Glob failed", err) } @@ -49,5 +47,4 @@ func TestGlobWithDir(t *testing.T) { t.Error(f, "is a dir (which cannot be uploaded)") } } - } diff --git a/request.go b/request.go index a415f9c..0f43258 100644 --- a/request.go +++ b/request.go @@ -27,20 +27,13 @@ import ( // RqOptions is the location that of where the data type RqOptions struct { - // Data is a map of key values that will eventually convert into // the body of a POST request. Data map[string]string - // RawData is string that will be directly written to the body - RawData string - // Params is a map of query strings that may be used within a GET request Params map[string]string - // RawParams is a string that will be directly written to the query string - RawParams string - // QueryStruct is a struct that encapsulates a set of URL query params // this paramter is mutually exclusive with `Params map[string]string` (they cannot be combined) // for more information please see https://godoc.org/github.com/google/go-querystring/query @@ -190,7 +183,6 @@ func buildRequest(httpMethod, url string, ro *RqOptions, httpClient *http.Client // Build the request req, err := buildHTTPRequest(httpMethod, url, ro) - if err != nil { return nil, err } @@ -251,7 +243,6 @@ func createFileUploadRequest(httpMethod, userURL string, ro *RqOptions) (*http.R // when uploading using PUT or PATCH req, err := http.NewRequest(httpMethod, userURL, ro.Files[0].FileContents) - if err != nil { return nil, err } @@ -259,7 +250,6 @@ func createFileUploadRequest(httpMethod, userURL string, ro *RqOptions) (*http.R req.Header.Set("Content-Type", mime.TypeByExtension(ro.Files[0].FileName)) return req, nil - } func createBasicXMLRequest(httpMethod, userURL string, ro *RqOptions) (*http.Request, error) { @@ -286,8 +276,8 @@ func createBasicXMLRequest(httpMethod, userURL string, ro *RqOptions) (*http.Req req.Header.Set("Content-Type", "application/xml") return req, nil - } + func createMultiPartPostRequest(httpMethod, userURL string, ro *RqOptions) (*http.Request, error) { requestBody := &bytes.Buffer{} @@ -348,7 +338,6 @@ func createMultiPartPostRequest(httpMethod, userURL string, ro *RqOptions) (*htt } req, err := http.NewRequest(httpMethod, userURL, requestBody) - if err != nil { return nil, err } @@ -359,7 +348,6 @@ func createMultiPartPostRequest(httpMethod, userURL string, ro *RqOptions) (*htt } func createBasicJSONRequest(httpMethod, userURL string, ro *RqOptions) (*http.Request, error) { - var reader io.Reader switch ro.JSON.(type) { case string: @@ -382,12 +370,10 @@ func createBasicJSONRequest(httpMethod, userURL string, ro *RqOptions) (*http.Re req.Header.Set("Content-Type", "application/json") return req, nil - } -func createBasicRequest(httpMethod, userURL string, ro *RqOptions) (*http.Request, error) { +func createBasicRequest(httpMethod, userURL string, ro *RqOptions) (*http.Request, error) { req, err := http.NewRequest(httpMethod, userURL, strings.NewReader(encodePostValues(ro.Data))) - if err != nil { return nil, err } @@ -456,7 +442,6 @@ func (ro RqOptions) dontUseDefaultClient() bool { // BuildHTTPClient is a function that will return a custom HTTP client based on the request options provided // the check is in UseDefaultClient func BuildHTTPClient(ro RqOptions) *http.Client { - if ro.HTTPClient != nil { return ro.HTTPClient } @@ -532,13 +517,11 @@ func createHTTPTransport(ro RqOptions) *http.Transport { // That is what the "magic" is on the last line func buildURLParams(userURL string, params map[string]string) (string, error) { parsedURL, err := url.Parse(userURL) - if err != nil { return "", err } parsedQuery, err := url.ParseQuery(parsedURL.RawQuery) - if err != nil { return "", nil } @@ -590,13 +573,11 @@ func addQueryParams(parsedURL *url.URL, parsedQuery url.Values) string { func buildURLStruct(userURL string, URLStruct interface{}) (string, error) { parsedURL, err := url.Parse(userURL) - if err != nil { return "", err } parsedQuery, err := url.ParseQuery(parsedURL.RawQuery) - if err != nil { return "", err } diff --git a/request_test.go b/request_test.go index 9fd67b5..0d65502 100644 --- a/request_test.go +++ b/request_test.go @@ -4,7 +4,6 @@ import "testing" func TestAddQueryStringParams(t *testing.T) { userURL, err := buildURLParams("https://www.google.com/", map[string]string{"1": "2", "3": "4"}) - if err != nil { t.Error("URL Parse Error: ", err) } @@ -16,7 +15,6 @@ func TestAddQueryStringParams(t *testing.T) { func TestSortAddQueryStringParams(t *testing.T) { userURL, err := buildURLParams("https://www.google.com/", map[string]string{"3": "4", "1": "2"}) - if err != nil { t.Error("URL Parse Error: ", err) } @@ -28,7 +26,6 @@ func TestSortAddQueryStringParams(t *testing.T) { func TestAddQueryStringParamsExistingParam(t *testing.T) { userURL, err := buildURLParams("https://www.google.com/?5=6", map[string]string{"3": "4", "1": "2"}) - if err != nil { t.Error("URL Parse Error: ", err) } diff --git a/response.go b/response.go index c51d82b..49503ce 100644 --- a/response.go +++ b/response.go @@ -12,7 +12,6 @@ import ( // Response is what is returned to a user when they fire off a request type Response struct { - // Ok is a boolean flag that validates that the server returned a 2xx code Ok bool @@ -53,7 +52,6 @@ func buildResponse(resp *http.Response, err error) (*Response, error) { // Read is part of our ability to support io.ReadCloser if someone wants to make use of the raw body func (r *Response) Read(p []byte) (n int, err error) { - if r.Error != nil { return -1, r.Error } @@ -63,7 +61,6 @@ func (r *Response) Read(p []byte) (n int, err error) { // Close is part of our ability to support io.ReadCloser if someone wants to make use of the raw body func (r *Response) Close() error { - if r.Error != nil { return r.Error } @@ -75,13 +72,11 @@ func (r *Response) Close() error { // DownloadToFile allows you to download the contents of the response to a file func (r *Response) DownloadToFile(fileName string) error { - if r.Error != nil { return r.Error } fd, err := os.Create(fileName) - if err != nil { return err } @@ -99,7 +94,6 @@ func (r *Response) DownloadToFile(fileName string) error { // getInternalReader because we implement io.ReadCloser and optionally hold a large buffer of the response (created by // the user's request) func (r *Response) getInternalReader() io.Reader { - if r.internalByteBuffer.Len() != 0 { return r.internalByteBuffer } @@ -109,7 +103,6 @@ func (r *Response) getInternalReader() io.Reader { // XML is a method that will populate a struct that is provided `userStruct` with the XML returned within the // response body func (r *Response) XML(userStruct interface{}, charsetReader XMLCharDecoder) error { - if r.Error != nil { return r.Error } @@ -128,7 +121,6 @@ func (r *Response) XML(userStruct interface{}, charsetReader XMLCharDecoder) err // JSON is a method that will populate a struct that is provided `userStruct` with the JSON returned within the // response body func (r *Response) JSON(userStruct interface{}) error { - if r.Error != nil { return r.Error } @@ -142,7 +134,6 @@ func (r *Response) JSON(userStruct interface{}) error { // createResponseBytesBuffer is a utility method that will populate the internal byte reader – this is largely used for .String() // and .Bytes() func (r *Response) populateResponseByteBuffer() { - // Have I done this already? if r.internalByteBuffer.Len() != 0 { return @@ -164,12 +155,10 @@ func (r *Response) populateResponseByteBuffer() { r.Error = err r.RawResponse.Body.Close() } - } // Bytes returns the response as a byte array func (r *Response) Bytes() []byte { - if r.Error != nil { return nil } @@ -181,7 +170,6 @@ func (r *Response) Bytes() []byte { return nil } return r.internalByteBuffer.Bytes() - } // String returns the response as a string @@ -198,7 +186,6 @@ func (r *Response) String() string { // ClearInternalBuffer is a function that will clear the internal buffer that we use to hold the .String() and .Bytes() // data. Once you have used these functions – you may want to free up the memory. func (r *Response) ClearInternalBuffer() { - if r == nil || r.internalByteBuffer == nil { return } diff --git a/response_test.go b/response_test.go index df201ad..2e99517 100644 --- a/response_test.go +++ b/response_test.go @@ -15,7 +15,6 @@ func TestResponseOk(t *testing.T) { func verifyResponseOkForStatus(status int, t *testing.T) { url := "http://httpbin.org/status/" + strconv.Itoa(status) resp, err := Get(url, nil) - if err != nil { t.Error("Unable to make request", err) } diff --git a/utils.go b/utils.go index 863f327..8279a51 100644 --- a/utils.go +++ b/utils.go @@ -79,7 +79,6 @@ func addRedirectFunctionality(client *http.Client, ro *RqOptions) { return } client.CheckRedirect = func(req *http.Request, via []*http.Request) error { - if ro.RedirectLimit < 0 { return http.ErrUseLastResponse }