Reference regex string In JSON response - spring

How to reference regex string In the JSON response
url value(consumer(regex('/connectors/(.*?)/status')))
So that if I request '/connectors/foo/status' I get { "name": "foo" }

Please read the docs - https://docs.spring.io/spring-cloud-contract/docs/current/reference/html/project-features.html#contract-dsl-referencing-request-from-response
You'd need to do
response {
body(name: fromRequest().path(1))
}

Related

[Jmeter]-> Less Than or Greater Than assertion in Jmeter Assertion on API response field

I have an API that responds to a parameter in JSON response body:
{
"metadata":
{
"count": 12206883,
"pagesize": 100,
"page": 1,
"total_pages": 122069,
"query_time": "1129ms"
}
}
I need to put an assertion in the "query_time" field value that it should be:
<= 1000 ms
I added JSON assertion in JMeter, but it is failing with the below message:
:Value expected to match regexp '<=1000', but it did not match: '102'
Can someone tell me how we can achieve it?
I think you should consider using JSON JMESPath Assertion
Example JSON:
{
"some_attribute": [
{
"query_time": 112
}
]
}
Example assertion configuration:
Textual representation just in case:
length(some_attribute[?query_time<=`1000`])
More information:
JMESPath Functions
JMESPath Examples
The JMeter JSON JMESPath Extractor and Assertion: A Guide
Got the answer. Would like to share so that it will help others:
Extract the value with the help of JSON Extractor.
For example:
Create variable: querytime
JSON Path expression: $.metadata.query_time
Now in JSR223 Assertion, write a script: Language: Groovy
String jsonString = vars.get("querytime");
int length1=jsonString.length();
String Qtime1=jsonString.substring(0,(length1-2));
int time = Qtime1.toInteger()
log.info ("The querytime is " + time);
if (time>1000)
{
AssertionResult.setFailureMessage("The Querytime is taking more than 1000ms");
AssertionResult.setFailure(true);
}

Getting 415 Unsupported Media Type when trying to send request body in text/plain

I have an API which accepts both json and text as request body.
#Path("submitObjects")
#Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN})
#Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN})
public List<APIResult> submitMultiObjects(#DefaultValue("false") #QueryParam(("suppressWarnings"))Boolean suppressWarnings,List<MyObject> objects) {
// API logic
}
class MyObject{
private String name;
private String description;
// and getters and setters
}
When I hit the API with request body in JSON it works fine.
[{
"name": "name1",
"description": "description1"
},{
"name": "name2",
"description": "description2"
}]
success for above request body
But when I pass request body in plain text it fails with error code 415 - Unsupported media type
name=name1
description=description1
failed for above request body
name=name1
description=description1
name=name2
description=description2
failed for above request body
I wanted to know how we can pass list of objects in plain/ text format.
For below API where I am using MyObject instead of List
#Path("submitObject")
#Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN})
#Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN})
public List<APIResult> submitObject(#DefaultValue("false") #QueryParam(("suppressWarnings"))Boolean suppressWarnings,MyObject object) {
// API logic
}
name=name1
description=description1
above request body works fine.

Ruby on Rails - JSON parse error unexpected token

I read json file after load it into json but i got error JSON::ParserError unexpected token at, i couldn't json parse.Below i mentioned what i got output after file read
Here my code,
file = File.read("sample.json")
hash = JSON.load(file)
after read my json file,
"{\"rename\"=>[{\"from\"=>\"TTTC\", \"to\"=>\"AAAB\"}, {\"from\"=>\"AAAA\", \"to\"=>\"Description\"}, {\"from\"=>\"AAAC\", \"to\"=>\"test\"}], \"drop\"=>{\"fields\"=>[\"AAAG\", \"AAAH\"]}}"
This is not valid in json =>. JSON looks like
{ "rename": [{ "from": "TTTC" }] }
That's not JSON that's a string created by applying the inspect method to a hash.
You can convert it back to a hash with eval
hash = eval(file)
However eval can be a security hole, so you should only do this if you're confident about the source and contents of the file.
If you're encountering this error while testing - make sure to add .to_json to your request's body hash.
Example:
headers = {
'ACCEPTS' => 'application/json'
}
post '/api-endpoint-here', { name: 'Dylan Pierce' }.to_json, headers

How do i send JsonObject with nested values as Post request in REST assured

I am using rest assured -https://code.google.com/p/rest-assured/wiki/Usage
My JsonObject looks like this
{
"id": "12",
"employeeInfo": null,
"employerInfo": null,
"checkDate": 1395093997218,
"netAmount": {
"amount": 70,
"currency": "USD"
},
"moneyDistributionLineItems": [
{
"mAmount": 100,
"employeeBankAccountId": "BankAccount 1"
}
],
}
how can i send this as part of parameters using REST-assured POST?
I have tried
given().param("key1", "value1").param("key2", "value2").when().post("/somewhere").then().
body(containsString("OK"));
but that is not scalable for HUGE objects with nested values. Is there a better approach?
You just send the JSON document in the body. For example if you have your JSON document in a String called myJson then you can just do like this:
String myJson = ..
given().contentType(JSON).body(myJson).when().post("/somewhere"). ..
You can also use a POJO, input stream and byte[] instead of a String.
URL file = Resources.getResource("PublishFlag_False_Req.json");
String myJson = Resources.toString(file, Charsets.UTF_8);
Response responsedata = given().header("Authorization", AuthorizationValue)
.header("X-App-Client-Id", XappClintIDvalue)
.contentType("application/vnd.api+json")
.body(myJson)
.with()
.when()
.post(dataPostUrl);

Error parsing string to JSON

I have a below String in Ruby. is there any way by which i can convert this string into JSON
{
drawer: {
stations: {
tv: {
header: "TV Channels",
logos: {
one: "www1",
two: "www2",
three: "www3"
}
}
}
}
}
Ideally there should be double quotes before and after the terms( like "drawer" instead of drawer). But the data returned from server is in the above format.
I am trying to use the JSON library to parse the string.
require 'json'
'something'.to_json #=> "\"something\""

Resources