Gatling Verifying Decoded JWT token - java

I'm trying to decode a JWT token returned, and check the sub claim for if it matches the username. But I can't seem to find the syntax to do so.
...
.check(status.is(HttpResponseStatus.OK.code()))
.check(jsonPath("$.access_token").saveAs("access_token"))
.check(jsonPath("$.refresh_token").exists)
.check(JWSObject.parse("${access_token}").getPayload.toJSONObject.get("sub").toString.substring("$username"))
I'm getting errors around it expecting a HttpCheck, is there a HTTPCheck for this type?
Thanks

This is pretty much exactly what .transform is for.
You extract the token with jsonPath, do a transform to get the sub, and then assert that it matches the username.
(I have not tried the actual jwt extract / validation)
.check(jsonPath("$.access_token").transform(jwt => JWT.decode(jwt).getClaim("sub").asString()).is("${username}")

Related

How to use Binance API, simple GET price by ticker

I would like to get only one pair from the response. And I can't really understand how I should pass my parameter.
Instructions say:
Symbol price ticker
GET /api/v3/ticker/price
Latest price for a symbol or symbols.
Weight: 1 for a single symbol; 2 when the symbol parameter is omitted
Parameters:
Name Type Mandatory Description
symbol STRING NO -
If the symbol is not sent, prices for all symbols will be returned in an array."
I'm able to get all symbols in the response body, but can't get a single one.
I have already tried (in Postman) these endpoints:
https://api.binance.com/api/v3/ticker/price/btcusdt
https://api.binance.com/api/v3/ticker/price/symbol=btcusdt
https://api.binance.com/api/v3/ticker/price/?symbol=btcusdt
Here is the link to entire API:
https://github.com/binance/binance-spot-api-docs/blob/master/rest-api.md#24hr-ticker-price-change-statistics
So, which endpoint is correct? postman result
https://api.binance.com/api/v3/ticker/price/?symbol=btcusdt
You must use Query without /
and Binance's /api/v3/ticker/price endpoint need symbol query as Upper case.
so you must request as below
https://api.binance.com/api/v3/ticker/price?symbol=BTCUSDT
You can you this API link to get all pairs only current price and symbols:
https://www.binance.com/api/v3/ticker/price
This for specific symbol and price:
https://www.binance.com/api/v3/ticker/price?symbol=BNBBTC
This for all pairs with full info:
https://api.binance.com/api/v3/exchangeInfo
This for 1 pair full info:
https://api.binance.com/api/v3/exchangeInfo?symbol=BNBBTC
Here is Binance API Detail pages:
https://binance-docs.github.io/apidocs/spot/en
https://github.com/binance/binance-spot-api-docs/blob/master/rest-api.md
Is there a chance to send more queries at once like for example
BTCUSDT and ETHUSDT ?
When I am trying various combinations I get a reply of:
{"code":-1104,"msg":"Not all sent parameters were read; read '1' parameter(s) but was sent '2'."}

How to pass an url by a rest api URL string

I have a table a with columns like pageId and page_name where values are inserted line [1,https://google.com] and so on.
Now i created an api that takes the URL and returns the pageid, so now the scenario is like:
localhost:8080/api/v1/page/https://google.com
whenever i am trying to pass it via Postman is is showing Could not send response can anyone help me to fix this problem?
The problem is that you have reserved chars in your query param.
Consider encoding your text.
So:
http://www.google.com
will become:
http%3A%2F%2Fwww.google.com
localhost:8080/api/v1/page/https://google.com
According url format documentation (see for example this article )- impossible use reserved chars (: and /) as parameters. I reccoment use something like
localhost:8080/api/v1/page/google.com
And add "https://" in service
or use
localhost:8080/api/v1/page/https~~google.com
And replaced "~~" to "://".

How do I get the value of a key if it contains a space in Rest Assured / Serenity?

I am trying to use Rest Assured in the Serenity framework to validate an endpoint response. I send an xml body to the endpoint and expect a JSON response back like so:
{"Entry ID" : "654123"}
I want to send the XML and verify in the JSON response that the value of the key "Entry ID" is not empty or null. The problem is, the key has a space in it, and I believe it is causing an error. Here is what I have so far:
SerenityRest.given().contentType(ContentType.XML)
.body(xmlBody)
.when().accept(ContentType.JSON).post(endpoint)
.then().body("Entry ID", not(isEmptyOrNullString()))
.and().statusCode(200);
This produces the error:
java.lang.IllegalArgumentException: Invalid JSON expression:
Script1.groovy: 1: unable to resolve class Entry
# line 1, column 33.
Entry ID
^
1 error
I have tried wrapping the "Entry ID" term in different ways to no avail:
.body("'Entry ID'", not(isEmptyOrNullString()))
.body("''Entry ID''", not(isEmptyOrNullString()))
.body("\"Entry ID\"", not(isEmptyOrNullString()))
.body("['Entry ID']", not(isEmptyOrNullString()))
.body("$.['Entry ID']", not(isEmptyOrNullString()))
Is it possible to get the value of a key that contains a space in Rest Assured?
You just need to escape the key with single quotes:
then().body("'Entry ID'", not(isEmptyOrNullString()))
Here's an example (tested in version 3.0.6):
// Given
String json = "{\"Entry ID\" : \"654123\"}";
// When
JsonPath jsonPath = JsonPath.from(json);
// Then
assertThat(jsonPath.getString("'Entry ID'"), not(isEmptyOrNullString()));

Mongo database Invalid BSON field name exception

I tried to follow this How to use dot in field name?. But it result as the picture. There is a additional space:-
protected Document setNestedField(Document doc, FieldValue parentField, String nestedFieldName, Object value, boolean concatenate) {
if (concatenate) {
doc.put(parentField.getSystemName() + "." + nestedFieldName, value);
}
else {
doc.put(nestedFieldName, value);
}
return doc;
}
Exception:-Invalid BSON field name photographs.inner_fields; nested exception is java.lang.IllegalArgumentException: Invalid BSON field name photographs.inner_fields.
How can I use dot "." in field name. I have to use . as I'm using some 3rd party api and I have no option to replace to something else like [dot]. Please suggest me?
In MongoDB field names cannot contain the dot (.) character as it is part of dot-notation syntax, see the documentation.
What third party API are you using ? Are you sure you need a dot ? Dots are commonly used when parsing JSON and your third party API should not need it.
So, a third party api is both constructing the keys (with periods in them), AND saving that to MongoDB?
I suggest that you open a bug ticker in said API:s tracker.
If this is not the case, encode the periods somewhere in the persistence code - and decode it on the way up.

How do you query sent items using gmail api?

When I use label:sent as a search query in the gmail UI it takes me to sent items but when I use a labelId of sent from the gmail API for messages (https://developers.google.com/gmail/api/v1/reference/users/messages/list) I get an error "Invalid label: sent" - just wondering how do I query for sent items from the API? Also is there a reference / examples for the type of input you can use for the "q" input parameter for the gmail API?
Thanks
If you're doing a list with "?labelId=" then use "SENT" (in upper case) as per:
https://developers.google.com/gmail/api/guides/labels
(those should probably be case insensitive but they are not.)
For the "?q=" parameter to the list methods it says on the URL you give:
Supports the same query format as the Gmail search box. For example, "from:someuser#example.com rfc822msgid: is:unread".
For more examples, I just tried searching for "gmail search queries" and got:
https://support.google.com/mail/answer/7190?hl=en
which gives lots of useful keywords, they should all work with the "q=" parameter (you may need to URL escape them, depending on language/client libraries).

Categories