DefaultMethodNotAllowedHandler implements MethodNotAllowedHandler { public void deny(RequestInfo request, Set<HttpMethod> allowedMethods) { request.getResponse().addHeader( "Allow", allowedMethods.toString().replaceAll("\\[|\\]", "")); try { if (!"OPTIONS".equalsIgnoreCase(request.getRequest().getMethod())) { request.getResponse().sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED); } } catch (IOException e) { throw new InterceptionException(e); } }  void deny(RequestInfo request, Set<HttpMethod> allowedMethods);  }
@Test public void shouldAddAllowHeader() throws Exception { this.handler.deny(request, EnumSet.of(HttpMethod.GET, HttpMethod.POST)); verify(response).addHeader("Allow", "GET, POST"); }
@Test public void shouldSendErrorMethodNotAllowed() throws Exception { this.handler.deny(request, EnumSet.of(HttpMethod.GET, HttpMethod.POST)); verify(response).sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED); }
@Test public void shouldNotSendMethodNotAllowedIfTheRequestMethodIsOptions() throws Exception { when(mockRequest.getMethod()).thenReturn("OPTIONS"); this.handler.deny(request, EnumSet.of(HttpMethod.GET, HttpMethod.POST)); verify(response, never()).sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED); }
@Test(expected=InterceptionException.class) public void shouldThrowInterceptionExceptionIfAnIOExceptionOccurs() throws Exception { doThrow(new IOException()).when(response).sendError(anyInt()); this.handler.deny(request, EnumSet.of(HttpMethod.GET, HttpMethod.POST)); }