I am running into a problem where two distinct paths are mapping to the same resource. Please let me know why the following 2 paths are mapping to the same path:
get("/test/:idtest/:idsimple", (request, response) -> "");
get("/test/all/:idtest", (request, response) -> "");
Following two call map to the same:
curl -X GET -i http://localhost:4567/test/2/3
curl -X GET -i http://localhost:4567/test/all/5
Thanks
The reason for these two requests to be mapped to the first route is the order you defined them. Spark Java documentation mentions here that:
Routes are matched in the order they are defined. The first route that matches the request is invoked.
When you call http://localhost:4567/test/2/3 Java Spark would try to match it first with the first route you defined "/test/:idtest/:idsimple":
The variable idtest would be matched to 2
The variable idsimple would be matched to 3
When you call http://localhost:4567/test/all/5 Java Spark would try to match it first with the first route you defined again:
The variable idtest would be matched to all
The variable idsimple would be matched to 5
So both of them match and therefore mapped to this route.
If you change the order of the routes definitions, then "/test/all/:idtest" will be the first path to match against and then calling http://localhost:4567/test/all/5 would be mapped to the right route, while calling http://localhost:4567/test/2/3 would fail the first one and would be mapped to the second.
Related
I'm using Spring Boot 2.4.6. For delete APIs getting 405 method not found. My endpoint is like: beauty/v1/sites/addressTemplates/:templateId
Path variable: ##$%#
Can someone please suggest what can be done to make this behavior as not complaining for 405? Please direct me to other questions in case I'm missing something.
I guess that your issue has nothing to do with Spring. Maybe you are trying to compose the whole URL by using reserved characters.
In a URL, a hash mark, number sign, or pound sign ( # ) points a browser to a specific spot in a page or website. It is used to separate the URI of an object from a fragment identifier. Source.
Which means that an URL which looks like:
beauty/v1/sites/addressTemplates/##$%#
is not exactly what you imagine it to be because # is interpreted in a special way. What you have to do is to percent encode the "special" path variable so it will look like this at the end:
beauty/v1/sites/addressTemplates/%23%40%24%25%23
Then Spring will not complain anymore and will resolve properly the endpoint.
Im' using OcpSoft rewrite, and I have this only rule for forwarding:
#Override public Configuration getConfiguration(ServletContext servletContext) {
return ConfigurationBuilder.begin().addRule(
Join.path("/x/{vendor}/{url}")
.to("/vendors/{url}")
);
}
Now, this works fine, I can find the "vendor" parameter in my list of parameters, and the "url" too.
The problem is that when I debug, I can see that the vendor and url are present multiple times instead of just once in the list of parameters:
Here, {url} is added 16 (!!) times in my list of parameters.
Do you know why ?
Ok, this is actually a bug of the library: https://github.com/ocpsoft/rewrite/issues/223
So, yeah.
Here in My HTTP Request I extracted some variables using JSON Path Extractor. I want to create a Java Request it will validate the above variables. i.e I want to check the value of reqVar1 is equal to resVar1 or not and reqVar2 is equal to resVar2 or not like that.
You are making things over complicated, you could achieve the same using normal Response Assertion.
For example, if you have a JMeter Variable ${var1} and you need to compare it to ${var2}, ${var3} and ${var4} you can just configure the Response Assertion like:
More information on conditionally marking sampler results as successful or failed: How to Use JMeter Assertions in Three Easy Steps
Instead of Java Request sampler, you can add BeanShell Assertion to compare the values. following is the code:
if(vars.get("reqVar1").equals(vars.get("resVar1")))
{
if(vars.get("reqVar2").equals(vars.get("resVar2")))
{
SampleResult.setResponseCode("200");
SampleResult.setResponseMessage("SUCESS");
}
else{
SampleResult.setResponseCode("403"); // keep error code as per your wish
SampleResult.setResponseMessage("reqVar2 and reqVar2 are NOT same" + vars.get("reqVar2") + vars.get("resVar2"));
}
}
else{
SampleResult.setResponseCode("403"); // keep error code as per your wish
SampleResult.setResponseMessage("reqVar1 and reqVar1 are NOT same" + vars.get("reqVar1") + vars.get("resVar1"));
}
Add the BeanShell Assertion as a child element to the HTTP Sampler in which you are getting the values for reqVar1, reqVar2, resVar1, resVar2
Once the decision is taken based on If condition, you can change the response code and message using SampleResult
Check the following reference that shows all methods available to you:
https://jmeter.apache.org/api/org/apache/jmeter/samplers/SampleResult.html
Image reference:
I'm testing RESt service which has path parameter.
/my-service/v1/Customer/order/{ordernumber}
I want to increment the number by 1 for each request. How to achieve this in Jmeter? Till now i had been passing a fixed path param, therefor our test result were on only one input parameter.
/my-service/v1/Customer/order/5247710017785924
The good point to start with is putting your initial order value into User Defined Variable
Given start order as "5247710017785924" you need to create an "ordernumber" variable and set it's value to 5247710017785924.
After each request you can increment variable value by adding BeanShell postprocessor to your HTTP Sampler with following code:
long ordernumber = Long.parseLong(vars.get("ordernumber"));
ordernumber++;
vars.put("ordernumber",String.valueOf(ordernumber));
And set ordernumber in your HTTP Sampler path as
/my-service/v1/Customer/order/${ordernumber}
None of the solutions worked for me. Here is what I did
Define HTTP request as shown below and add path /api/v2/state/find/${id} to the request
Right click on HTTP request --> Preprocessor -> User Parameters ->Add variable -> input id and it's value
Start HTTP request, this should work
Use JMeter Counter component to increment variable.
This question is path parameter related, where the value of the order number is incremented by 1 in each successive request. But I faced a scenario where I got a list of order numbers and I had to make request for those order numbers. So, I am gonna answer this question with respect to that, this solution can be applied in both the scenarios.
What I did is put all the parameter paths in a CSV file, like this -
/my-service/v1/Customer/order/5247710017785924
/my-service/v1/Customer/order/5247710017785976
/my-service/v1/Customer/order/5247710017785984
/my-service/v1/Customer/order/5247710017785991
Then I iterated through the list of paths in the CSHTTPle and made http request to the server. To know how to iterate through the CSV file and make http request in Jmeter, you can check this link:
https://stackoverflow.com/a/47159022/5892553
You can use a JMeter Counter:
right click on your Thread Group (under the Test Plan)
select Add–>Config Element–>Counter
set the Starting value (0), Increment (1), Maximum value, Exported Variable Name ("ordernumber")
Then you can use the exported variable name as path param:
/my-service/v1/Customer/order/${ordernumber}
I used a BeanShell PreProcessor to generate an id
vars.put("id", UUID.randomUUID().toString());
Then used the path Http Request
/api/v1/event/${id}/
BINGO!!!
I'm writing a RESTful Java server with CXF framework.
How do I can write a #Path Regular Expression in order to obtain any URI finished in "/action" value?
Not sure if its /action/*, /*/action, or /*/action/* you want?. Anyway here goes:
1) /action/* can be matched by
#Path("/action/{search:.*}")<br>
doStuff(#PathParam("search") List<PathSegment> list)
In this example, a request like GET /action/order/2/price will be served by the doStuff() method where list can be used to get to all the path segments in order/2/price captured by the regular expression.
2) /*/action can be matched by (WARNING untested)
#Path("/{search:.*}/action")
findStuff(#PathParam("search") List<PathSegment> list)
In this example, a request like GET /item/2/action will be served by the findStuff() method where list can be used to get to all the path segments in item/2 captured by the regular expression.
3) /*/action/* Here I believe you are out of luck (feel free to correct me if I am wrong), for further info check this blog post.