MCPcopy Create free account
hub / github.com/gavv/httpexpect / WithQueryObject

Method WithQueryObject

request.go:1230–1323  ·  view source on GitHub ↗

WithQueryObject adds multiple query parameters to request URL. object is converted to query string using github.com/google/go-querystring if it's a struct or pointer to struct, or github.com/ajg/form otherwise. Various object types are supported. Structs may contain "url" struct tag, similar to "j

(object interface{})

Source from the content-addressed store, hash-verified

1228// req.WithQueryObject(map[string]interface{}{"a": 123, "b": "foo"})
1229// // URL is now http://example.com/path?a=123&b=foo
1230func (r *Request) WithQueryObject(object interface{}) *Request {
1231 opChain := r.chain.enter("WithQueryObject()")
1232 defer opChain.leave()
1233
1234 r.mu.Lock()
1235 defer r.mu.Unlock()
1236
1237 if opChain.failed() {
1238 return r
1239 }
1240
1241 if !r.checkOrder(opChain, "WithQueryObject()") {
1242 return r
1243 }
1244
1245 if object == nil {
1246 return r
1247 }
1248
1249 var (
1250 q url.Values
1251 err error
1252 )
1253
1254 encoder := r.queryEncoder
1255 if encoder == defaultQueryEncoder {
1256 encoder = selectQueryEncoder(object)
1257 }
1258
1259 switch encoder {
1260 case defaultQueryEncoder:
1261 // can't happen
1262
1263 case QueryEncoderGoogle:
1264 q, err = query.Values(object)
1265 if err != nil {
1266 opChain.fail(AssertionFailure{
1267 Type: AssertValid,
1268 Actual: &AssertionValue{object},
1269 Errors: []error{
1270 errors.New("invalid query object"),
1271 errors.New("google/go-querystring encoding failed"),
1272 err,
1273 },
1274 })
1275 return r
1276 }
1277
1278 case QueryEncoderForm, QueryEncoderFormKeepZeros:
1279 var b bytes.Buffer
1280 enc := form.NewEncoder(&b)
1281
1282 if encoder == QueryEncoderFormKeepZeros {
1283 enc.KeepZeros(true)
1284 }
1285
1286 err = enc.Encode(object)
1287 if err != nil {

Callers 4

TestRequest_FailedChainFunction · 0.80
TestRequest_URLQueryFunction · 0.80
TestRequest_OrderFunction · 0.80
TestOAuth2Function · 0.80

Calls 10

checkOrderMethod · 0.95
selectQueryEncoderFunction · 0.85
enterMethod · 0.80
leaveMethod · 0.80
LockMethod · 0.80
UnlockMethod · 0.80
failedMethod · 0.80
failMethod · 0.80
ValuesMethod · 0.45
StringMethod · 0.45

Tested by 4

TestRequest_FailedChainFunction · 0.64
TestRequest_URLQueryFunction · 0.64
TestRequest_OrderFunction · 0.64
TestOAuth2Function · 0.64