Skip to content

Commit 65d463c

Browse files
committed
add response code to the Request()
1 parent 0da7b1e commit 65d463c

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

request.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,26 @@ import (
1111
)
1212

1313
// Request - for http requests. Expect method, url and body. Will inject bearer token and tracing id to request.
14-
func Request(ctx context.Context, method string, url string, b []byte, auth bool) ([]byte, error) {
14+
func Request(ctx context.Context, method string, url string, b []byte, auth bool) ([]byte, int, error) {
1515
req, err := http.NewRequest(method, url, bytes.NewBuffer(b))
1616
if err != nil {
17-
return nil, err
17+
return nil, 0, err
1818
}
1919

2020
req.Header.Add("Content-Type", "application/json")
2121
if auth {
2222
if err := SetAuthorization(ctx, req); err != nil {
23-
return nil, err
23+
return nil, 0, err
2424
}
2525
}
2626
if err := InjectToReq(ctx, req); err != nil {
27-
return nil, err
27+
return nil, 0, err
2828
}
2929

3030
client := http.Client{}
3131
resp, err := client.Do(req)
3232
if err != nil {
33-
return nil, err
33+
return nil, 0, err
3434
}
3535

3636
defer func() {
@@ -39,15 +39,15 @@ func Request(ctx context.Context, method string, url string, b []byte, auth bool
3939

4040
body, err := ioutil.ReadAll(resp.Body)
4141
if err != nil {
42-
return nil, err
42+
return nil, 0, err
4343
}
4444

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

50-
return body, nil
50+
return body, resp.StatusCode, nil
5151
}
5252

5353
// SetAuthorization - take token from context value and set as Bearer token in Authorization header.

request_test.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,52 +23,58 @@ func TestRequest(t *testing.T) {
2323
defer ts.Close()
2424

2525
t.Run("invalid method", func(t *testing.T) {
26-
resp, err := Request(context.Background(), " ", "", []byte{}, false)
26+
resp, code, err := Request(context.Background(), " ", "", []byte{}, false)
2727
require.Error(t, err)
2828
require.Nil(t, resp)
29+
require.Equal(t, 0, code)
2930
})
3031

3132
t.Run("no token", func(t *testing.T) {
32-
resp, err := Request(context.Background(), "", "", []byte{}, true)
33+
resp, code, err := Request(context.Background(), "", "", []byte{}, true)
3334
require.Error(t, err)
3435
require.Nil(t, resp)
36+
require.Equal(t, 0, code)
3537
})
3638

3739
t.Run("no span", func(t *testing.T) {
3840
ctx := context.WithValue(context.Background(), "token", "test")
39-
resp, err := Request(ctx, "", "", []byte{}, true)
41+
resp, code, err := Request(ctx, "", "", []byte{}, true)
4042
require.Error(t, err)
4143
require.Nil(t, resp)
44+
require.Equal(t, 0, code)
4245
})
4346

4447
t.Run("wrong url", func(t *testing.T) {
4548
ctx := context.WithValue(context.Background(), "token", "test")
4649
span, ctx := opentracing.StartSpanFromContext(ctx, "test")
4750
require.NotNil(t, span)
4851

49-
resp, err := Request(ctx, "", "", []byte{}, true)
52+
resp, code, err := Request(ctx, "", "", []byte{}, true)
5053
require.Error(t, err)
5154
require.Nil(t, resp)
55+
require.Equal(t, 0, code)
5256
})
5357

5458
t.Run("non 1xx, 2xx, 3xx response", func(t *testing.T) {
5559
ctx := context.WithValue(context.Background(), "token", "test")
5660
span, ctx := opentracing.StartSpanFromContext(ctx, "test")
5761
require.NotNil(t, span)
5862

59-
resp, err := Request(ctx, "GET", ts.URL, []byte{}, true)
63+
resp, code, err := Request(ctx, "GET", ts.URL, []byte{}, true)
6064
require.Error(t, err)
6165
require.Nil(t, resp)
66+
require.Equal(t, http.StatusBadRequest, code)
6267
})
6368

6469
t.Run("200", func(t *testing.T) {
6570
ctx := context.WithValue(context.Background(), "token", "test")
6671
span, ctx := opentracing.StartSpanFromContext(ctx, "test")
6772
require.NotNil(t, span)
6873

69-
resp, err := Request(ctx, "POST", ts.URL, []byte{}, true)
74+
resp, code, err := Request(ctx, "POST", ts.URL, []byte{}, true)
7075
require.NoError(t, err)
7176
require.Equal(t, []byte("OK"), resp)
77+
require.Equal(t, http.StatusOK, code)
7278
})
7379
}
7480

0 commit comments

Comments
 (0)