Skip to content

Commit

Permalink
#516 [feat] interceptor에서 OPTIONS 확인 로직 추가
Browse files Browse the repository at this point in the history
#516 [feat] interceptor에서 OPTIONS 확인 로직 추가
  • Loading branch information
sohyundoh authored Sep 27, 2024
2 parents b7027e4 + 02a7836 commit 6f1b62c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import static org.springframework.web.servlet.HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE;

Expand All @@ -32,7 +33,7 @@ public class MoimAuthInterceptor implements HandlerInterceptor {

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
if (handler instanceof ResourceHttpRequestHandler) {
if (handler instanceof ResourceHttpRequestHandler || Objects.equals(request.getMethod(), "OPTIONS")) {
return true;
}
HandlerMethod method = (HandlerMethod) handler;
Expand Down
32 changes: 32 additions & 0 deletions module-api/src/test/java/com/mile/common/CorsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.mile.common;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.options;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@SpringBootTest
@AutoConfigureMockMvc
public class CorsTest {

@Autowired
private MockMvc mockMvc;

@Test
public void testCorsWithMockMvc() throws Exception {
mockMvc.perform(options("/api/moim/MQ==")
.header("Origin", "http://localhost:5173")
.header("Access-Control-Request-Method", "OPTIONS")
.header("Access-Control-Request-Headers", "Content-Type")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(print());
}
}

0 comments on commit 6f1b62c

Please sign in to comment.