MCPcopy
hub / github.com/grafana/k6 / parseRequest

Method parseRequest

js/modules/k6/http/request.go:143–404  ·  view source on GitHub ↗

TODO: break this function up nolint:gocyclo, cyclop, funlen, gocognit

(
	method string, reqURL, body any, params sobek.Value,
)

Source from the content-addressed store, hash-verified

141//
142//nolint:gocyclo, cyclop, funlen, gocognit
143func (c *Client) parseRequest(
144 method string, reqURL, body any, params sobek.Value,
145) (*httpext.ParsedHTTPRequest, error) {
146 rt := c.moduleInstance.vu.Runtime()
147 state := c.moduleInstance.vu.State()
148 if state == nil {
149 return nil, ErrHTTPForbiddenInInitContext
150 }
151
152 if urlJSValue, ok := reqURL.(sobek.Value); ok {
153 reqURL = urlJSValue.Export()
154 }
155 u, err := httpext.ToURL(reqURL)
156 if err != nil {
157 return nil, err
158 }
159
160 result := &httpext.ParsedHTTPRequest{
161 URL: &u,
162 Req: &http.Request{
163 Method: method,
164 URL: u.GetURL(),
165 Header: make(http.Header),
166 },
167 Timeout: 60 * time.Second,
168 Throw: state.Options.Throw.Bool,
169 Redirects: state.Options.MaxRedirects,
170 Cookies: make(map[string]*httpext.HTTPRequestCookie),
171 ResponseCallback: c.responseCallback,
172 TagsAndMeta: c.moduleInstance.vu.State().Tags.GetCurrentValues(),
173 }
174
175 if state.Options.DiscardResponseBodies.Bool {
176 result.ResponseType = httpext.ResponseTypeNone
177 } else {
178 result.ResponseType = httpext.ResponseTypeText
179 }
180
181 formatFormVal := func(v any) string {
182 // TODO: handle/warn about unsupported/nested values
183 return fmt.Sprintf("%v", v)
184 }
185
186 handleObjectBody := func(data map[string]any) error {
187 if !requestContainsFile(data) {
188 bodyQuery := make(url.Values, len(data))
189 for k, v := range data {
190 if arr, ok := v.([]any); ok {
191 for _, el := range arr {
192 bodyQuery.Add(k, formatFormVal(el))
193 }
194 continue
195 }
196 bodyQuery.Set(k, formatFormVal(v))
197 }
198 result.Body = bytes.NewBufferString(bodyQuery.Encode())
199 result.Req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
200 return nil

Callers 3

RequestMethod · 0.95
asyncRequestMethod · 0.95
parseBatchRequestMethod · 0.95

Calls 15

ToURLFunction · 0.92
ToBytesFunction · 0.92
IsNullishFunction · 0.92
CompressionTypeStringFunction · 0.92
CompressionTypeValuesFunction · 0.92
ApplyCustomUserTagsFunction · 0.92
GetDurationValueFunction · 0.92
ResponseTypeStringFunction · 0.92
SetRequestCookiesFunction · 0.92
requestContainsFileFunction · 0.85
escapeQuotesFunction · 0.85
ExportMethod · 0.80

Tested by

no test coverage detected