Skip to content
This repository has been archived by the owner on Sep 18, 2019. It is now read-only.

Inject Requests With Query Parameter Object? #334

Closed
DavidTPate opened this issue Aug 18, 2016 · 7 comments
Closed

Inject Requests With Query Parameter Object? #334

DavidTPate opened this issue Aug 18, 2016 · 7 comments

Comments

@DavidTPate
Copy link

DavidTPate commented Aug 18, 2016

For testing we want to go through and be able to use an object for our query parameters when executing server.inject(...) but I'm not seeing a method to be able to do just that. I realize that we would be able to accomplish this through a method such as the following:

server.inject(`/some/path?${querystring.stringify(someObject)}`, (res) => {

    console.log(res.result);
});

But it would be preferable to avoid that boilerplate and just be able to send a straight object. Something like the following:

server.inject({
  method: 'GET',
  url: '/some/path',
  query: {
    sort: 'ASC'
  }
}, (res) => {

    console.log(res.result);
});
@mtharrison
Copy link

Could be a nice feature. However, as hapi only supports the standard node querystring module out of the box, we'd have to do the internal stringifying with that. It could lead to some confusing behaviours for people thinking they could pass complex nested objects in. I suppose though we could validate that the object is a simple flat dictionary of key value-pairs.

@AdriVanHoudt
Copy link

@mtharrison if people are putting in complex objects they are probably already using qs in hapi. (you can set the querystring parser right?) They could set it in the tests as well and inject would use that one?

@mtharrison
Copy link

How do you mean by:

set it in the tests as well and inject would use that one

How would that look exactly?

@AdriVanHoudt
Copy link

AdriVanHoudt commented Aug 18, 2016

@mtharrison Ok I thought you could pass qs to hapi to let it use that but I must have read it wrong, this is how you should do it aperently hapijs/hapi#2985

In that case using the default seems fine, if they use the fix from the issue referenced above it will work right?

@mtharrison
Copy link

Yes, but that's all about how to parse the qs-encoded string from a URL into an object so it's available in request.query. This is the opposite problem. @DavidTPate is starting with an object in a test and wants server.inject() to stringify that for him and add it to the request URL.

We don't want to use qs internally because we've already dropped that - so the only alternative I see is to use Node's built-in querystring module and limit to simple objects. If someone wants to use the qs format, it's then up to them to build the URL for themselves instead.

@AdriVanHoudt
Copy link

@mtharrison derp you're right  🙃

@DavidTPate
Copy link
Author

I agree with your points @mtharrison

I took a look through how query strings are handled in relation to shot and hapi and that definitely appears to be the only option that shot would set the url.search parameter prior to executing url.format(...) which would then pass of the rest of the handling to Node's url module.

Personally, I'm fine with using simple objects for the query string for my particular use case.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants