FlashMiddleware extends AbstractWebMiddleware<HttpRequest, NRES> { @Override public HttpResponse handle(HttpRequest request, MiddlewareChain<HttpRequest, NRES, ?, ?> next) { request = MixinUtils.mixin(request, FlashAvailable.class); flashRequest(request); HttpResponse response = castToHttpResponse(next.next(request)); response = MixinUtils.mixin(response, FlashAvailable.class); flashResponse(response, request); return response; }  @Override HttpResponse handle(HttpRequest request, MiddlewareChain<HttpRequest, NRES, ?, ?> next); void setFlashKey(String flashKey);  }
@Test void getFlash_and_NoResponseFlash() { MiddlewareChain<HttpRequest, HttpResponse, ?, ?> chain = new DefaultMiddlewareChain<>(Predicates.any(), null, (Endpoint<HttpRequest, HttpResponse>) req -> builder(HttpResponse.of("hello")) .set(HttpResponse::setHeaders, Headers.of("Content-Type", "text/html")) .build()); request = MixinUtils.mixin(request, FlashAvailable.class); HttpResponse response = middleware.handle(request, chain); assertThat(request.getFlash().getValue()).isEqualTo("message"); assertThat(request.getSession()) .isNotNull() .doesNotContainKeys("_flash"); assertThat(response.getFlash()).isNull(); }
@Test void setFlash() { MiddlewareChain<HttpRequest, HttpResponse, ?, ?> chain = new DefaultMiddlewareChain<>(Predicates.any(), null, (Endpoint<HttpRequest, HttpResponse>) req -> builder(HttpResponse.of("hello")) .set(HttpResponse::setHeaders, Headers.of("Content-Type", "text/html")) .set(HttpResponse::setFlash, new Flash<>("new flash")) .build()); HttpResponse response = middleware.handle(request, chain); assertThat(request.getFlash().getValue()).isEqualTo("message"); assertThat(response.getFlash().getValue()).isEqualTo("new flash"); }