Skip to content

Commit

Permalink
Requester: Add field variable to request item struct (thrasher-corp#536)
Browse files Browse the repository at this point in the history
* Add in initial header pass back for package side inspection

* change to ptr to point to correct ptr value to ensure len and cap values; dereferences so we don't need to string join the value iteration coming from main header

* Add tests

* More tests added for edge case

* change field name

* remove unnecessary coversion

* changed return and comment
  • Loading branch information
shazbert authored Aug 12, 2020
1 parent 3107f70 commit 049f18e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
12 changes: 12 additions & 0 deletions exchanges/request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ func (i *Item) validateRequest(ctx context.Context, r *Requester) (*http.Request
return nil, errors.New("invalid path")
}

if i.HeaderResponse != nil {
if *i.HeaderResponse == nil {
return nil, errors.New("header response is nil")
}
}

req, err := http.NewRequestWithContext(ctx, i.Method, i.Path, i.Body)
if err != nil {
return nil, err
Expand Down Expand Up @@ -200,6 +206,12 @@ func (r *Requester) doRequest(req *http.Request, p *Item) error {
}
}

if p.HeaderResponse != nil {
for k, v := range resp.Header {
(*p.HeaderResponse)[k] = v
}
}

if resp.StatusCode < http.StatusOK ||
resp.StatusCode > http.StatusAccepted {
return fmt.Errorf("%s unsuccessful HTTP status code: %d raw response: %s",
Expand Down
28 changes: 24 additions & 4 deletions exchanges/request/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@ func TestCheckRequest(t *testing.T) {
t.Fatal(err)
}

var passback http.Header
check.HeaderResponse = &passback
_, err = check.validateRequest(ctx, r)
if err == nil {
t.Fatal("expected error when underlying memory is not allocated")
}
passback = http.Header{}

// Test setting headers
check.Headers = map[string]string{
"Content-Type": "Super awesome HTTP party experience",
Expand Down Expand Up @@ -278,11 +286,15 @@ func TestDoRequest(t *testing.T) {
var resp struct {
Response bool `json:"response"`
}

// Check header contents
var passback = http.Header{}
err = r.SendPayload(ctx, &Item{
Method: http.MethodGet,
Path: testURL,
Result: &resp,
Endpoint: UnAuth,
Method: http.MethodGet,
Path: testURL,
Result: &resp,
Endpoint: UnAuth,
HeaderResponse: &passback,
})
if err != nil {
t.Fatal(err)
Expand All @@ -291,6 +303,14 @@ func TestDoRequest(t *testing.T) {
t.Fatal(unexpected)
}

if passback.Get("Content-Length") != "17" {
t.Fatal("incorrect header value")
}

if passback.Get("Content-Type") != "application/json" {
t.Fatal("incorrect header value")
}

// Check error
var respErr struct {
Error bool `json:"error"`
Expand Down
5 changes: 4 additions & 1 deletion exchanges/request/request_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ type Item struct {
HTTPDebugging bool
HTTPRecording bool
IsReserved bool
Endpoint EndpointLimit
// HeaderResponse for inspection of header contents package side useful for
// pagination
HeaderResponse *http.Header
Endpoint EndpointLimit
}

// Backoff determines how long to wait between request attempts.
Expand Down

0 comments on commit 049f18e

Please sign in to comment.