-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(citrus-http): easy json response helpers
- Loading branch information
Showing
3 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...trus-http/src/test/java/org/citrusframework/http/actions/HttpServerActionBuilderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package org.citrusframework.http.actions; | ||
|
||
import org.citrusframework.endpoint.Endpoint; | ||
import org.citrusframework.http.message.HttpMessage; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.testng.annotations.BeforeMethod; | ||
import org.testng.annotations.Test; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.springframework.test.util.ReflectionTestUtils.getField; | ||
import static org.testng.AssertJUnit.assertEquals; | ||
import static org.testng.AssertJUnit.assertTrue; | ||
|
||
public class HttpServerActionBuilderTest { | ||
|
||
private static final TestJsonObject JSON_OBJECT_REPRESENTATION = new TestJsonObject("value"); | ||
private static final String JSON_STRING_REPRESENTATION = """ | ||
{ | ||
"property" : "value" | ||
}"""; | ||
|
||
private HttpServerActionBuilder fixture; | ||
|
||
private static void verifyOkJsonResponse(HttpServerResponseActionBuilder.HttpMessageBuilderSupport httpMessageBuilderSupport) { | ||
Object responseMessage = getField(httpMessageBuilderSupport, "httpMessage"); | ||
assertTrue(responseMessage instanceof HttpMessage); | ||
|
||
HttpMessage httpMessage = (HttpMessage) responseMessage; | ||
|
||
assertEquals(HttpStatus.OK, httpMessage.getStatusCode()); | ||
assertEquals(MediaType.APPLICATION_JSON_VALUE, httpMessage.getContentType()); | ||
assertEquals(JSON_STRING_REPRESENTATION, httpMessage.getPayload(String.class).replace("\r\n", "\n")); | ||
} | ||
|
||
@BeforeMethod | ||
public void beforeMethodSetup() { | ||
fixture = new HttpServerActionBuilder(mock(Endpoint.class)); | ||
} | ||
|
||
@Test | ||
public void sendOkJsonFromString() { | ||
HttpServerResponseActionBuilder.HttpMessageBuilderSupport httpMessageBuilderSupport = fixture.respondOkJson(JSON_STRING_REPRESENTATION); | ||
verifyOkJsonResponse(httpMessageBuilderSupport); | ||
} | ||
|
||
@Test | ||
public void sendOkJsonFromObject() { | ||
HttpServerResponseActionBuilder.HttpMessageBuilderSupport httpMessageBuilderSupport = fixture.respondOkJson(JSON_OBJECT_REPRESENTATION); | ||
verifyOkJsonResponse(httpMessageBuilderSupport); | ||
} | ||
|
||
private record TestJsonObject(String property) { | ||
} | ||
} |