Spring/Spring MVC

[Spring] HTTP 요청 맵핑 - 헤더와 매개변수

TheWing 2020. 11. 15. 17:33

헤더와 매개변수

특정한 헤더가 있는 요청을 처리하고 싶은 경우

  • @RequestMapping(headers = “key”)
@Controller
public class SampleController {

    @GetMapping(
            value = "/hello",
            headers = HttpHeaders.AUTHORIZATION
    )
    @ResponseBody
    public String hello() {
        return "hello";
    }
}
@WebMvcTest
public class SampleControllerTest {

    @Autowired
    MockMvc mockMvc;

    @Test
    public void helloTest() throws Exception {
        mockMvc.perform(get("/hello")
                .header(HttpHeaders.AUTHORIZATION,"localhost")
        )
                .andDo(print())
                .andExpect(status().isOk())
        ;

    }

}
  • 테스트 결과 성공

특정한 헤더가 없는 요청을 처리하고 싶은 경우

  • @RequestMapping(headers = “!key”)
@WebMvcTest
public class SampleControllerTest {

    @Autowired
    MockMvc mockMvc;

    @Test
    public void helloTest() throws Exception {
        mockMvc.perform(get("/hello")
                .header(HttpHeaders.FROM,"localhost")
        )
                .andDo(print())
                .andExpect(status().isOk())
        ;
    }
}
@Controller
public class SampleController {

    @GetMapping(
            value = "/hello",
            headers = "!"+HttpHeaders.AUTHORIZATION
    )
    @ResponseBody
    public String hello() {
        return "hello";
    }
}
  • 테스트 결과 성공

특정한 헤더 키/값이 있는 요청을 처리하고 싶은 경우

  • @RequestMapping(headers = “key=value”)
@Controller
public class SampleController {

    @GetMapping(
            value = "/hello",
            headers = HttpHeaders.AUTHORIZATION+"="+"111"
    )
    @ResponseBody
    public String hello() {
        return "hello";
    }
}
@WebMvcTest
public class SampleControllerTest {

    @Autowired
    MockMvc mockMvc;

    @Test
    public void helloTest() throws Exception {
        mockMvc.perform(get("/hello")
                .header(HttpHeaders.AUTHORIZATION,"111")
        )
                .andDo(print())
                .andExpect(status().isOk())
        ;
    }
}
  • 테스트 결과 성공

특정한 요청 매개변수 키를 가지고 있는 요청을 처리하고 싶은 경우

  • @RequestMapping(params = “a”)
@Controller
public class SampleController {

    @GetMapping(
            value = "/hello",
            params = "name"
    )
    @ResponseBody
    public String hello() {
        return "hello";
    }
}
@WebMvcTest
public class SampleControllerTest {

    @Autowired
    MockMvc mockMvc;

    @Test
    public void helloTest() throws Exception {
        mockMvc.perform(get("/hello")
                .param("name","sungjun")
        )
                .andDo(print())
                .andExpect(status().isOk())
        ;
    }
}
  • 테스트 결과 성공

특정한 요청 매개변수가 없는 요청을 처리하고 싶은 경우

  • @RequestMapping(params = “!a”)
@Controller
public class SampleController {

    @GetMapping(
            value = "/hello",
            params = "!name" //1
            params = "!named" //2
    )
    @ResponseBody
    public String hello() {
        return "hello";
    }
}
@WebMvcTest
public class SampleControllerTest {

    @Autowired
    MockMvc mockMvc;

    @Test
    public void helloTest() throws Exception {
        mockMvc.perform(get("/hello")
                .param("name","sungjun")
        )
                .andDo(print())
                .andExpect(status().isOk())
        ;
    }
}
  • 1번 테스트 결과 실패
  • 2번 테스트 결과 성공

특정한 요청 매개변수 키/값을 가지고 있는 요청을 처리하고 싶은 경우

  • @RequestMapping(params = “a=b”)
@Controller
public class SampleController {

    @GetMapping(
            value = "/hello",
            params = "name=sungjun"
    )
    @ResponseBody
    public String hello() {
        return "hello";
    }
}
@WebMvcTest
public class SampleControllerTest {

    @Autowired
    MockMvc mockMvc;

    @Test
    public void helloTest() throws Exception {
        mockMvc.perform(get("/hello")
                .param("name","sungjun")
        )
                .andDo(print())
                .andExpect(status().isOk())
        ;
    }
}
  • 테스트 결과 성공

Reference