You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
How do I add different responses based on request GET parameters? #285
So is there a way to make different responses for different parameters?
Im using Swifter to mock server responses and it would be great to response differently based on request’ GET parameters, but I can’t really figure how to add such responses
@AndrewSad were you able to add 2 of the same urls that return different reponses based on the request parameters? I tried the above, and the second stub I setup overrides the previous one, and results in return HttpResponse.notFound when executing the first stub.
letsomeValues=[DynamicStubParameter(name:"<Field here>", value:"<Value>"):"<filename first JSON>",DynamicStubParameter(name:"<Same field>", value:"<Another value>"):"<filename second JSON>",]self.dynamicStubs.mapStub(url:"<your URL here>", to: someValues, method:.GET)
Notice: someValues is the name for our array of DynamicStubParameter values.
Thanks so much for sharing @AndrewSad !
This works for a request's body parameters as well, by replacing request.queryParams with String(bytes: request.body, encoding: .utf8)!, and handling if that String contains your required parameters.
Activity
basmaessen commentedon Aug 20, 2018
@AndrewSad Did you find any solution for this problem?
AndrewSad commentedon Aug 20, 2018
@basmaessen yes, turned out it was quite easy
basmaessen commentedon Aug 20, 2018
@AndrewSad How did you do it?
AndrewSad commentedon Aug 21, 2018
I've added this method to HTTPDynamicStubs class:
Usage:
andynovak12 commentedon Feb 25, 2019
@AndrewSad were you able to add 2 of the same urls that return different reponses based on the request parameters? I tried the above, and the second stub I setup overrides the previous one, and results in
return HttpResponse.notFoundwhen executing the first stub.AndrewSad commentedon Feb 25, 2019
@andynovak12, actually I rewrote that piece since the time of my previous answer, will post solution in a bit
andynovak12 commentedon Feb 25, 2019
Thank you @AndrewSad ! I am looking forward to seeing it.
AndrewSad commentedon Feb 26, 2019
So, method now goes like this:
New 'magic' inside this function:
New typealias:
And usage:
Notice:
someValuesis the name for our array ofDynamicStubParametervalues.andynovak12 commentedon Feb 27, 2019
Thanks so much for sharing @AndrewSad !
This works for a request's body parameters as well, by replacing
request.queryParamswithString(bytes: request.body, encoding: .utf8)!, and handling if that String contains your required parameters.AndrewSad commentedon Feb 27, 2019
You are welcome ;) Got through a lot of pain back in the day to solve this.