diff --git a/spring-boot-modules/spring-boot-mvc-3/pom.xml b/spring-boot-modules/spring-boot-mvc-3/pom.xml index 43a492786e..f2b6c129f8 100644 --- a/spring-boot-modules/spring-boot-mvc-3/pom.xml +++ b/spring-boot-modules/spring-boot-mvc-3/pom.xml @@ -40,6 +40,18 @@ commons-io ${commons-io.version} + + org.springframework.boot + spring-boot-starter-test + test + + + + + /src/main/resources + + + \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/produceimage/controller/DataProducerController.java b/spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/produceimage/controller/DataProducerController.java index ab233a8b60..218be68a45 100644 --- a/spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/produceimage/controller/DataProducerController.java +++ b/spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/produceimage/controller/DataProducerController.java @@ -1,9 +1,12 @@ package com.baeldung.produceimage.controller; import org.apache.commons.io.IOUtils; +import org.springframework.core.io.InputStreamResource; import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import java.io.IOException; @@ -29,6 +32,18 @@ public class DataProducerController { return IOUtils.toByteArray(in); } + @GetMapping("/get-image-dynamic-type") + @ResponseBody + public ResponseEntity getImageDynamicType(@RequestParam("jpg") boolean jpg) { + final MediaType contentType = jpg ? MediaType.IMAGE_JPEG : MediaType.IMAGE_PNG; + final InputStream in = jpg ? + getClass().getResourceAsStream("/com/baeldung/produceimage/image.jpg") : + getClass().getResourceAsStream("/com/baeldung/produceimage/image.png"); + return ResponseEntity.ok() + .contentType(contentType) + .body(new InputStreamResource(in)); + } + @GetMapping(value = "/get-file", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE) public @ResponseBody byte[] getFile() throws IOException { final InputStream in = getClass().getResourceAsStream("/com/baeldung/produceimage/data.txt"); diff --git a/spring-boot-modules/spring-boot-mvc-3/src/main/resources/com/baeldung/produceimage/data.txt b/spring-boot-modules/spring-boot-mvc-3/src/main/resources/com/baeldung/produceimage/data.txt new file mode 100644 index 0000000000..5a8297fc5e --- /dev/null +++ b/spring-boot-modules/spring-boot-mvc-3/src/main/resources/com/baeldung/produceimage/data.txt @@ -0,0 +1 @@ +Hello Baeldung! \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-mvc-3/src/main/resources/com/baeldung/produceimage/image.jpg b/spring-boot-modules/spring-boot-mvc-3/src/main/resources/com/baeldung/produceimage/image.jpg new file mode 100644 index 0000000000..db3abce0d6 Binary files /dev/null and b/spring-boot-modules/spring-boot-mvc-3/src/main/resources/com/baeldung/produceimage/image.jpg differ diff --git a/spring-boot-modules/spring-boot-mvc-3/src/main/resources/com/baeldung/produceimage/image.png b/spring-boot-modules/spring-boot-mvc-3/src/main/resources/com/baeldung/produceimage/image.png new file mode 100644 index 0000000000..16d218e334 Binary files /dev/null and b/spring-boot-modules/spring-boot-mvc-3/src/main/resources/com/baeldung/produceimage/image.png differ diff --git a/spring-boot-modules/spring-boot-mvc-3/src/test/java/com/baeldung/produceimage/DataProducerControllerIntegrationTest.java b/spring-boot-modules/spring-boot-mvc-3/src/test/java/com/baeldung/produceimage/DataProducerControllerIntegrationTest.java new file mode 100644 index 0000000000..29f794645a --- /dev/null +++ b/spring-boot-modules/spring-boot-mvc-3/src/test/java/com/baeldung/produceimage/DataProducerControllerIntegrationTest.java @@ -0,0 +1,46 @@ +package com.baeldung.produceimage; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import org.springframework.web.context.WebApplicationContext; + +@SpringBootTest(classes = ImageApplication.class, + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +public class DataProducerControllerIntegrationTest { + + @Autowired + private WebApplicationContext webApplicationContext; + + private MockMvc mockMvc; + + @BeforeEach + public void setup() { + this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); + } + + @Test + void givenJpgTrue_whenGetImageDynamicType_ThenContentTypeIsJpg() throws Exception { + mockMvc.perform(get("/get-image-dynamic-type?jpg=true")) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.IMAGE_JPEG)) + .andExpect(header().stringValues(HttpHeaders.CONTENT_TYPE, MediaType.IMAGE_JPEG_VALUE)); + } + + @Test + void givenJpgFalse_whenGetImageDynamicType_ThenContentTypeIsFalse() throws Exception { + mockMvc.perform(get("/get-image-dynamic-type?jpg=false")) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.IMAGE_PNG)) + .andExpect(header().stringValues(HttpHeaders.CONTENT_TYPE, MediaType.IMAGE_PNG_VALUE)); + } + +}