I've a response
{ errors: [
{
code: 123,
reason: "this is the reason1 is dynamically generated"
} ,
{
code: 234,
reason: "this is the reason2 for another random reason"
}
...
...
}
Now when I validate this response
I use following
...
...
And match response.errors[*].reason contains "this is the reason"
This validation fails, because there is an equality check for complete String for every reason with the phrase mentioned in the condition. this is reason1
I tried few wild cards but didn't work either, how to do it ?
You missed each:
And match each response.errors[*].reason contains "this is reason"
Refer: https://github.com/karatelabs/karate#match-each
Related
I am designing the API where all the following three cases are possible
All the inputs in the array are correct, so this API will return the 200 status code
Sample output: 200 Status code
[{ "status" : "success", "value" : "some response" }, { "status" : "success", "value" : "some response" }]
Few inputs in the array are correct, so this API will return the 207 Multi-Status code
Sample output: 207 Multi-Status code
[{ "status" : "success", "value" : "some response" }, { "status" : "fail", "value" : "reason for failure" }, { "status" : "success", "value" : "some response" }]
All the inputs are wrong in the array. In this case do I need to send the 400 Bad Request (OR) 207 status code ?
Because if I send the 400 Bad Request then the response format will not be consistent like below
Sample Output: 400 Status code
{"errorcode" : "XXXXXX", "message" : "It is failed due to invalid inputs. Input must contain XXXX"}
In Case 3, do I need to send the 400 status code response or 207 status code response with all the status as 'FAIL'. Which is correct and consistent?
There is a huge difference between 2xx and 4xx codes.
2xx like 207 means the request is valid and returns values (success or fails).
However 4xx codes like 400 means that client sent an invalid request.
For instance if you have to provide at least one input in the array and client request with an empty array, it's a bad request.
In your case, the request looks valid and you have a mixed behavior of 2xx and 4xx.
If client is able to use part of your results then it's a 207, if he can use successes without failures then the result is 2xx.
If he can't and he needs each input to be valid then it's a 4xx as the request should contains only valid inputs (resulting in success only).
For instance in case 3, a 400 makes sense as user can't continue his process with failures only.
You should be able to format this error by adding a property "details" containing your array of input's failures like
400
{
"errorcode" : "XXXXXX",
"message" : "Only invalid inputs. Input must contain XXXX",
"details" : [{
"status" : "fail",
"value" : "reason for failure"
}, {
"status" : "fail",
"value" : "reason for failure"
}, {
"status" : "fail",
"value" : "reason for failure"
}]
}
I am setting up an E2E test and chaining my request/responses. I am defining variables from each response and using them in the next call.
Its working up to a point, and then a problem surfaces when defining off the 2nd response.
If I def operationId, operationSubject, or operationStatus (e.g response.operationId), it works.
If I store anything from the results (e.g response.results.0.personId) it throws this error
Expected ; but found .0
response.results.0.personId
My response:
{
"operationId": "922459ecxxxxx",
"operationSubject": "BATCH_ENROLLMENT",
"operationStatus": "PROCESSED",
"results": {
"0": {
"personId": "367a73b5xxxx",
"status": "PRE_AUTH",
"email": "mquinter+TEST.69387488#email.com",
"loanNumber": null
},
"1": {
"personId": "56f060fd-e34xxxxxx",
"status": "PRE_AUTH",
"email": "mquintxxxx#email.com",
"loanNumber": null
}
}
}
That's not how to access data in JSON. See this similar question: https://stackoverflow.com/a/71847841/143475
Maybe you meant to do this:
* def foo = response.results[0].personId
https://stackoverflow.com/users/143475/peter-thomas
I see the issue - It wasn't finding the response because I wasn't giving it enough time before the next call.
I put a sleep in there and its working as expected.
Thanks
I am trying to test/run a mutation that creates groupChat in my DynamoDB by id,groupChatName, messages, createdTime, createdUser, users. I have 2 seperate tables, UserTable and GroupChatTable.The problem is I keep getting data is null and an error that says "the provided key element does not match the schema. ErrorCode: Validation Exception, request ID." Resolvers are attached to my tables so I am not sure why I am getting this error.
The weird thing is when I check the groupChatTable, my mutation is saved incorrectly as an input.This is what it looks like,
Ex: {"createdTime":{"S":"12:00"},"createdUser":{"S":"Me"},........
Below is the Mutation,Schema type,and Resolver.
createGroupChat(input:{
id: 4
groupChatName: "newgroup"
messages: "we love this group"
createdTime:"12:00"
createdUser: "Me"
users:"we, me"
}) {
id
groupChatName
messages
createdTime
createdUser
users
}
}```
```type GroupChat {
id: ID!
groupChatName: String!
messages: String
createdTime: String!
createdUser: String!
users: String
}```
```{
"version" : "2017-02-28",
"operation" : "PutItem",
"key" : {
"id": $util.dynamodb.toDynamoDBJson($util.autoId()),
},
"attributeValues" : $util.dynamodb.toMapValuesJson($ctx.args)
}```
It looks like the way data is being stored through resolver is incorrect and when it returns it doesn't match the schema
Instead of using $util.dynamodb.toMapValuesJson(($ctx.args))
use: $util.dynamodb.toMapValuesJson($util.parseJson($util.toJson($ctx.args.input)))
I try to learn Karate but have some issue and I can't resolve it by myself.
So my Feature is looking rather simple:
Feature: Alerting get the list of all alerts
Background:
* url 'url'
Scenario: Retrieve all alerts
Given path '5c348c553a892c000bb1f2dd'
When method get
Then status 200
And match response contains {id: 5c348c553a892c000bb1f2dd}
The case here is to fetch a response and make sure that given ID is on the list. As far I understand this documentation keyword contains should lookup only for the given phrase but I get an error: reason: all key-values did not match
This is my console output:
allAlertsGet.feature:10 - path: $, actual: {data={name=Baelish of Harrenhal, user=griffin, id=5c348c553a892c000bb1f2dd, tags=["Gared"], triggers={prometheus=[{"js_id":"Qarth","labels":["Harry Potter and the Sorcerer's Stone"],"operator":"==","query":"up","value":"1"}]}, trigger_interval=398s, receivers={slack=[{"holdoffTime":"0s","id":"Stalls","message":"Dark and difficult times lie ahead. Soon we must all face the choice between what is right and what is easy.","revokeMessage":"Every flight begins with a fall.","token":"Buckbeak"}]}, hold_cap=4, max_cap=16, factor=2, createDate=1546947669, triggered_date=1546948867, mute_until=0, muted=false, status=3}}, expected: {id=5c348c553a892c000bb1f2dd}, reason: all key-values did not match
What I have missed? I use karate 0.9.0.
Pay attention to the nested structure of your JSON. You can paste this snippet into a Scenario and try it, this is a tip - you can experiment quickly without making HTTP requests like this:
* def response = { data: { name: 'Baelish of Harrenhal', user: 'griffin', id: '5c348c553a892c000bb1f2dd' } }
* match response.data contains { id: '5c348c553a892c000bb1f2dd' }
EDIT: just to show off a few other ways to do assertions:
* match response.data.id == '5c348c553a892c000bb1f2dd'
* match response..id contains '5c348c553a892c000bb1f2dd'
* def id = { id: '5c348c553a892c000bb1f2dd' }
* match response == { data: '#(^id)' }
* match response contains { data: '#(^id)' }
I am checking for an 'OK' parameter in response but getting above mentioned error. The same code was working with my previous karate version, now I am using 0.9.0 - This might be some issue with my coding, can't figure out.
Response:
"testResponse": {
"planSummary": {
"includedServicesList": [
{
"some elements goes here": "test"
}
],
"status": {
"statusCd": "200",
"statusTxt": "OK"
}
}
}
My Feature File Code:
When method get
Then status 200
Then match response contains 'OK'
I get expected: 'OK', reason: actual value is not a string error.
I tried with Then match response contains {statusTxt: 'OK'} as well.
You can try this,
* match response.testResponse.planSummary.status contains {'statusTxt' : 'OK'}
OR
* match response.testResponse.planSummary.status.statusTxt == 'OK'