From d128976e1abc60b0e3edd1535f66a849a2e0f1d9 Mon Sep 17 00:00:00 2001
From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com>
Date: Mon, 1 Sep 2025 02:02:10 +0000
Subject: [PATCH 1/3] Initial plan
From 073a6496a70b85c8eafaf7957f12bf12d16bfdc3 Mon Sep 17 00:00:00 2001
From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com>
Date: Mon, 1 Sep 2025 02:05:28 +0000
Subject: [PATCH 2/3] Initial analysis: Found bug and coverage gaps
Co-authored-by: ziflex <1607148+ziflex@users.noreply.github.com>
---
coverage.html | 162 ++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 162 insertions(+)
create mode 100644 coverage.html
diff --git a/coverage.html b/coverage.html
new file mode 100644
index 0000000..9ae8de3
--- /dev/null
+++ b/coverage.html
@@ -0,0 +1,162 @@
+
+
+
+
+
+
+
+
+
+ not tracked
+
+ not covered
+ covered
+
+
+
+
+
+
package http
+
+import (
+ "context"
+ "errors"
+ "fmt"
+ "net/http"
+ "net/url"
+
+ "github.com/go-waitfor/waitfor"
+)
+
+const Scheme = "http"
+
+type HTTP struct {
+ url *url.URL
+}
+
+func Use() waitfor.ResourceConfig {
+ return waitfor.ResourceConfig{
+ Scheme: []string{Scheme, Scheme + "s"},
+ Factory: New,
+ }
+}
+
+func New(u *url.URL) (waitfor.Resource, error) {
+ if u == nil {
+ return nil, fmt.Errorf("%q: %w", "url", waitfor.ErrInvalidArgument)
+ }
+
+ return &HTTP{u}, nil
+}
+
+func (h *HTTP) Test(ctx context.Context) error {
+ req, err := http.NewRequest(http.MethodGet, h.url.String(), nil)
+
+ if err != nil {
+ return err
+ }
+
+ client := http.Client{}
+ resp, err := client.Do(req.WithContext(ctx))
+
+ if err != nil {
+ return err
+ }
+
+ defer func() {
+ if resp.Body != nil {
+ _ = resp.Body.Close()
+ }
+ }()
+
+ if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusBadRequest {
+ return errors.New(resp.Status)
+ }
+
+ return err
+}
+
+
+
+
+
+
From fd354bd91045af3594de9ed7ac21cc9966afa349 Mon Sep 17 00:00:00 2001
From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com>
Date: Mon, 1 Sep 2025 02:09:42 +0000
Subject: [PATCH 3/3] Fix bug and improve test coverage to 94.1%
Co-authored-by: ziflex <1607148+ziflex@users.noreply.github.com>
---
.gitignore | 2 +-
coverage.html | 162 --------------------------------------------------
http.go | 2 +-
http_test.go | 76 +++++++++++++++++++++++
4 files changed, 78 insertions(+), 164 deletions(-)
delete mode 100644 coverage.html
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/coverage.html b/coverage.html
deleted file mode 100644
index 9ae8de3..0000000
--- a/coverage.html
+++ /dev/null
@@ -1,162 +0,0 @@
-
-
-
-
-
-
-
-
-
- not tracked
-
- not covered
- covered
-
-
-
-
-
-
package http
-
-import (
- "context"
- "errors"
- "fmt"
- "net/http"
- "net/url"
-
- "github.com/go-waitfor/waitfor"
-)
-
-const Scheme = "http"
-
-type HTTP struct {
- url *url.URL
-}
-
-func Use() waitfor.ResourceConfig {
- return waitfor.ResourceConfig{
- Scheme: []string{Scheme, Scheme + "s"},
- Factory: New,
- }
-}
-
-func New(u *url.URL) (waitfor.Resource, error) {
- if u == nil {
- return nil, fmt.Errorf("%q: %w", "url", waitfor.ErrInvalidArgument)
- }
-
- return &HTTP{u}, nil
-}
-
-func (h *HTTP) Test(ctx context.Context) error {
- req, err := http.NewRequest(http.MethodGet, h.url.String(), nil)
-
- if err != nil {
- return err
- }
-
- client := http.Client{}
- resp, err := client.Do(req.WithContext(ctx))
-
- if err != nil {
- return err
- }
-
- defer func() {
- if resp.Body != nil {
- _ = resp.Body.Close()
- }
- }()
-
- if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusBadRequest {
- return errors.New(resp.Status)
- }
-
- return err
-}
-
-
-
-
-
-
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
+}