Requester: Add field variable to request item struct (#536)

* 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
This commit is contained in:
Ryan O'Hara-Reid
2020-08-12 10:24:55 +10:00
committed by GitHub
parent 3107f70647
commit 049f18e27c
3 changed files with 40 additions and 5 deletions

View File

@@ -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
@@ -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",

View File

@@ -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",
@@ -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)
@@ -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"`

View File

@@ -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.