diff --git a/.gitignore b/.gitignore index 106f21a..1006bf4 100644 --- a/.gitignore +++ b/.gitignore @@ -89,4 +89,4 @@ Temporary Items .apdisk bin/ -dist/ \ No newline at end of file +dist/coverage.html diff --git a/http.go b/http.go index c8fb2e4..990965f 100644 --- a/http.go +++ b/http.go @@ -55,5 +55,5 @@ func (h *HTTP) Test(ctx context.Context) error { return errors.New(resp.Status) } - return err + return nil } diff --git a/http_test.go b/http_test.go index 4c2c463..18f6e16 100644 --- a/http_test.go +++ b/http_test.go @@ -2,6 +2,7 @@ package http_test import ( "context" + "errors" "net/url" "testing" "time" @@ -105,3 +106,78 @@ func TestHTTPS(t *testing.T) { assert.NoError(t, err) } + +func TestNew_NilURL(t *testing.T) { + _, err := http.New(nil) + assert.Error(t, err) + assert.Contains(t, err.Error(), "url") +} + +func TestHTTP_NetworkError(t *testing.T) { + httpmock.Activate() + defer httpmock.DeactivateAndReset() + + uStr := "http://unreachable.test.com/" + + // Register a responder that returns a connection error + httpmock.RegisterResponder("GET", uStr, + httpmock.NewErrorResponder(errors.New("network error"))) + + u, err := url.Parse(uStr) + assert.NoError(t, err) + + r, err := http.New(u) + assert.NoError(t, err) + + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + + err = r.Test(ctx) + assert.Error(t, err) + assert.Contains(t, err.Error(), "network error") +} + +func TestHTTP_InternalServerError(t *testing.T) { + httpmock.Activate() + defer httpmock.DeactivateAndReset() + + uStr := "http://api.test.com/" + + httpmock.RegisterResponder("GET", uStr, + httpmock.NewStringResponder(500, `Internal Server Error`)) + + u, err := url.Parse(uStr) + assert.NoError(t, err) + + r, err := http.New(u) + assert.NoError(t, err) + + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + + err = r.Test(ctx) + assert.Error(t, err) + assert.Contains(t, err.Error(), "500") +} + +func TestHTTP_RedirectStatusCode(t *testing.T) { + httpmock.Activate() + defer httpmock.DeactivateAndReset() + + uStr := "http://api.test.com/" + + httpmock.RegisterResponder("GET", uStr, + httpmock.NewStringResponder(301, `Moved Permanently`)) + + u, err := url.Parse(uStr) + assert.NoError(t, err) + + r, err := http.New(u) + assert.NoError(t, err) + + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + + err = r.Test(ctx) + assert.NoError(t, err) // 3xx status codes should be considered successful +}