return FALSE; $r = well_tag_thread__update(array('id' => $id), $update); return $r; } function well_tag_thread_find($tagid, $page, $pagesize) { $arr = well_tag_thread__find(array('tagid' => $tagid), array('id' => -1), $page, $pagesize); return $arr; } function well_tag_thread_find_by_tid($tid, $page, $pagesize) { $arr = well_tag_thread__find(array('tid' => $tid), array(), $page, $pagesize); return $arr; } ?>WebFilter Mock Testing for Spring boot 3.3.5 workaround - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

WebFilter Mock Testing for Spring boot 3.3.5 workaround - Stack Overflow

programmeradmin3浏览0评论

I have changed the version from 3.3.1 to 3.3.5 for spring boot in my project.

This caused the http headers to read only when using a filters. I found a workaround for this and its working perfectly fine.

But the trouble is that i am unable to write test for this inspite of many tries.

The workaround is

@Bean
  @Order(Ordered.HIGHEST_PRECEDENCE)
  WebFilter writeableHeaders() {
    return (exchange, chain) -> {
      HttpHeaders writeableHeaders =
          HttpHeaders.writableHttpHeaders(exchange.getRequest().getHeaders());
      ServerHttpRequestDecorator writeableRequest =
          new ServerHttpRequestDecorator(exchange.getRequest()) {
            @Override
            public HttpHeaders getHeaders() {
              return writeableHeaders;
            }
          };
      ServerWebExchange writeableExchange = exchange.mutate().request(writeableRequest).build();
      return chain.filter(writeableExchange);
    };
  }

In my test case, i can use wiremock or MockWebServer. But Code is failing with 404 error

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureWebTestClient
public class WebFilterWorkaroundTest {

  @Autowired
  private WebTestClient webTestClient;

  private static WireMockServer wireMockServer;

  @BeforeAll
  static void startWireMock() {
    wireMockServer = new WireMockServer(7653);
    wireMockServer.start();

    wireMockServer.stubFor(get(urlEqualTo("/instance-details/instance-type/abc"))
            .willReturn(aResponse()
                    .withHeader("Content-Type", "application/json")
                    .withHeader("Custom-Header", "Value")
                    .withHeader("Nokia-ProjectHub-Project-ID","xyz")
                    .withBody("{\"message\":\"success\"}")
                    .withStatus(200))
    );

    //configureFor("localhost", wireMockServer.port());

    System.setProperty("wiremock.server.port", String.valueOf(wireMockServer.port()));
    System.out.println("WireMock is running on port: " + wireMockServer.port());

  }

  @AfterAll
  static void stopWireMock() {
    if (wireMockServer != null && wireMockServer.isRunning()) {
      wireMockServer.stop();
    }
  }

  @Test
  void testWriteableHeadersFilter() {
    webTestClient.get()
            .uri("http://localhost:7653/instance-details/instance-type/abc")
            .header("Nokia-ProjectHub-Project-ID","xyz")
            .exchange()
            .expectStatus().isOk()
            .expectHeader().valueEquals("Custom-Header", "Value")
            .expectBody()
            .jsonPath("$.message").isEqualTo("success");
  }
}

Can someone tell me if my approach is wrong, or if i am doing something wrong

I have tried to use wiremock to mock some api randomly that will git this workaround such that the "exhange" chain.filter is getting hit and the headers cam be modified.

The expectation is that the api should return a response with modified headers

发布评论

评论列表(0)

  1. 暂无评论