Path parameters being passed as a String with comma not read properly in rest assured - rest-assured

I have a code where I am passing path parameters in the GET request in Rest Assured. But I see the path parameters aren't read properly and I see some gibberish text being read. Actually the String I am passing as path parameter contains a comma in it. Below is my code.
ValidatableResponse response = given().header("Authorization", token).header("Content-type", "application/json")
.when().log().all().pathParam("CalendarId", testCaseBean.getCalendarId().toString())
.queryParam("from", testCaseBean.getStartDate()).queryParam("to", testCaseBean.getEndDate())
.queryParam("monthEnd", testCaseBean.getMonthEndBusinessDay())
.get(EndPoint.GET_CALENDAR_BUSINESS_DAY_INFO_DATE_PARAM).then().log().all();
The path param I am passing is "AUS,EUR" and it is being read as AUS%2CEUR. I am passing this path parameter as test data from the CSV file. Below is the request being formed on the console.
https://portculation-qa.us-east-1.m5435454345.easn.mss.com/master-data/v1/calendars/AUS%2CEUR?from=2022-11-01&to=2022-11-01&monthEnd=false
My expected request URI is https://portculation-qa.us-east-1.m5435454345.easn.mss.com/master-data/v1/calendars/AUS,EUR?from=2022-11-01&to=2022-11-01&monthEnd=false
You can see the only difference in the expected and actual URI is the gibberish path param which isn't read properly. Any solution to tackle this issue?

Try adding this:
.urlEncodingEnabled(false)
RestAssured.given()
.contentType(JSON)
.log()
.all()
.urlEncodingEnabled(false)
or:
RestAssured.urlEncodingEnabled = false;
By default it set to true.

Related

How to give forward slash in postman url as a parameter

