Skip to content

Commit

Permalink
Async response text
Browse files Browse the repository at this point in the history
  • Loading branch information
inancgumus committed Jun 4, 2024
1 parent 5304b0f commit 9e4ffc0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
1 change: 1 addition & 0 deletions browser/mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ type responseAPI interface {
Status() int64
StatusText() string
URL() string
Text() (string, error)
}

// locatorAPI represents a way to find element(s) on a page at any moment.
Expand Down
7 changes: 6 additions & 1 deletion browser/response_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

// mapResponse to the JS module.
func mapResponse(vu moduleVU, r *common.Response) mapping {
func mapResponse(vu moduleVU, r *common.Response) mapping { //nolint:funlen
if r == nil {
return nil
}
Expand Down Expand Up @@ -81,6 +81,11 @@ func mapResponse(vu moduleVU, r *common.Response) mapping {
"status": r.Status,
"statusText": r.StatusText,
"url": r.URL,
"text": func() *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
return r.Text() //nolint:wrapcheck
})
},
}

return maps
Expand Down
8 changes: 5 additions & 3 deletions common/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,13 +568,15 @@ func (r *Response) StatusText() string {
}

// Text returns the response body as a string.
func (r *Response) Text() string {
func (r *Response) Text() (string, error) {
if err := r.fetchBody(); err != nil {
k6ext.Panic(r.ctx, "getting response body as text: %w", err)
return "", fmt.Errorf("getting response body as text: %w", err)
}

r.bodyMu.RLock()
defer r.bodyMu.RUnlock()
return string(r.body)

return string(r.body), nil
}

// URL returns the request URL.
Expand Down

0 comments on commit 9e4ffc0

Please sign in to comment.