Skip to content

Commit

Permalink
update Request params (moved to struct)
Browse files Browse the repository at this point in the history
  • Loading branch information
exelban committed Mar 4, 2021
1 parent 65d463c commit c216d38
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
21 changes: 13 additions & 8 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,26 @@ import (
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
)

type RequestOpts struct {
Client http.Client
Method string
URL string
Data []byte
InjectBearer bool
}

// Request - for http requests. Expect method, url and body. Will inject bearer token and tracing id to request.
func Request(ctx context.Context, method string, url string, b []byte, auth bool) ([]byte, int, error) {
req, err := http.NewRequest(method, url, bytes.NewBuffer(b))
func Request(ctx context.Context, params RequestOpts) ([]byte, int, error) {
req, err := http.NewRequest(params.Method, params.URL, bytes.NewBuffer(params.Data))
if err != nil {
return nil, 0, err
}

req.Header.Add("Content-Type", "application/json")
if auth {
if params.InjectBearer {
if err := SetAuthorization(ctx, req); err != nil {
return nil, 0, err
}
Expand All @@ -27,8 +34,7 @@ func Request(ctx context.Context, method string, url string, b []byte, auth bool
return nil, 0, err
}

client := http.Client{}
resp, err := client.Do(req)
resp, err := params.Client.Do(req)
if err != nil {
return nil, 0, err
}
Expand All @@ -43,7 +49,6 @@ func Request(ctx context.Context, method string, url string, b []byte, auth bool
}

if resp.StatusCode >= 400 {
log.Printf("[DEBUG] %s %s: %d (%s) %s", method, url, resp.StatusCode, http.StatusText(resp.StatusCode), string(body))
return nil, resp.StatusCode, errors.New(string(body))
}

Expand All @@ -64,4 +69,4 @@ func SetAuthorization(ctx context.Context, req *http.Request) error {
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", jwtToken))

return nil
}
}
28 changes: 21 additions & 7 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,26 @@ func TestRequest(t *testing.T) {
defer ts.Close()

t.Run("invalid method", func(t *testing.T) {
resp, code, err := Request(context.Background(), " ", "", []byte{}, false)
resp, code, err := Request(context.Background(), RequestOpts{})
require.Error(t, err)
require.Nil(t, resp)
require.Equal(t, 0, code)
})

t.Run("no token", func(t *testing.T) {
resp, code, err := Request(context.Background(), "", "", []byte{}, true)
resp, code, err := Request(context.Background(), RequestOpts{
InjectBearer: true,
})
require.Error(t, err)
require.Nil(t, resp)
require.Equal(t, 0, code)
})

t.Run("no span", func(t *testing.T) {
ctx := context.WithValue(context.Background(), "token", "test")
resp, code, err := Request(ctx, "", "", []byte{}, true)
resp, code, err := Request(ctx, RequestOpts{
InjectBearer: true,
})
require.Error(t, err)
require.Nil(t, resp)
require.Equal(t, 0, code)
Expand All @@ -49,7 +53,9 @@ func TestRequest(t *testing.T) {
span, ctx := opentracing.StartSpanFromContext(ctx, "test")
require.NotNil(t, span)

resp, code, err := Request(ctx, "", "", []byte{}, true)
resp, code, err := Request(ctx, RequestOpts{
InjectBearer: true,
})
require.Error(t, err)
require.Nil(t, resp)
require.Equal(t, 0, code)
Expand All @@ -60,7 +66,11 @@ func TestRequest(t *testing.T) {
span, ctx := opentracing.StartSpanFromContext(ctx, "test")
require.NotNil(t, span)

resp, code, err := Request(ctx, "GET", ts.URL, []byte{}, true)
resp, code, err := Request(ctx, RequestOpts{
Method: "GET",
URL: ts.URL,
InjectBearer: true,
})
require.Error(t, err)
require.Nil(t, resp)
require.Equal(t, http.StatusBadRequest, code)
Expand All @@ -71,7 +81,11 @@ func TestRequest(t *testing.T) {
span, ctx := opentracing.StartSpanFromContext(ctx, "test")
require.NotNil(t, span)

resp, code, err := Request(ctx, "POST", ts.URL, []byte{}, true)
resp, code, err := Request(ctx, RequestOpts{
Method: "POST",
URL: ts.URL,
InjectBearer: true,
})
require.NoError(t, err)
require.Equal(t, []byte("OK"), resp)
require.Equal(t, http.StatusOK, code)
Expand All @@ -94,4 +108,4 @@ func TestSetAuthorization(t *testing.T) {
req := httptest.NewRequest("GET", "http://test.com", nil)
require.NoError(t, SetAuthorization(ctx, req))
})
}
}

0 comments on commit c216d38

Please sign in to comment.