Validate a Json response using PAW rest client - paw

Lets say i make a rest call to the server and it sends back this JSON response
{
"pageTotal": 1,
"total": 10,
}
Is there a way i can automatically validate in PAW that the values match or lets say the values are integers and not strings
Please let me know.

Related

How to structure a JMeter test plan properly for a sockets.io scenario - event "subscription"

Scenario Outline
Test sends a REST API request to activate a game.
Website receives a socket.io event and displays an alert on the browser.
Question
Since I don't know when the event will be sent, do I need to run a WebSocket Sampler, or perhaps a WebSocket Single-Read Sampler, in a loop, until I get the matching message?
So far in my attempts, I can connect to the event server and get message, but they are empty frames or messages are entirely different from the below.
I expect a message like this, which I am able to verify manually using the browser debugger.
{
"locationId": 110,
"name": "GAME_STARTED", <---------------------
"payload": {
"id": 146418,
"boxId": 2002,
"userId": 419,
"createdAt": "2022-02-17T09:10:16",
"lastModifiedAt": "2022-02-17T09:10:22.189",
"completedAt": "2022-02-17T09:10:22.07",
"activationMethod": "TAG",
"nfcTagId": "123423423412342134",
"gameCount": 1,
"app": false
}
}
Alternatively, would this work?
thread A:
open socket
while (true):
read socket
if message ~ 'GAME_STARTED':
break
thread B:
send HTTP REST API request # triggers event to be sent
Here are the parameters used to connect and where I specify the response pattern, which needs wildcards or a JSON expression.
You can consider using WebSocket Text Frame Filter
If you add the filter configured like above it will remove all the frames which don't contain GAME_STARTED text so the WebSocket Single Read sampler will not "see" them, this way you can just have one sampler without any loops or other logic.
More information:
Smart close with filter sample example test plan
JMeter WebSocket Samplers - A Practical Guide

Rest API enveloping or not

I have one question that i am finding hard to clearly identify the correct answer.
My API has one endpoint that does pagination. So, in the response i have to return to the client the total number of lines afected by the query. The count of records in practice.
I have read that, i should be passing metadata like what i want in the body, enveloping it, but i have also read that, it is not ok to afect the body with meta-data and that the future is to return only non meta-data on the body meaning that it must be on the response header.
O'reilly
If the information you are conveying through a custom HTTP header is important for the correct interpretation of the request or response, include that information in the body of the request or response or the URI used for the request. Avoid custom headers for such usages.
So, my question is, what is the correct approach to the problem? Should i pass the number of lines in response headers or put it in the message body.
Thank you very much.
For pagination, you could use an envelope in the response payload:
{
"data": [...],
"paging": {
"first": "http://api.example.com/foo?page=1&size=10",
"previous": "http://api.example.com/foo?page=2&size=10"
"next": "http://api.example.com/foo?page=4&size=10"
"last": "http://api.example.com/foo?page=5&size=10"
}
}
A similar approach is described in the RFC 5005: each page of results is a separate resource, which contains hyperlinks to the previous and next page of results
You also could use the Link header (line breaks are just for readability purposes):
Link: <http://api.example.com/foo?page=1&size=10>; rel="first",
<http://api.example.com/foo?page=2&size=10>; rel="previous",
<http://api.example.com/foo?page=4&size=10>; rel="next",
<http://api.example.com/foo?page=5&size=10>; rel="last"
Check the link relations registered in IANA.
For returning the number of records, I've seen some APIs using custom headers such as X-Total-Count, but I don't recommend such approach. Send those details in the response payload then.

Accepting api keys in HTTP headers or JSON POST data

I have an HTTP JSON endpoint localhost:8000/resource/ that takes JSON data as input and returns JSON as output. I am now adding API Key based authorization to the endpoint. There are 2 ways to accept the API Key at this endpoint:
Method A: In the request headers
Example python code:
import requests
headers = {
'API-Key': '<my-api-key>',
}
r = requests.post('http://localhost:8000/resource/',
json={'input': <value>},
headers=headers)
Method B: In the JSON data itself
Example python code:
import requests
r = requests.post('http://localhost:8000/resource/',
json={'input': <value>, 'API-Key': '<my-api-key>'},)
I usually notice Method A being adopted. Is there anything wrong with latter approach in which the API key is passed along with the other json data?
I think it has to do with clarity, the API Key isn't really relevant to the input, it's just a form of authorization.
Large frameworks that deal with routing and such are able to filter based on specific headers, and it would be cumbersome to filter based off of a specific form of input in the request body that would require user to intervene and obtain that value for it. Headers are simpler, and suffice for simple data that would fit in a hash-table.

POSTa Request with rest assured api using XML Payload

I am in need of one requirement.I want to POST a request using Rest Assured API using XML payload,I did not get any where how to set the body using XML. any one please tell me how I can achieve this.
I have one table customers with 5 fileds,name,id,address,email and phone number.My URL to post the request is something like
"http://com.myproject.app:8080/MyApp/SchmaName/customers".Please any one help me out.Thanks in advance
The above should work, here it is again in a slightly different version with your example...
Response response = given().
contentType("application/xml").
body(myXML).
when().
post("http://com.myproject.app:8080/MyApp/SchmaName/customers");
myXML would be the xml you want to send (5 fields; name,id,address,email and phone number). After that you can pull the result from response.
You can just post in the request body as a String, InputStream, byte[] or a Java object (that will be serialized to XML using JAXB). For example:
String myXML = ..
given().contentType(ContentType.XML).body(myXML).when().post("/x").then(). ..

GET request with data in body

I'm implementing an API endpoint that serves data but makes no modifications to data - it's something along the lines of "GET all items that match this list of filters", where a filter could be something like "ID > 200" or "propertyA != null".
In actual implementation, I have to send an array to the endpoint, specifying a bunch of resources on a per-id basis to GET back to the client. Something like
GET api/tickets
{
ids: [1, 3, 5, 7, 9],
filter: "on-sale"
}
From what I understand, a Restfully implemented api would not use GET for this kind of request as it's expected that only the id of the target resource is specified in the url, with no contents in the body.
Though I hate to think I would have to hamfist this thing into a PUT or POST request.
What's the right thing to do here?
HTTP does not allow you to sending meaningful information in the GET body. You can however send lists in the request URI.
This URI is perfectly valid.
GET /tickets?ids=1,3,5,7,9&filter=on-sale