Skip to content

Commit 5edabce

Browse files
committed
Created convenience functions for generating common types of responses
1 parent 425087b commit 5edabce

2 files changed

Lines changed: 72 additions & 1 deletion

File tree

spring/fluentforms-spring-boot-autoconfigure/src/main/java/com/_4point/aem/fluentforms/spring/AemProxyAfSubmission.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,44 @@ public sealed interface SubmitResponse permits SubmitResponse.Response, SubmitRe
280280
/**
281281
* A Normal response with a 200 HTTP status code (204 if the responseBytes variable is empty)
282282
*/
283-
public record Response(byte[] responseBytes, String mediaType) implements SubmitResponse {};
283+
public record Response(byte[] responseBytes, String mediaType) implements SubmitResponse {
284+
/**
285+
* Creates a text response from a String
286+
*
287+
* @param text
288+
* Text to go into the response.
289+
* @return
290+
* Response object with a media type of "text/plain"
291+
*/
292+
public static Response text(String text) { return new Response(text.getBytes(StandardCharsets.UTF_8), MediaType.TEXT_PLAIN); }
293+
/**
294+
* Creates an HTML response from a String
295+
*
296+
* @param html
297+
* String containing HTML. No checking is done to ensure that this is valid HTML.
298+
* @return
299+
* Response object with a media type of "text/html"
300+
*/
301+
public static Response html(String html) { return new Response(html.getBytes(StandardCharsets.UTF_8), MediaType.TEXT_HTML); }
302+
/**
303+
* Creates an JSON response from a String
304+
*
305+
* @param json
306+
* String containing JSON. No checking is done to ensure that this is valid JSON.
307+
* @return
308+
* Response object with a media type of "application/html"
309+
*/
310+
public static Response json(String json) { return new Response(json.getBytes(StandardCharsets.UTF_8), MediaType.APPLICATION_JSON); }
311+
/**
312+
* Creates an XML response from a String
313+
*
314+
* @param xml
315+
* String containing XML. No checking is done to ensure that this is valid XML.
316+
* @return
317+
* Response object with a media type of "application/xml"
318+
*/
319+
public static Response xml(String xml) { return new Response(xml.getBytes(StandardCharsets.UTF_8), MediaType.APPLICATION_XML); }
320+
};
284321
/**
285322
* A Temporary Redirect (307 HTTP status code) response
286323
*/

spring/fluentforms-spring-boot-autoconfigure/src/test/java/com/_4point/aem/fluentforms/spring/AemProxyAfSubmissionTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@
88

99
import java.net.URI;
1010
import java.net.URISyntaxException;
11+
import java.nio.charset.StandardCharsets;
12+
import java.util.function.Function;
1113

1214
import org.glassfish.jersey.client.ClientProperties;
1315
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
1416
import org.glassfish.jersey.media.multipart.MultiPartFeature;
1517
import org.glassfish.jersey.server.ResourceConfig;
1618
import org.junit.jupiter.api.BeforeEach;
1719
import org.junit.jupiter.api.Test;
20+
import org.junit.jupiter.params.ParameterizedTest;
21+
import org.junit.jupiter.params.provider.EnumSource;
1822
import org.slf4j.Logger;
1923
import org.slf4j.LoggerFactory;
2024
import org.springframework.boot.SpringApplication;
@@ -29,6 +33,7 @@
2933
import com._4point.aem.fluentforms.spring.AemProxyAfSubmission.AfSubmitLocalProcessor;
3034
import com._4point.aem.fluentforms.spring.AemProxyAfSubmission.AfSubmissionHandler;
3135
import com._4point.aem.fluentforms.spring.AemProxyAfSubmission.AfSubmitProcessor;
36+
import com._4point.aem.fluentforms.spring.AemProxyAfSubmission.AfSubmissionHandler.SubmitResponse;
3237
import com.github.tomakehurst.wiremock.client.WireMock;
3338
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
3439

@@ -303,4 +308,33 @@ public Response processRequest(FormDataMultiPart inFormData, HttpHeaders headers
303308
}
304309
}
305310

311+
public static class SubmitResponseResponseTests {
312+
private static final String SAMPLE_TEXT = "text";
313+
private static final byte[] SAMPLE_TEXT_BYTES = SAMPLE_TEXT.getBytes(StandardCharsets.UTF_8);
314+
315+
enum TestScenario {
316+
TEXT("text/plain", SubmitResponse.Response::text),
317+
HTML("text/html", SubmitResponse.Response::html),
318+
JSON("application/json", SubmitResponse.Response::json),
319+
XML("application/xml", SubmitResponse.Response::xml)
320+
;
321+
final String expectedContentType;
322+
final Function<String, SubmitResponse.Response> methodUnderTest;
323+
324+
private TestScenario(String expectedContentType, Function<String, SubmitResponse.Response> methodUnderTest) {
325+
this.expectedContentType = expectedContentType;
326+
this.methodUnderTest = methodUnderTest;
327+
}
328+
}
329+
330+
@ParameterizedTest
331+
@EnumSource
332+
void testText(TestScenario scenario) {
333+
var result = scenario.methodUnderTest.apply(SAMPLE_TEXT);
334+
assertAll(
335+
()->assertArrayEquals(SAMPLE_TEXT_BYTES, result.responseBytes()),
336+
()->assertEquals(scenario.expectedContentType, result.mediaType())
337+
);
338+
}
339+
}
306340
}

0 commit comments

Comments
 (0)