Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.springframework.web.server.WebExceptionHandler;
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -78,14 +79,14 @@ private Mono<Void> getExceptionResponse(ServerHttpResponse response, EgovErrorCo

private Mono<Void> handlerResponse(ServerHttpResponse response, String timestamp, int status, String code, String message) {
response.setStatusCode(HttpStatus.valueOf(status));
response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
response.getHeaders().setContentType(new MediaType(MediaType.APPLICATION_JSON, StandardCharsets.UTF_8));
Map<String, Object> map = new HashMap<>();
map.put("timestamp", timestamp);
map.put("status", HttpStatus.valueOf(status));
map.put("code", code);
map.put("message", message);
JSONObject jsonObject = new JSONObject(map);
DataBuffer dataBuffer = response.bufferFactory().wrap(JSONObject.toJSONString(jsonObject).getBytes());
DataBuffer dataBuffer = response.bufferFactory().wrap(JSONObject.toJSONString(jsonObject).getBytes(StandardCharsets.UTF_8));
return response.writeWith(Mono.just(dataBuffer));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package org.egovframe.rte.ptl.reactive.exception;

import org.egovframe.rte.ptl.reactive.annotation.EgovController;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.bind.annotation.GetMapping;
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;

import static org.junit.jupiter.api.Assertions.assertTrue;

@ExtendWith(SpringExtension.class)
public class EgovExceptionHandlerTest {
Expand All @@ -14,7 +21,7 @@ public class EgovExceptionHandlerTest {

@BeforeEach
public void setUp() {
this.webTestClient = WebTestClient.bindToController(new SampleController())
this.webTestClient = WebTestClient.bindToController(new SampleController(), new KoreanMessageController())
.controllerAdvice(new EgovExceptionHandler())
.build();
}
Expand All @@ -25,7 +32,30 @@ public void exceptionHandlerTest() {
.uri("/test")
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectStatus().isNotFound();
.expectStatus().isNotFound()
.expectHeader().contentType(MediaType.parseMediaType("application/json;charset=UTF-8"));
}

@Test
public void koreanMessageTest() {
this.webTestClient.get()
.uri("/korean-message")
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectStatus().is5xxServerError()
.expectBody()
.consumeWith(result -> assertTrue(new String(result.getResponseBody(), StandardCharsets.UTF_8)
.contains("\"message\":\"서비스 예외 메시지\"")));
}

@EgovController
private static class KoreanMessageController {

@GetMapping("/korean-message")
public Mono<String> koreanMessage() {
return Mono.error(new EgovServiceException("서비스 예외 메시지"));
}

}

}