diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml new file mode 100644 index 0000000..a64bc40 --- /dev/null +++ b/.github/workflows/go.yml @@ -0,0 +1,25 @@ +name: Go + +on: + push: + branches: [ '*' ] + pull_request: + branches: [ main ] + +jobs: + + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: 1.21.5 + + - name: Build + run: go build -v ./... + + - name: Test + run: go test -v ./... diff --git a/README.md b/README.md index d055b76..b3d810a 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ +![Workflow](https://github.com/binalyze/httpreq/actions/workflows/go.yml/badge.svg) # httpreq @@ -75,4 +76,7 @@ Here is an example to use some helper methods of `httpreq`. You can find more ex // Headers headers := resp.Headers() + + // Cookies + cookies := resp.Cookies() ``` diff --git a/go.mod b/go.mod index 78b9d14..af1a962 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,11 @@ module github.com/binalyze/httpreq -go 1.16 +go 1.21.5 -require github.com/stretchr/testify v1.7.0 +require github.com/stretchr/testify v1.8.4 + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum index b380ae4..fa4b6e6 100644 --- a/go.sum +++ b/go.sum @@ -1,10 +1,10 @@ -github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= -github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/request.go b/request.go index 619308e..172ad26 100644 --- a/request.go +++ b/request.go @@ -84,7 +84,7 @@ func (r *Req) SetCookie(c *http.Cookie) *Req { return r } -//SetTransport sets transport configuration of request +// SetTransport sets transport configuration of request func (r *Req) SetTransport(transport *http.Transport) *Req { r.client.Transport = transport return r @@ -101,7 +101,7 @@ func (r *Req) SetBody(data []byte) *Req { return r } -//SetBodyXML sets content type as XML. +// SetBodyXML sets content type as XML. func (r *Req) SetBodyXML() *Req { r.SetContentType("application/xml; charset=UTF-8") return r @@ -164,7 +164,7 @@ func (r *Req) SetParam(param, value string) *Req { r.Params = &url.Values{} } r.Params.Set(param, value) - r.address = r.address +"//"+ r.Params.Encode() + r.address = r.address + "//" + r.Params.Encode() return r } @@ -176,7 +176,7 @@ func (r *Req) SetParams(queryParams map[string]string) *Req { for key, value := range queryParams { r.SetParam(key, value) } - r.address = r.address +"//"+ r.Params.Encode() + r.address = r.address + "//" + r.Params.Encode() return r } diff --git a/request_test.go b/request_test.go index c86ca4d..f9fc1a6 100644 --- a/request_test.go +++ b/request_test.go @@ -16,6 +16,7 @@ import ( ) var responseData = `{"success": true,"data": "done!"}` +var cookieData http.Cookie = http.Cookie{Name: "foo", Value: "bar"} var token = `123456` type Data struct { @@ -338,5 +339,4 @@ func TestCancelCtx(t *testing.T) { r := New(ctx, "") require.Equal(t, context.Canceled, r.request.Context().Err()) - } diff --git a/response.go b/response.go index 72b46d5..aa795e2 100644 --- a/response.go +++ b/response.go @@ -39,6 +39,10 @@ func (r *Response) Headers() http.Header { return r.resp.Header } +func (r *Response) Cookies() []*http.Cookie { + return r.resp.Cookies() +} + // Body returns the response body func (r *Response) Body() ([]byte, error) { body, err := r.readBody() diff --git a/response_test.go b/response_test.go index df5bcd2..bcd308d 100644 --- a/response_test.go +++ b/response_test.go @@ -23,15 +23,18 @@ func TestResponse(t *testing.T) { rw.Header().Set("Test-Header", "this is response") rw.Header().Set("Content-Type", "application/json") + http.SetCookie(rw, &cookieData) + _, err := rw.Write([]byte(responseData)) require.NoError(t, err) }), ) defer server.Close() - url := fmt.Sprintf("%s/post", server.URL) + req := New(context.Background(), server.URL) + req.client.Transport.(*http.Transport).Proxy = nil - resp, err := New(context.Background(), url).Get() + resp, err := req.Get() require.NoError(t, err) // Test Response() @@ -47,6 +50,12 @@ func TestResponse(t *testing.T) { require.True(t, ok) require.Equal(t, "this is response", headerValue[0]) + // Test Cookies() + cookies := resp.Cookies() + require.Equal(t, len(cookies), 1) + require.Equal(t, cookies[0].Name, cookieData.Name) + require.Equal(t, cookies[0].Value, cookieData.Value) + // Test Body() result, err := resp.Body() require.NoError(t, err) @@ -122,9 +131,7 @@ func TestSaveFile(t *testing.T) { ) defer server.Close() - url := fmt.Sprintf("%s/get", server.URL) - - resp, err := New(context.Background(), url).Get() + resp, err := New(context.Background(), server.URL).Get() require.NoError(t, err) dstFile, err := ioutil.TempFile("", "dest-file-*.png")