diff --git a/TDD/collect_commands_test.go b/TDD/collect_commands_test.go index 2044a1a..156408f 100644 --- a/TDD/collect_commands_test.go +++ b/TDD/collect_commands_test.go @@ -4,14 +4,12 @@ import ( "bytes" "context" "github.com/Snider/Borg/cmd" - "os" "strings" "testing" ) func TestCollectCommands(t *testing.T) { - os.Setenv("BORG_PLEXSUS", "0") - defer os.Unsetenv("BORG_PLEXSUS") + t.Setenv("BORG_PLEXSUS", "0") testCases := []struct { name string diff --git a/pkg/github/github.go b/pkg/github/github.go index a590205..51aeb0d 100644 --- a/pkg/github/github.go +++ b/pkg/github/github.go @@ -33,7 +33,7 @@ func newAuthenticatedClient(ctx context.Context) *http.Client { }, "https://api.github.com/orgs/test/repos": { StatusCode: http.StatusOK, - Body: io.NopCloser(bytes.NewBufferString(`[{"clone_url": "https.github.com/test/repo2.git"}]`)), + Body: io.NopCloser(bytes.NewBufferString(`[{"clone_url": "https://github.com/test/repo2.git"}]`)), Header: make(http.Header), }, } diff --git a/pkg/mocks/http.go b/pkg/mocks/http.go index d8b1a99..29ba838 100644 --- a/pkg/mocks/http.go +++ b/pkg/mocks/http.go @@ -15,11 +15,34 @@ type MockRoundTripper struct { func (m *MockRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { url := req.URL.String() if resp, ok := m.Responses[url]; ok { - // Create a new reader for the body each time, as it can be read only once. - bodyBytes, _ := io.ReadAll(resp.Body) + // Read the original body + bodyBytes, err := io.ReadAll(resp.Body) + if err != nil { + return nil, err + } resp.Body.Close() // close original body + + // Re-hydrate the original body so it can be read again resp.Body = io.NopCloser(bytes.NewReader(bodyBytes)) - return resp, nil + + // Create a deep copy of the response + newResp := &http.Response{ + Status: resp.Status, + StatusCode: resp.StatusCode, + Proto: resp.Proto, + ProtoMajor: resp.ProtoMajor, + ProtoMinor: resp.ProtoMinor, + Header: resp.Header.Clone(), + Body: io.NopCloser(bytes.NewReader(bodyBytes)), + ContentLength: resp.ContentLength, + TransferEncoding: resp.TransferEncoding, + Close: resp.Close, + Uncompressed: resp.Uncompressed, + Trailer: resp.Trailer.Clone(), + Request: resp.Request, + TLS: resp.TLS, + } + return newResp, nil } return &http.Response{ StatusCode: http.StatusNotFound,