【问题标题】:Micronaut: Unable to test Redirect response with junitMicronaut:无法使用 junit 测试重定向响应
【发布时间】:2019-11-14 00:04:13
【问题描述】:

我正在尝试测试控制器返回 HttpResponse.seeOther(URI.create(redirectUri)); 的重定向场景。问题是我的测试用例遵循重定向而不是返回 HttpResponse<String> 或类似的东西。如何在 Micronaut 中进行测试?

控制器

  @Post("/some/endpoint")
  public HttpResponse<?> capture(@Body Map<String, String> body)
      throws Exception {
    // do something
    String uri = getRedirectUri();
    return HttpResponse.seeOther(URI.create(redirectUri));
  }

测试

  @Test
  public void testCapture() {
    String expectedUri = getRedirectUri();
    //following doesn't work
    String redirectUrl = client.toBlocking().retrieve(HttpRequest.POST("/some/endpoint",  body), String.class);
    assertEquals(expectedUri, redirectUrl);

    //following also doesn't work
    HttpResponse<String> response = client.toBlocking().retrieve(HttpRequest.POST("/some/controller", someBody), HttpResponse.class);
    assertEquals(HttpStatus.SEE_OTHER, response.status());
  }

错误

16:59:32.321 [nioEventLoopGroup-1-3] TRACE i.m.http.client.DefaultHttpClient - HTTP Client Response Received for Request: POST http://localhost:65164/some/endpoint
16:59:32.322 [nioEventLoopGroup-1-3] TRACE i.m.http.client.DefaultHttpClient - Status Code: 303 See Other
16:59:32.322 [nioEventLoopGroup-1-3] TRACE i.m.http.client.DefaultHttpClient - Location: http://localhost:8080/target/location.html
16:59:32.322 [nioEventLoopGroup-1-3] TRACE i.m.http.client.DefaultHttpClient - Date: Wed, 13 Nov 2019 23:59:32 GMT
16:59:32.322 [nioEventLoopGroup-1-3] TRACE i.m.http.client.DefaultHttpClient - connection: close
16:59:32.322 [nioEventLoopGroup-1-3] TRACE i.m.http.client.DefaultHttpClient - Response Body
16:59:32.322 [nioEventLoopGroup-1-3] TRACE i.m.http.client.DefaultHttpClient - ----
16:59:32.322 [nioEventLoopGroup-1-3] TRACE i.m.http.client.DefaultHttpClient - 
16:59:32.322 [nioEventLoopGroup-1-3] TRACE i.m.http.client.DefaultHttpClient - ----


io.micronaut.http.client.exceptions.HttpClientException: Connect Error: Connection refused: localhost/127.0.0.1:8080

    at io.micronaut.http.client.DefaultHttpClient.lambda$null$24(DefaultHttpClient.java:1035)
    at io.netty.util.concurrent.DefaultPromise.notifyListener0(DefaultPromise.java:511)
    at io.netty.util.concurrent.DefaultPromise.notifyListeners0(DefaultPromise.java:504)
    at io.netty.util.concurrent.DefaultPromise.notifyListenersNow(DefaultPromise.java:483)
    at io.netty.util.concurrent.DefaultPromise.notifyListeners(DefaultPromise.java:424)
    at io.netty.util.concurrent.DefaultPromise.tryFailure(DefaultPromise.java:121)
    at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.fulfillConnectPromise(AbstractNioChannel.java:327)
    at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:343)
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:632)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:579)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:496)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:458)
    at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:897)
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
    at java.lang.Thread.run(Thread.java:748)
Caused by: io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: localhost/127.0.0.1:8080
    at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
    at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
    at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:327)
    at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:340)
    ... 7 more
Caused by: java.net.ConnectException: Connection refused
    ... 11 more

【问题讨论】:

    标签: java junit micronaut


    【解决方案1】:

    我测试了某些场景,观察到Micronaut 客户端处理某些类型的Response(Http See Other, Bad Request, Server Error)。
    在您的情况下,一种解决方法是尝试获取对象,该对象由See Other 指向的端点和asserting 它给出。

    但是如果真的想得到String只有在Controller方法中,把你的返回改成HttpResponse.status(HttpStatus.SEE_OTHER).body(URI.create(redirectUri);

    【讨论】:

      【解决方案2】:

      通过使用返回发布者的 client.exchange() 方法实现了解决方法。 io.reactivex.subscribers.TestSubscriber 可用于订阅和测试断言。

      HttpClient.exchange()方法签名-

      <I, O> Publisher<HttpResponse<O>> exchange(HttpRequest<I> request, Class<O> bodyType) {...}
      

      使用 TestSubscriber-

      的工作测试用例
      @Test
      public void testCapture() {
        //Instead of using client.toBlocking().retrieve(...)
        /*
        String redirectUrl = client.toBlocking()
            .retrieve(HttpRequest.POST("/some/endpoint",  body), String.class);
        */
      
        // Use client.exchange(...)
        Publisher<HttpResponse<Object>> exchange =
          client.exchange(HttpRequest.POST("/payment/1234567890/capture", body)
              .contentType(MediaType.APPLICATION_FORM_URLENCODED), Object.class);
      
        TestSubscriber<HttpResponse<?>> testSubscriber = new TestSubscriber<HttpResponse<?>>() {
          @Override
          public void onNext(HttpResponse<?> httpResponse) {
            assertNotNull(httpResponse);
            assertEquals(HttpStatus.SEE_OTHER, httpResponse.status());
            assertEquals(getRedirectUri(), httpResponse.header("location"));
          }
        };
      
        exchange.subscribe(testSubscriber);
      
        // await to allow for response
        testSubscriber.awaitTerminalEvent(2, TimeUnit.SECONDS);
      
        // assert for errors, completion, terminated etc.
        testSubscriber.assertNoErrors();
        testSubscriber.assertComplete();
        testSubscriber.assertTerminated();
      }
      

      【讨论】:

        猜你喜欢
        • 2021-04-14
        • 1970-01-01
        • 1970-01-01
        • 2021-04-09
        • 1970-01-01
        • 1970-01-01
        • 2021-06-14
        • 1970-01-01
        • 2014-11-29
        相关资源
        最近更新 更多