Comments (18)
Sure. Will do! thank you.
In the meanwhile you can do something like that if you are having problem:
request:
method: POST
url: /some/uri?param=true&anotherParam=false
headers: content-type: application/json
postBody: >
{ "attr1":"value1", "attr2":"value2" }
from stubby4j.
Unfortunately it doesn't work for me. I've figured out it works with such definition(in single quote):
postBody: '{ "attr1":"value1", "attr2":"value2" }'
It would be great to be able define file
parameter for request object as response has. Because most json serializers output such json representation:
{
"attr1": "value1",
"attr2": "value2"
}
And stubby won't match request for single-line view of json to multi-line view.
from stubby4j.
Did you try putting the ">" sign after postBody, and keeping the JSON indented on the new line as per example?
postBody: >
{ "attr1":"value1", "attr2":"value2" }
from stubby4j.
When you say: "it does not work for me", what do you mean? You get errors during the YAML parse time or when you POSTing, you cant match the stubbed request?
Can you please provide more information.
Cheers
from stubby4j.
Hi Misha,
I have implemented file
in request
object. Just get the version stubby4j-1.0.23-SNAPSHOT.jar
from artifacts
or build the project your self. Keep on mind that queryParams
is now query
and postBody
is now post
.
Please refer to YAML sample under docs
Cheers
from stubby4j.
Hi @azagniotov ,
Thanks for your quick enhancement with supporting file
attribute in request
object. It works perfect.
But there is an issue with json specifying directly in request definition.
Steps to reproduce:
1.Define such request/response:
- request:
method: POST
url: /test/url
headers:
content-type: application/json
post: >
{
"name": "value",
"param": "description"
}
response:
headers:
content-type: application/json
status: 200
body: >
{"status" : "OK"}
2.Example of test code:
@Test
public void test1() throws Exception
{
// GIVEN
class Foo {
private String name = "value";
private String param = "description";
public String getName() {
return name;
}
public String getParam() {
return param;
}
}
String content = TestUtils.JSON_OBJECT_MAPPER.writeValueAsString(new Foo());
//configuration from step 1 loaded from file
String config = messages.getMessage("test_configuration");
final ClientRequestInfo adminRequest =
new ClientRequestInfo(HttpMethods.POST, "/stubdata/new", "localhost", ADMIN_PORT, config);
INTEGRATION_SERVER.makeRequestWith(adminRequest);
// WHEN
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://localhost:8882/test/url");
httpPost.setHeader(CONTENT_TYPE, "application/json");
httpPost.setHeader(ACCEPT, "application/json");
httpPost.setEntity(new StringEntity(content, "UTF-8"));
HttpResponse response = httpclient.execute(httpPost);
// THEN
String actualContent = RestTestUtils.toStringAndClose(response.getEntity().getContent());
System.out.println("Response Content:" + actualContent);
}
3.Response:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1"/>
<title>Error 404 No data found for POST request at URI /test/url for post data: {
"name" : "value",
"param" : "description"
}</title>
</head>
<body>
<h2>HTTP ERROR: 404</h2>
<p>Problem accessing /test/url. Reason:
<pre> No data found for POST request at URI /test/url for post data: {
"name" : "value",
"param" : "description"
}</pre></p>
<hr /><i><small>Powered by Jetty://</small></i>
If you change request/response definition in such way and execute code above - everything works OK.
- request:
method: POST
url: /test/url
headers:
content-type: application/json
file: path/to/post/file/test.txt
response:
headers:
content-type: application/json
status: 200
body: >
{"status" : "OK"}
where test.txt contains:
{
"name" : "value",
"param" : "description"
}
from stubby4j.
We have found with @OleksandrBerezianskyi that problem is located in YamlScanner org.yaml.snakeyaml.scanner.ScannerImpl.scanLineBreak().
It transforms '\r\n' to '\n'.
So, request body from request definition is
{\n "name" : "value",\n "param" : "description"\n }\n
And posted body from code is:
{\r\n "name" : "value",\r\n "param" : "description"\r\n }\r\n
from stubby4j.
Hi Alexander Zagniotov,
In other words: SnakeYaml parser ruins multiline strings.
There are 2 issues:
- system line breaks are not preserved - SnakeYaml always put '\n' as a line break (see http://code.google.com/p/snakeyaml/source/browse/src/main/java/org/yaml/snakeyaml/scanner/ScannerImpl.java around line 1604 scanBlockScalar() -> 2237 scanLineBreak())
- line separator is always appended at the end (see http://code.google.com/p/snakeyaml/source/browse/src/main/java/org/yaml/snakeyaml/scanner/ScannerImpl.java around line 1629 scanBlockScalar())
That is why request/response from file works like a charm.
from stubby4j.
Hi,
Yeah I was afraid that this might happen. I was sceptical about this way of raw string comparison.
I will try to strip all the white spaces from post body at the time of comparison and see what happens.
Btw, stubby4j is now on maven central
from stubby4j.
I think stripping white spaces is not an option as well because sometimes you want a message with a know number of spaces (e.g. your server talks YAML).
I think you should just state in documentation that preferable way to configure multiline request/response body is a file or even check posted YAML afterwards.
from stubby4j.
What I meant is: you can still specify a multi line JSON POST body in the request in the YAML. Respectively, you can still POST a multiline JSON.
At the time of comparison, in other words in the equals() of StubRequest I will strip white spaces from POST body in order to reduce the chances of failing due to a missing or an extra \r\n... In fact, i can be comparing MD5 hashes of POSTed and expected POST after stripping...
Does it make sense?
Thanks for your comments, I will tweak the docs
from stubby4j.
Let's say that you are posting YAML to server in HTTP request and server answers with YAML back to you in HTTP response.
In this case you want to be sure that posted YAML had exactly same structure as you have configured because number of white spaces will affect result.
Of course you can change comparison mechanism to something that will skip only line separators by either replacing them in both sources with some common line separator (more strict option) or by removing them out completely (less strict option). This will result in more broad match which still should be quite accurate.
from stubby4j.
Yep, you got it right.
from stubby4j.
Do you really want to care about white space in the configured structure?
I think the most important is the JSON content, for example:
post: >
{
"id" :
"8",
"description" :
"sweet butter"
}
and
{"id" : "8", "description" : "sweet butter"}
should return positive match when comparing
{"id":"8","description": "sweetbutter"}
and
{"id":"8","description": "sweetbutter"}
from stubby4j.
My point is that there are many formats besides json. Theoretically, stubbed server can produce/consume XML, JSON, YAML, HTML, etc. More over your json strings can contain a white space like
{"id":"8","description": "spaces must be preserved here"}
So, it would be nice to configure matching mechanism with some options like:
- strict match
- don't match line separators
- don't match any white space characters
from stubby4j.
I think it would be better at this stage to simply advise users to use file
for large post
content that inconvenient to configure as one-liners. Since you two already confirmed that file
in request
works perfectly, I think it the simplest solution ATM
from stubby4j.
100% agree with you
from stubby4j.
I'd think making Stub server dumb from the response type would be ideal. We cannot expect Stub server to recognize all sorts of response bodies. File is definitely the way to go here.
We should close this ticket.
from stubby4j.
Related Issues (20)
- Split yaml config file HOT 12
- How to disable the RegEx pattern cache? HOT 16
- How to match "a token not in the payload" with RegEx pattern? HOT 7
- [FEATURE REQUEST] Request proxying HOT 7
- [FEATURE REQUEST] Create a client to simplify configuration through HTTP HOT 3
- [NEW FEATURE RELEASED]: Request Proxying is GA
- [DOCKER HUB]: stubby4j multi architecture Docker images are on DockerHub HOT 3
- [NEW FEATURE RELEASED]: Added support for HTTP/2 over TLS
- [QUESTION] Regex with XML in request.file and response.file HOT 20
- [NEW FEATURE RELEASED]: Added support for WebSocket [RFC6455] over HTTP/1.1 and HTTP/1.1 with TLS
- [SECURITY] Upgrade log4j to 2.16.0 as a vulnerability workaround HOT 3
- [SECURITY] Upgrade log4j to 2.17.0 as a vulnerability workaround HOT 3
- [SECURITY] Upgrade log4j to 2.17.1 as a vulnerability workaround HOT 3
- [ENHANCEMENT] Moving the baseline to JDK 11 and moving to Jetty 11 HOT 2
- [FEATURE REQUEST] We want to have a way to get a count on requests made to stub uri and reset the count for next test to run HOT 5
- for websocket on-message's client request from the text file new line (Enter) not accepted by using of stubby4j any solution or guideline to use new line in requested file, my requirement is, in requested text file must use multiline value HOT 9
- [FEATURE REQUEST] ... Support ARM Docker Images HOT 14
- [QUESTION] Is it possible to configure websocket server to return different results HOT 6
- [QUESTION] Is snakeyaml 2.0 support in scope for next releases? HOT 5
- [BUG] stubb4j testcontainer failing with "exec /bin/sh: exec format error" HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from stubby4j.