HTTP 요청 매핑하기 : 헤더와 매개변수
- 특정한 헤더가 있는 요청을 처리하고 싶은 경우
ReqeustMapping(hearders="key")
- 특정한 헤더가 없는 요청을 처리하고 싶은 경우
@RequestMapping(headers="!key")
- 특정한 헤더 키/값이 있는 요청을 처리하고 싶은 경우
@ReqeustMapping(header="key=value")
- 특정한 요청 매개변수 키를 가지고 있는 요청을 처리하고 싶은 경우
@RequestMapping(params="!a")
- 특정한 요청 매개변수 키/값을 가지고 있는 요청을 처리하고 싶은 경우
- @RequestMapping(params=”a=b”)
테스트 코드
@ExtendWith(SpringExtension.class)
@WebMvcTest
class SampleControllerTest {
@Autowired
MockMvc mockMvc;
@Test
public void helloTest() throws Exception {
mockMvc.perform(get("/hello")
.header(HttpHeaders.FROM, "localhost") // 헤더 추가
.param("name", "testName") // 파라미터 추가
)
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string("hello"))
.andExpect(handler().handlerType(SampleController.class))
.andExpect(handler().methodName("hello"));
}
}
댓글남기기