6

I initially implemented a controller to handle OPTIONS requests and apply the headers required for CORS to the response. Recently I have been cleaning up code and created an interceptor for the CORS stuff. Part of that cleanup would be to remove the controller methods for the OPTIONS requests.

However, my mockmvc tests require the OPTIONS handling methods to be there, while when I actually run the app the interceptor chain handles the OPTIONS requests, even though there is no controller method for the OPTIONS request.

I would expect a test like this

MockHttpServletRequestBuilder optionsRequest = options("the-url");

this.mockMvc.perform(optionsRequest).
    andExpect(status().isOk()).
    andExpect(header().string("Access-Control-Allow-Origin", "*")).
    andExpect(header().string("Access-Control-Allow-Methods", "OPTIONS,GET,HEAD,POST,PUT,DELETE,TRACE,CONNECT")).
    andExpect(header().string("Access-Control-Allow-Headers", "content-type"));

to pass if my test setup has

StandaloneMockMvcBuilder builder = MockMvcBuilders.standaloneSetup(this.controller);
builder = builder.dispatchOptions(true);
builder = builder.addInterceptors(new CorsInterceptor());

either that or I expect I would not be seeing the cross origin requests working on the running app.

hvgotcodes
  • 118,147
  • 33
  • 203
  • 236

0 Answers0