-
Notifications
You must be signed in to change notification settings - Fork 103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Json in postBody for request object #8
Comments
Sure. Will do! thank you. In the meanwhile you can do something like that if you are having problem:
|
Unfortunately it doesn't work for me. I've figured out it works with such definition(in single quote): It would be great to be able define And stubby won't match request for single-line view of json to multi-line view. |
Did you try putting the ">" sign after postBody, and keeping the JSON indented on the new line as per example?
|
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 |
Hi Misha, I have implemented Please refer to YAML sample under Cheers |
Hi @azagniotov , Thanks for your quick enhancement with supporting But there is an issue with json specifying directly in request definition. Steps to reproduce: - 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:
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:
|
We have found with @OleksandrBerezianskyi that problem is located in YamlScanner org.yaml.snakeyaml.scanner.ScannerImpl.scanLineBreak(). It transforms '\r\n' to '\n'.
And posted body from code is:
|
Hi Alexander Zagniotov, In other words: SnakeYaml parser ruins multiline strings.
That is why request/response from file works like a charm. |
Hi, Yeah I was afraid that this might happen. I was sceptical about this way of raw string comparison. Btw, stubby4j is now on maven central |
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). |
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... Thanks for your comments, I will tweak the docs |
Let's say that you are posting YAML to server in HTTP request and server answers with YAML back to you in HTTP response. 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. |
Yep, you got it right. |
Do you really want to care about white space in the configured structure?
and
should return positive match when comparing
and
|
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:
|
I think it would be better at this stage to simply advise users to use |
100% agree with you |
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. |
Hi,
Could you add example to YAML configuration sample how to define json in post body?(if it's possible)
Example
method: POST
url: /some/uri?param=true&anotherParam=false
headers:
content-type: application/json
postBody: {
"attr1":"value1",
"attr2":"value2"
}
The text was updated successfully, but these errors were encountered: