일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 언리얼엔진
- 문자열
- unityui
- 스택
- Unity
- 포톤
- 순열
- Unity2D
- C++
- Unity3d
- 내일배움캠프
- Photon
- 구현
- UE4
- 이분탐색
- 알고리즘
- 프로그래머스
- 스파르타내일배움캠프
- 유클리드호제법
- BFS
- Inventory
- 워크플로
- QueryDSL
- 스파르타내일배움캠프TIL
- Firebase
- 해시
- FSM
- UnrealEngine
- c#
- 유니티
Archives
- Today
- Total
개발 낙서장
[Spring] @RequestPart 테스트 HttpMediaTypeNotSupportedException 본문
문제 상황
유닛 테스트 중 @RequestPart가 있는 API 테스트에서
HttpMediaTypeNotSupportedException: Content-Type 'application/octet-stream' is not supported 오류 발생
@PostMapping("/chat-rooms")
public GetChatRoomResponse createChatRoom(
@RequestPart(value = "chatRoom") CreateChatRoomRequest request,
@RequestPart(value = "image", required = false) MultipartFile image,
@AuthenticationPrincipal UserDetailsImpl userDetails) {
return chatRoomService.createChatRoom(request, image, userDetails.getUser());
}
@Test
@DisplayName("채팅방 생성")
void createChatRoom() throws Exception {
// given
CreateChatRoomRequest request = CreateChatRoomRequest.builder()
.chatRoomName("채팅방")
.description("채팅방 설명")
.chatRoomTags(Arrays.asList("태그1", "태그2", "태그3"))
.build();
MockMultipartFile image = new MockMultipartFile(
"image",
"test.jpg",
MediaType.IMAGE_JPEG_VALUE,
"test image".getBytes()
);
// when&then
mockMvc.perform(multipart("/api/v1/chat-rooms")
.file("chatRoom", objectMapper.writeValueAsBytes(request))
.file(image)
.contentType(MediaType.MULTIPART_FORM_DATA)
.accept(MediaType.APPLICATION_JSON)
.principal(principal))
.andExpect(status().isOk());
}
채팅 방을 생성하는 API이고 커버 이미지와 함께 업로드가 필요하기에 @RequestPart로 요청 값을 받았음.
이후 multipart로 테스트를 진행했지만 Content-Type이 맞지 않아서 에러가 발생함.
해결
[Spring] MultipartFile을 이용한 파일 업로드 테스트 (MultipartFile과 함께 전송되는 데이터를 DTO에 바인
MultipartFile과 함께 넘어오는 데이터를 DTO로 받기
velog.io
위 글을 참고하여 해결했음.
이미지는 이미지 파일 형태로 보내지만 Dto는 Json 형식으로 보내져야 하기에 ObjectMapper로 변환한 다음 Json 형식의 파일로 만들어 해결했다!
@Test
@DisplayName("채팅방 생성")
void createChatRoom() throws Exception {
// given
CreateChatRoomRequest request = CreateChatRoomRequest.builder()
.chatRoomName("채팅방")
.description("채팅방 설명")
.chatRoomTags(Arrays.asList("태그1", "태그2", "태그3"))
.build();
String postInfo = objectMapper.writeValueAsString(request);
MockMultipartFile chatRoom = new MockMultipartFile("chatRoom", "chatRoom",
"application/json",
postInfo.getBytes(StandardCharsets.UTF_8));
MockMultipartFile image = new MockMultipartFile(
"image",
"test.jpg",
MediaType.IMAGE_JPEG_VALUE,
"test image".getBytes()
);
// when&then
mockMvc.perform(multipart("/api/v1/chat-rooms")
.file(chatRoom)
.file(image)
.contentType(MediaType.MULTIPART_FORM_DATA)
.accept(MediaType.APPLICATION_JSON)
.principal(principal))
.andDo(print())
.andExpect(status().isOk());
}
MockHttpServletResponse:
Status = 200
Error message = null
Headers = [Content-Type:"application/json"]
Content type = application/json
Body = {"userName":"abc123","chatRoomId":1,"chatRoomName":"채팅방","description":"채팅방 설명","coverImage":"http://example.com/test.jpg","chatRoomTags":["태그1","태그2","태그3"]}
Forwarded URL = null
Redirected URL = null
Cookies = []
'Java' 카테고리의 다른 글
[JSP] SpringBoot + JSP 설정 (0) | 2024.12.12 |
---|---|
[JAVA] compareTo (0) | 2024.11.26 |
Spring + Redis Cache (0) | 2024.05.13 |
JDK 17을 쓰는 이유 (1) | 2024.05.02 |
[Spring] WebSocket 통신 (0) | 2024.03.28 |
Comments