diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 7104634..58cbc79 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -11,23 +11,23 @@ jobs: build: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - name: Set up Go - uses: actions/setup-go@v2 + uses: actions/setup-go@v5 with: - go-version: 1.17 + go-version: 1.24.4 - - name: Build - run: go build -v ./... + - name: Install just + uses: extractions/setup-just@v3 - - name: Vet - run: go vet -v ./... - - - name: golangci-lint - uses: golangci/golangci-lint-action@v2 + - name: Install golangci-lint + uses: golangci/golangci-lint-action@v8 with: version: latest - - name: Test - run: go test -v ./... + - name: Install gosec + run: go install github.com/securego/gosec/v2/cmd/gosec@latest + + - name: Run pre-commit checks + run: just pre-commit diff --git a/Justfile b/Justfile new file mode 100644 index 0000000..29ab451 --- /dev/null +++ b/Justfile @@ -0,0 +1,81 @@ +[doc("Display available commands")] +default: + @just --list + +[doc("Build the binary")] +build: + go build -o http-assert . + +[doc("Build for release (optimized)")] +build-release: + go build -ldflags="-s -w" -o http-assert . + +[doc("Run all pre-commit checks")] +pre-commit: build vet lint test test-race test-cover security + +[doc("Build and check compilation without creating binary")] +check: + go build -o /dev/null . + +[doc("Run go vet")] +vet: + go vet ./... + +[doc("Run golangci-lint")] +lint: + golangci-lint run ./... + +[doc("Run tests")] +test: + go test ./... + +[doc("Run tests with race detection")] +test-race: + go test ./... -race + +[doc("Run tests with coverage")] +test-cover: + go test ./... -cover + +[doc("Run tests with coverage and generate HTML report")] +test-coverage: + go test ./... -coverprofile=coverage.out + go tool cover -html=coverage.out -o coverage.html + @echo "Coverage report: coverage.html" + +[doc("Clean build artifacts")] +clean: + rm -f http-assert coverage.out coverage.html + +[doc("Install the binary to $GOPATH/bin")] +install: + go install . + +[doc("Format Go code")] +fmt: + go fmt ./... + +[doc("Update dependencies")] +deps-update: + go get -u ./... + go mod tidy + +[doc("Download dependencies")] +deps-download: + go mod download + +[doc("Run a quick development cycle")] +dev: fmt vet test + +[doc("Show Go version and environment")] +info: + @echo "Go version:" + @go version + @echo "\nGo environment:" + @go env GOOS GOARCH + @echo "\nModule info:" + @go list -m + +[doc("Run security scan with gosec (if installed)")] +security: + gosec ./... \ No newline at end of file diff --git a/Makefile b/Makefile deleted file mode 100644 index a644e90..0000000 --- a/Makefile +++ /dev/null @@ -1,9 +0,0 @@ -.PHONY: pre-commit - -pre-commit: *.go - go build -o /dev/null . - go vet ./... - golangci-lint run ./... - go test ./... - go test ./... -race - go test ./... -cover diff --git a/README.md b/README.md index 7058088..61300fd 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# http-assert ![Github Actions](https://github.com/PlanitarInc/http-assert/actions/workflows/build.yml/badge.svg) [![Go Reference](https://pkg.go.dev/badge/github.com/PlanitarInc/http-assert.svg)](https://pkg.go.dev/github.com/PlanitarInc/http-assert) +# http-assert ![Github Actions](https://github.com/korya/http-assert/actions/workflows/build.yml/badge.svg) [![Go Reference](https://pkg.go.dev/badge/github.com/korya/http-assert.svg)](https://pkg.go.dev/github.com/korya/http-assert) A command-line tool for performing HTTP requests and asserting properties of the response. This tool is designed for testing HTTP endpoints, health checks, monitoring, and CI/CD pipelines. @@ -17,13 +17,13 @@ A command-line tool for performing HTTP requests and asserting properties of the ### From Source ```bash -go install github.com/PlanitarInc/http-assert@latest +go install github.com/korya/http-assert@latest ``` ### Build from Repository ```bash -git clone https://github.com/PlanitarInc/http-assert.git +git clone https://github.com/korya/http-assert.git cd http-assert go build -o http-assert . ``` diff --git a/go.mod b/go.mod index c75d121..31cc090 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ -module github.com/PlanitarInc/http-assert +module github.com/korya/http-assert -go 1.17 +go 1.24.4 require ( github.com/onsi/gomega v1.17.0 diff --git a/main.go b/main.go index 053bb3e..511d881 100644 --- a/main.go +++ b/main.go @@ -300,7 +300,7 @@ func (c Client) Do(req *http.Request, assertions ...Assertion) error { c.writeHttpDetails(&b, req, nil) return errors.New(b.String()) } - defer res.Body.Close() + defer func() { _ = res.Body.Close() }() c.logInfo("[:] %s %s\n", res.Proto, res.Status) httpRes := &httpResponse{Response: res} @@ -329,7 +329,7 @@ func (c Client) Do(req *http.Request, assertions ...Assertion) error { } func (c Client) writeHttpDetails(w io.Writer, req *http.Request, res *httpResponse) { - fmt.Fprintf(w, "\nFAILED: %s %s (%s)\n\n", req.Method, req.URL, req.Proto) + _, _ = fmt.Fprintf(w, "\nFAILED: %s %s (%s)\n\n", req.Method, req.URL, req.Proto) _ = req.Write(w) _, _ = w.Write([]byte("\n\n")) if res != nil { @@ -356,7 +356,7 @@ func (c Client) getHttpClient() *http.Client { }, } if c.SkipSslChecks { - tr.TLSClientConfig = &tls.Config{InsecureSkipVerify: true} + tr.TLSClientConfig = &tls.Config{InsecureSkipVerify: true} // #nosec G402 - user askef for it } return &http.Client{ @@ -414,19 +414,19 @@ type httpResponse struct { func (r httpResponse) writeTo(w io.Writer, withBody bool) { // Ensure to close previous body - b := r.Response.Body - defer b.Close() + b := r.Body + defer func() { _ = b.Close() }() if withBody { var b bytes.Buffer croppedBytes := printPayload(&b, r.BodyBytes, 256) if croppedBytes > 0 { fmt.Fprintf(&b, "\n\n << Payload is cropped: %d bytes are hidden >>", croppedBytes) } - r.Response.Body = io.NopCloser(&b) + r.Body = io.NopCloser(&b) } else { - r.Response.Body = io.NopCloser(strings.NewReader(" << Payload is omitted >>")) + r.Body = io.NopCloser(strings.NewReader(" << Payload is omitted >>")) } - _ = r.Response.Write(w) + _ = r.Write(w) } type hostMapping struct { diff --git a/utils.go b/utils.go index 22f0dcd..5ee83a7 100644 --- a/utils.go +++ b/utils.go @@ -30,7 +30,7 @@ func printPayload(w io.Writer, bs []byte, maxSize int) (croppedBytes int) { _, _ = w.Write(bs) } else { d := hex.Dumper(w) - defer d.Close() + defer func() { _ = d.Close() }() _, _ = d.Write(bs) } return