This the controller calls having path variable.
#RequestHapping(value="/circuit/getCircuitDetails/{circuitid:.+}", method =RequestMethod.GET, produces "application/json")
public ResponseEntity<ResponsePayLoad> getCircuitDetails (HttpServletRequest request, **#PathVariable (value = "circuitId" String circuitid**) throws Exception {
If I give path variable CircuitId as N1036596/N1036597 in postman url -> that is string containing forward slash
http://localhost:77/enggnar/circuit/getCircuitDetails/N1036596/N1036597
It is giving me error in postman.
Even by giving %2F and %5c in place of slash is also not working for me.
Please help me how to give the pathvariable containing forward slash.

How to test POST method with Postman and x-www-form-urlencoded Body?

I'm at the final step of making my first JS Web App using Pug, where I need to test that it supports POST method, which takes in a string should return a JSON object with 2 key.value pairs that represent original string and its length.
I'm instructed to use Postman and x-www-form-urlencoded to test my App, but I don't know how to test the POST method using urlencoded body.
Can someone tell me what I should fill in for KEY and VALUE on Postman, under x-www-form-urlencoded?
//POST method in route file
router.post('/', (req, res, next) => {
res.render('ps3post', {string: req.body.string, stringLength: req.body.string.length});
});
block content
h1 POST method, page rendered by PUG
p= string
p= stringLength
Okay I figured it out.. It seems that KEY needs to be type (string in my example), and VALUE needs to be what I want the actually value to be.

Is there a way to avoid amp; prepending to request parameter name in GrailsParameterMap

My request URL is
http://localhost:8080/application/controller/action?param1=value1&param2=value2
I obtain request params in controller through grails-core/WebAttributes.groovy
/**
* Obtains the Grails parameter map
*
* #return The GrailsParameterMap instance
*/
GrailsParameterMap getParams() {
currentRequestAttributes().getParams()
}
The 2nd param in the map is of structure
{LinkedHashMap$Entry#00000} "amp;param2" -> "value2"
How or is it possible to eliminate "amp;"?
Your request URL (https://en.wikipedia.org/wiki/Query_string) includes parameters named amp;param2 and similar; parameters are separated by ampersands, not HTML-encoded ampersands. This is not a problem with Grails, but instead with however you are generating your URL.
How are you generating the request URL that you reference here?

Swapping Rails 4 ParamsParser removes params body

I'm trying to follow this solution to add a params parser to my rails app, but all that happens is that I now get the headers but no parameters from the body of the JSON request at all. In other words, calling params from within the controller returns this:
{"controller"=>"residences", "action"=>"create",
"user_email"=>"wjdhamilton#wibble.com",
"user_token"=>"ayAJ8kDUKjCiy1r1Mxzp"}
but I expect this as well:
{"data"=>{"type"=>"residences",
"attributes"=>{"name-number"=>"The Byre",
"street"=>"Next Door",
"town"=>"Just Dulnain Bridge",
"postcode"=>"PH1 3SY",
"country-code"=>""},
"relationships"=>{"residence-histories"=>{"data"=>nil},
"occupants"=>{"data"=>nil}}}}
Here is my initializer, which as you can see is almost identical to the one in the other post:
Rails.application.config.middleware.swap(
::ActionDispatch::ParamsParser, ::ActionDispatch::ParamsParser,
::Mime::Type.lookup("application/vnd.api+json") => Proc.new { |raw_post|
# Borrowed from action_dispatch/middleware/params_parser.rb except for
# data.deep_transform_keys!(&:underscore) :
data = ::ActiveSupport::JSON.decode(raw_post)
data = {:_json => data} unless data.is_a?(::Hash)
data = ::ActionDispatch::Request::Utils.deep_munge(data)
# Transform dash-case param keys to snake_case:
data = data.deep_transform_keys(&:underscore)
data.with_indifferent_access
}
)
Can anyone tell me where I'm going wrong? I'm running Rails 4.2.7.1
Update 1: I decided to try and use the Rails 5 solution instead, the upgrade was overdue anyway, and now things have changed slightly. Given the following request:
"user_email=mogwai%40balnaan.com
&user_token=_1o3Kpzo4gTdPC2bivy
&format=json
&data[type]=messages&data[attributes][sent-on]=2014-01-15
&data[attributes][details]=Beautiful+Shetland+Pony
&data[attributes][message-type]=card
&data[relationships][occasion][data][type]=occasions
&data[relationships][occasion][data][id]=5743
&data[relationships][person][data][type]=people
&data[relationships][person][data][id]=66475"
the ParamsParser middleware only receives the following hash:
"{user":{"email":"mogwai#balnaan.com","password":"0h!Mr5M0g5"}}
Whereas I would expect it to receive the following:
{"user_email"=>"mogwai#balnaan.com", "user_token"=>"_1o3Kpzo4gTdPC2b-ivy", "format"=>"5743", "data"=>{"type"=>"messages", "attributes"=>{"sent-on"=>"2014-01-15", "details"=>"Beautiful Shetland Pony", "message-type"=>"card"}, "relationships"=>{"occasion"=>{"data"=> "type"=>"occasions", "id"=>"5743"}}, "person"=>{"data"=>{"type"=>"people", "id"=>"66475"}}}}, "controller"=>"messages", "action"=>"create"}
The problem was caused by the tests that I had written. I had not added the Content-Type to the requests in the tests, and had not explicitly converted the payload to JSON like so (in Rails 5):
post thing_path, params: my_data.to_json, headers: { "Content-Type" => "application/vnd.api+json }
The effects of this were twofold: Firstly, since params parsers are mapped to specific media types then withholding the media type meant that rails assumed its default media type (in this case application/json) so the parser was not used to process the body of the request. What confused me was that it still passed the headers to the parser. Once I fixed that problem, I was then faced with the body in the format of the request above. That is where the explicit conversion to JSON is required. I could have avoided all of this if I had just written accurate tests!

HttpRequest GET with sendData as query parameters?

This question is kind of a duplicate of HTTPRequest.request with sendData, can't seem to get this to work, but I've got some more information now. The goal here is to send a GET request with query parameters attached. I initially tried to send my request as such:
HttpRequest request = new HttpRequest();
request.open("GET", _url, async:true);
request.onError.listen(_onLoadError, onError: _onLoadError);
request.send(sendData);
Where sendData is a String following the normal format for query parameters (?myVariable=2&myOtherVariable=a, etc), since this is the ultimate goal here. The request gets sent, but I never see any additional data (sendData) go with it in any monitoring tools (I'm using Charles). I then tried:
HttpRequest request = new HttpRequest();
request.open("GET", _url + sendData, async:true);
request.onError.listen(_onLoadError, onError: _onLoadError);
request.send();
So now I'm just attaching the query string to the url itself. This works as intended, but is far from elegant. Is there a better solution?
On a GET request you always add the query string to the URL?
When you create the Uri you can pass in a map with the query parameters if you find this more elegant.
Map query = {'xx':'yy', 'zz' : 'ss'};
String url = "http://localhost:8080/myapp/signinService";
Uri uri = new Uri(path: url, queryParameters : query);
HttpRequest.getString(uri.toString().then((HttpRequest req) ...
According to the W3 XMLHttpRequest Specification:
(In the send() method) The argument is ignored if request method is GET or HEAD.
So simple answer to this question is no. sendData cannot be appended to a GET request, this is by XMLHttpRequest specification and not a limitation of Dart.
That said, for requests like this it may be more readable and idiomatic to use HttpRequest.getString
HttpRequest.getString(_url + sendData).then((HttpRequest req) {
// ... Code here
}).catchError(_onLoadError);
If you want to generate a valid URL from url (String) + query parameters (Map) , you can do the following :
Uri uri = Uri.parse(url).replace(queryParameters: parameters);
String finalURL = uri.toString();
url is a String and parameters is a Map

Resources