2017-09-25 2 views
0

내가이 항상 java.lang.IllegalArgumentException: Header value must not be null 내가 발견 발생합니다

@Test 
public void shouldReturnDefaultMessage() throws Exception { 
    this.mockMvc.perform(get("/")).andDo(print()).andExpect(status().isOk()) 
      .andExpect(content().string(containsString("Hello World"))); 
} 

그러나 기본적인 MVC 테스트를 실행하려고 한 필터링 나는 CORS 테스트를 필터링 비활성화하면 에서 오류없이 작업하십시오. 나는 시험 작동을 SimpleCORSFilter에 @Component를 제거하고의 SecurityConfig에 선 .addFilterBefore(new SimpleCORS...)을 제거 할 경우에만 내 보안 구성

@Configuration 
@EnableWebSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    UserDetailsServiceImp userDetailsService; 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.csrf().disable().authorizeRequests() 
       .antMatchers("/").permitAll() 
       .anyRequest().authenticated() 
       .and() 
       .addFilterBefore(new SimpleCORSFilter(),UsernamePasswordAuthenticationFilter.class); 
    } 
} 

내 SimpleCORSFilter

@Component 
public class SimpleCORSFilter implements Filter { 

    private final Logger log = LoggerFactory.getLogger(SimpleCORSFilter.class); 

    public SimpleCORSFilter() { 
     log.info("SimpleCORSFilter init"); 
    } 

    @Override 
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) 
      throws IOException, ServletException { 

     HttpServletRequest request = (HttpServletRequest) req; 
     HttpServletResponse response = (HttpServletResponse) res; 

     response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin")); 
     response.setHeader("Access-Control-Allow-Credentials", "true"); 
     //... 
     chain.doFilter(req, res); 
    } 

} 

부.

테스트에서 mockMVC를 어떻게 사용할 수 있습니까? 테스트를 위해 CORSFilter를 비활성화하는 방법 또는 mockMvc에서 요청을 올바르게 작성하여 "헤더 값이 null이 아니어야 함"에 대한 오류가 발생하지 않도록하는 방법입니다.

mockMvc에서 임의의 헤더 값을 설정하려고했지만 오류가 변경되지 않았습니다.

답변

1

java.lang.IllegalArgumentException가 : 헤더 값은 아래와 같이 (키, 값) .header 사용하여 헤더 값을 전달 null.so 안됩니다 :

@Test 
    public void shouldReturnDefaultMessage() throws Exception { 
     this.mockMvc.perform(get("/").header("Origin","*")).andDo(print()).andExpect(status().isOk()) 
       .andExpect(content().string(containsString("Hello World"))); 
    } 
+0

내가 먼저 어떤 임의의 헤더 값을 추가하려고했다. 동일한 오류가 발생했습니다. 앞서 언급 한 "원점"을 추가하면 효과가 나타납니다. 감사! – isADon