Rest Assured: What is the best way to test a raw response body? - rest-assured

I have a get request which simply returns a simple string:
given()
.when()
.get("https://www.example.com")
This get returns a simple string, for example "123".
Is there a cooler way to verify it other than using for example:
Assert.assertEquals("123",
given()
.when()
.get("https://www.example.com").asString());
Something like:
given()
.when()
.get("https://www.example.com")
.then()
.statusCode(200);
Thank you

You can use method T body(Matcher<?> matcher, Matcher<?>... additionalMatchers);
Example:
given()
.get("https://www.example.com")
.then()
.statusCode(200)
.body(is("text_in_body"));

Related

How to compare two JSON fields in RestAssured and Hamcrest?

I'm using RestAssured and Hamcrest to functional test our Back-end API and I'd like to know if there's any way to compare two distinct JSON fields inside the body method, or any equivalent.
For example, given the JSON response below:
[
{ name: "Foo", age: 25 },
{ name: "Bar", age: 30 }
]
And given the code below, with a little excerpt of an invalid source code line that exposes what I'm trying to achieve:
given()
.when()
.get("/my-endpoint")
.then()
.body("[0].age", lessThan("[1].age")); // Invalid code just to show what I need to do
How can I achieve the goal exposed above in a clean way?
I think I have found the best way of doing it. It's quite simple actually.
I just need to:
given()
.when()
.get("/my-endpoint")
.then()
.body("[0].age", resp -> lessThan(resp.path("[1].age"));

No Response when Restassured url has custom encoding

I am using rest-assured to test REST APIs, when hitting the url, url has it own encoded parameters.
Response b = given().
relaxedHTTPSValidation().body(gbody).
with().
contentType(ConfigReader.get("application.json")).
then()
.post(url);
It ran successfully but response is empty.
Please tell what can be the reason
This is because, rest-assured will automatically encode the url when you are encoding the url, you need to use method urlEncodingEnabled(false), so that , it will not encode the url again.
Now your code will become
Response res = given()
.urlEncodingEnabled(false)
.relaxedHTTPSValidation()
.body(gbody)
.with()
.contentType(ConfigReader.get("application.json"))
.then()
.post(url);

Can some one help me with working code for Restassured post request with Authorization Header

I'm new to Restassured Webservice automation.
I'm able to get OAuth2 token and save it to a string. But when I pass this string in Authorization header, I'm getting 403 error.
Working code to store token in a string:
`String response = given()
.params("grant_type", "XXX", "scope", "XXX")
.auth()
.preemptive()
.basic("XXX","XXX")
.when()
.post("api/path")
.asString();
JsonPath jsonPath = new JsonPath(response);
accessToken = jsonPath.getString("access_token");
String pswd = "Bearer " + accessToken; `
Code not working is below:
`given().header("Authorization", pswd)
.body(content).with().contentType("application/json")
.when()
.post("/api/path")
.then().statusCode(200);`
I'm getting 403 error.
Can you try the following
`given().auth().oauth2(accessToken).
.body(content).with().contentType("application/json")
.when()
.post("/api/path")
.then().statusCode(200)`

how deserialize a json in rails

I send a http request and get a json for response like below:
{"total":"1200.0","used":"35.0","available":1165.0}
now I want deserialize this json and show to user in a table like below:
total : 1200
used : 35
availabele : 1165
I use JSON.parse(response),but I get below error:
no implicit conversion of Net::HTTPOK into String
How can I do this?
Net::HTTP has responses in body. so, Try
JSON.parse response.body
You have to pass the response.body as the parameters into the JSON.parse method then assign the method to a variable to use elsewhere in your code.
parse_res = JSON.parse(res.body)

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