Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -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 ./...
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
![Workflow](https://github.com/binalyze/httpreq/actions/workflows/go.yml/badge.svg)

# httpreq

Expand Down Expand Up @@ -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()
```
10 changes: 8 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
)
14 changes: 7 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
8 changes: 4 additions & 4 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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

}
Expand Down
2 changes: 1 addition & 1 deletion request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -338,5 +339,4 @@ func TestCancelCtx(t *testing.T) {

r := New(ctx, "")
require.Equal(t, context.Canceled, r.request.Context().Err())

}
4 changes: 4 additions & 0 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
17 changes: 12 additions & 5 deletions response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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")
Expand Down