Skip to content

Commit

Permalink
client: support reading raw HTTP response (#227)
Browse files Browse the repository at this point in the history
* client: support reading raw HTTP response

* Fix lint
  • Loading branch information
carlcarl authored Jul 26, 2023
1 parent 776b80f commit 7365fe5
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 6 deletions.
4 changes: 4 additions & 0 deletions client/client_execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func (r *Request) Execute(ctx context.Context, req *Req) ([]byte, error) {
return nil, err
}

if req.rawResponseContainer != nil && res != nil {
*req.rawResponseContainer = *res
}

err = r.HttpErrorHandler(res, request.URL.String())
if err != nil {
return nil, err
Expand Down
19 changes: 13 additions & 6 deletions client/request.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
"net/http"
"net/url"
)

Expand All @@ -9,12 +10,13 @@ import (
//
// To build this struct, use NewReqBuilder.
type Req struct {
headers map[string]string
resultContainer any
method string
path Path
query url.Values
body any
headers map[string]string
resultContainer any
method string
path Path
query url.Values
body any
rawResponseContainer *http.Response

metricName string
pathMetricEnabled bool
Expand Down Expand Up @@ -46,6 +48,11 @@ func (builder *ReqBuilder) WriteTo(resultContainer any) *ReqBuilder {
return builder
}

func (builder *ReqBuilder) WriteRawResponseTo(resp *http.Response) *ReqBuilder {
builder.req.rawResponseContainer = resp
return builder
}

func (builder *ReqBuilder) Method(method string) *ReqBuilder {
builder.req.method = method
return builder
Expand Down
74 changes: 74 additions & 0 deletions client/request_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package client

import (
"context"
"io"
"net/http"
"strings"
"testing"

"github.com/stretchr/testify/require"
)

func TestRequest_WriteRawResponseTo(t *testing.T) {
const (
pathOk = "/ok"
path5xx = "/5xx"
)

tests := []struct {
name string
path string
statusCode int
headers http.Header
}{
{
name: "Test write raw response with statusOK",
path: pathOk,
statusCode: http.StatusOK,
headers: http.Header{
"Content-Type": []string{"application/json"},
"x-aptos-block-height": []string{"73287085"},
},
},
{
name: "Test write raw response with status5xx",
path: path5xx,
statusCode: http.StatusInternalServerError,
headers: http.Header{
"Content-Type": []string{"application/json"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client := InitClient("http://www.example.com", nil,
WithHttpClient(&http.Client{
Transport: RoundTripperFunc(func(request *http.Request) (*http.Response, error) {
switch request.URL.Path {
case pathOk:
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader(`{"Data": "ok"}`)),
Header: tt.headers,
}, nil
case path5xx:
return &http.Response{
StatusCode: http.StatusInternalServerError,
Request: request,
Body: io.NopCloser(strings.NewReader(`{"Data": "5xx"}`)),
Header: tt.headers,
}, nil
default:
return nil, nil
}
}),
}),
)
var resp http.Response
_, _ = client.Execute(context.Background(), NewReqBuilder().Method(http.MethodGet).PathStatic(tt.path).WriteRawResponseTo(&resp).Build())
require.Equal(t, tt.headers, resp.Header)
require.Equal(t, tt.statusCode, resp.StatusCode)
})
}
}

0 comments on commit 7365fe5

Please sign in to comment.