MethodOverrideMiddleware extends AbstractWebMiddleware<HttpRequest, NRES> { @Override public HttpResponse handle(HttpRequest request, MiddlewareChain<HttpRequest, NRES, ?, ?> chain) { String val = getterFunction.apply(request); if (val != null) { request.setRequestMethod(val); } return castToHttpResponse(chain.next(request)); }  void setGetterFunction(String functionName); void setGetterFunction(Function<HttpRequest, String> getterFunction); @Override HttpResponse handle(HttpRequest request, MiddlewareChain<HttpRequest, NRES, ?, ?> chain);  }
@Test void defaultIsParameter_method() { MethodOverrideMiddleware<HttpResponse> middleware = new MethodOverrideMiddleware<>(); HttpRequest request = builder(new DefaultHttpRequest()) .set(HttpRequest::setParams, Parameters.of("_method", "PUT")) .build(); MiddlewareChain<HttpRequest, HttpResponse, ? ,?> chain = new DefaultMiddlewareChain<>(new AnyPredicate<>(), null, (Endpoint<HttpRequest, HttpResponse>) req -> { assertThat(req.getRequestMethod()).isEqualTo("PUT"); return builder(HttpResponse.of("hello")).build(); }); middleware.handle(request, chain); }
@Test void overrideUsingByHeader() { MethodOverrideMiddleware<HttpResponse> middleware = builder(new MethodOverrideMiddleware<HttpResponse>()) .set(MethodOverrideMiddleware::setGetterFunction, "X-Override-Method") .build(); HttpRequest request = builder(new DefaultHttpRequest()) .set(HttpRequest::setParams, Parameters.of("_method", "PUT")) .set(HttpRequest::setHeaders, Headers.of("X-Override-Method", "DELETE")) .build(); MiddlewareChain<HttpRequest, HttpResponse, ?, ?> chain = new DefaultMiddlewareChain<>(new AnyPredicate<>(), null, (Endpoint<HttpRequest, HttpResponse>) req -> { assertEquals("DELETE", req.getRequestMethod()); return builder(HttpResponse.of("hello")).build(); }); middleware.handle(request, chain); }