2022년 2월 26일 기록

yshjft·2022년 2월 26일
0

하루 기록

목록 보기
14/16

✔︎ Cookie와 ResponseCookie

//생성
Cookie cookie = new Cookie(“key”, “value”);

// 기타 설정(만료시간 설정)
cookie.setMaxAge(3600) // 초단위
...

// 추가
httpServletResponse.add(cookie); 

ResponseCookie

// 생성 및 기타설정
ResponseCookie responseCookie = ResponseCookie.from(refreshTokenKey, refreshToken)
        .maxAge(3600)
        .build();

// 추가1: ResponseEntity 이용
ResponseEntity
        .status(HttpStatus.OK)
        .header(HttpHeaders.SET_COOKIE, responseCookie.toString());

// 추가2: HttpServletResponse 이용
httpServletResponse
	.addHeader(HttpHeaders.SET_COOKIE, responseCookie.toString());

결과(응답 헤더)

Set-Cookie: key = value; Max-Age: 3600;

✔︎ json으로 데이터를 주고 받을 때 null 필드는 제외하고 싶은 경우

  • @JsonInclude(JsonInclude.Include.NON_NULL)를 사용
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResponseDto<T> {
    private int status;
    private String message;
    private T result; // null일 수도 있는 필드
    …..
}

✔︎ test에서 assert와 verify의 차이

assert

예상하고 있는 값을 반환하고 있는지 확인한다.

verify

함수가 제대로 호출되고 있는지 확인한다.

✔︎ test with SecurityContextHolder

  • 방법1
@Mock
Authentication authentication
@Mock
SecurityContext securityContext;
...

when(securityContext.getAuthentication()).thenReturn(authentication);
SecurityContextHolder.setContext(securityContext);
...
  • 방법2
@Mock
SecurityContext securityContext;
...

Authentication authentication = new UsernamePasswordAuthenticationToken(userDto, "");
when(securityContext.getAuthentication()).thenReturn(authentication);
SecurityContextHolder.setContext(securityContext);
...
profile
꾸준히 나아가자 🐢

0개의 댓글