Merge pull request #13256 from etrandafir93/features/BAEL-6058-extracting_request_header
BAEL-6058: extracting request header
This commit is contained in:
commit
618f549b0e
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.requestheader;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.baeldung.requestheader.interceptor.OperatorHolder;
|
||||
|
||||
@RestController
|
||||
public class BuzzController {
|
||||
private final OperatorHolder operatorHolder;
|
||||
|
||||
public BuzzController(OperatorHolder operatorHolder) {
|
||||
this.operatorHolder = operatorHolder;
|
||||
}
|
||||
|
||||
@GetMapping("buzz")
|
||||
public String buzz() {
|
||||
return "hello, " + operatorHolder.getOperator();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.requestheader;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class FooBarController {
|
||||
|
||||
@GetMapping("foo")
|
||||
public String foo(HttpServletRequest request) {
|
||||
String operator = request.getHeader("operator");
|
||||
return "hello, " + operator;
|
||||
}
|
||||
|
||||
@GetMapping("bar")
|
||||
public String bar(@RequestHeader("operator") String operator) {
|
||||
return "hello, " + operator;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package com.baeldung.requestheader;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableWebMvc
|
||||
public class HeaderInterceptorApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(HeaderInterceptorApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package com.baeldung.requestheader.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.context.annotation.ScopedProxyMode;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
import com.baeldung.requestheader.interceptor.OperatorHolder;
|
||||
import com.baeldung.requestheader.interceptor.OperatorInterceptor;
|
||||
|
||||
@Configuration
|
||||
public class HeaderInterceptorConfig implements WebMvcConfigurer {
|
||||
|
||||
@Override
|
||||
public void addInterceptors(final InterceptorRegistry registry) {
|
||||
registry.addInterceptor(operatorInterceptor());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public OperatorInterceptor operatorInterceptor() {
|
||||
return new OperatorInterceptor(operatorHolder());
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)
|
||||
public OperatorHolder operatorHolder() {
|
||||
return new OperatorHolder();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.requestheader.interceptor;
|
||||
|
||||
public class OperatorHolder {
|
||||
private String operator;
|
||||
|
||||
public String getOperator() {
|
||||
return operator;
|
||||
}
|
||||
|
||||
public void setOperator(String operator) {
|
||||
this.operator = operator;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.requestheader.interceptor;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
|
||||
public class OperatorInterceptor implements HandlerInterceptor {
|
||||
|
||||
private final OperatorHolder operatorHolder;
|
||||
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
String operator = request.getHeader("operator");
|
||||
operatorHolder.setOperator(operator);
|
||||
return true;
|
||||
}
|
||||
|
||||
public OperatorInterceptor(OperatorHolder operatorHolder) {
|
||||
this.operatorHolder = operatorHolder;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,65 @@
|
|||
package com.baeldung.requestheader;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@ContextConfiguration(classes = { HeaderInterceptorApplication.class })
|
||||
@WebAppConfiguration
|
||||
public class HeaderInterceptorIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext webApplicationContext;
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenARequestWithOperatorHeader_whenWeCallFooEndpoint_thenOperatorIsExtracted() throws Exception {
|
||||
MockHttpServletResponse response = this.mockMvc.perform(get("/foo").header("operator", "John.Doe"))
|
||||
.andDo(print())
|
||||
.andReturn()
|
||||
.getResponse();
|
||||
|
||||
assertThat(response.getContentAsString()).isEqualTo("hello, John.Doe");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenARequestWithOperatorHeader_whenWeCallBarEndpoint_thenOperatorIsExtracted() throws Exception {
|
||||
MockHttpServletResponse response = this.mockMvc.perform(get("/bar").header("operator", "John.Doe"))
|
||||
.andDo(print())
|
||||
.andReturn()
|
||||
.getResponse();
|
||||
|
||||
assertThat(response.getContentAsString()).isEqualTo("hello, John.Doe");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenARequestWithOperatorHeader_whenWeCallBuzzEndpoint_thenOperatorIsIntercepted() throws Exception {
|
||||
MockHttpServletResponse response = this.mockMvc.perform(get("/buzz").header("operator", "John.Doe"))
|
||||
.andDo(print())
|
||||
.andReturn()
|
||||
.getResponse();
|
||||
|
||||
assertThat(response.getContentAsString()).isEqualTo("hello, John.Doe");
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue