如何使用pact-jvm-provider-junit + http basic auth + Eclipse TPC/IP Monitor?

How using pact-jvm-provider-junit + http basic auth + Eclipse TPC/IP Monitor?

我想做这样的事情:

private static final String protocol = "http";

// host verification via http basic auth, http://username:passwort@url ...
private static final String host = "http://username:password@myservice.com";
private static final int port = 80;

private static final String path = "/";

@TestTarget
public final Target target = new HttpTarget(protocol, host, port, path);

问题是: 我想通过 TCP/IP 监视器分析我的连接,它似乎没有机会使用 http basic auth。 如何使用 http 基本身份验证通过 TCP/IP 监视器转发连接?

我可以根据 headers 设置必要的参数吗?也许是这样的:

@TargetRequestFilter
public void exampleRequestFilter(HttpRequest request) {
    request.addHeader("Authorization", "OAUTH hdsagasjhgdjashgdah...");
}

或者还有其他选项可以让我如何使用 http basic auth + TCP/IP Monitor?

提前致谢!

我发现当我通过 base64 编码时,我可以使用 http base 身份验证。 解决方案应如下所示:

@TargetRequestFilter
public void exampleRequestFilter(HttpRequest request) {
    // Authorization header Base64 encoded...
    String encoded = Base64.getEncoder()
            .encodeToString((username + ":" + password).getBytes(StandardCharsets.UTF_8));
    request.addHeader("Authorization", "Basic " + encoded);
}

对于 Junit 5,这是不同的 here:

    @TestTemplate
    @ExtendWith(PactVerificationInvocationContextProvider.class)
    void testTemplate(PactVerificationContext context, HttpRequest request) {
      // This will add a header to the request
      request.addHeader("X-Auth-Token", "1234");
      context.verifyInteraction();
    }