有没有办法获取 HttpHeaders 中设置的 bearerToken 以在测试中执行验证?

发布于 2025-01-11 16:39:50 字数 878 浏览 0 评论 0原文

我有一个构建 HttpHeaders 的方法。它在登录后获取 bearerToken 并将其设置在标头中。我想验证是否在单元测试中设置了令牌,但我无法找到任何方法从标头获取令牌。

这是构建标头的方法:

public static HttpHeaders buildHeaders() {
    HttpHeaders headers = new HttpHeaders();

    headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
    String bearerToken = getToken();
    headers.setBearerAuth(bearerToken);

    return headers;
}

这是我的测试:

    @Test
void shouldBuildHeaders() {
    HttpHeaders httpHeaders = Util.buildHeaders();
    assertNotNull(httpHeaders);
    assertEquals(httpHeaders.getAccept(), Collections.singletonList(MediaType.APPLICATION_JSON));

    //I would like to do something like this to verify if the token is being set.
    // String bearerToken = httpHeaders.getBearerToken();
    // assertNotNull(bearerToken);
}

有什么方法可以获取令牌,或者我测试的方式有什么问题吗?

I have a method that builds HttpHeaders. It fetches a bearerToken after logging in and sets it in the header. I would like to verify that the token is being set in a unit test, but I am unable to find any method to get the token from the header.

This is the method that builds the headers:

public static HttpHeaders buildHeaders() {
    HttpHeaders headers = new HttpHeaders();

    headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
    String bearerToken = getToken();
    headers.setBearerAuth(bearerToken);

    return headers;
}

And this is my test:

    @Test
void shouldBuildHeaders() {
    HttpHeaders httpHeaders = Util.buildHeaders();
    assertNotNull(httpHeaders);
    assertEquals(httpHeaders.getAccept(), Collections.singletonList(MediaType.APPLICATION_JSON));

    //I would like to do something like this to verify if the token is being set.
    // String bearerToken = httpHeaders.getBearerToken();
    // assertNotNull(bearerToken);
}

Is there any way to get the token, or is there anything wrong in the way I am testing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋叶绚丽 2025-01-18 16:39:50

您可以尝试循环遍历标头以查看可用的键,如下所示:

httpHeaders.forEach((key,value)->{
    System.out.println(key+"-->"+value);
});

这允许您检查拥有的所有标头。

但是,在您的情况下,您可以通过查看下面的文档来查找您想要的特定标头(我假设您正在使用 Spring):
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/HttpHeaders.html#setBearerAuth-java.lang.String-

标头键实际上是“Authorization”,这意味着您可以使用以下代码访问它的值:

String bearerToken = httpHeaders.getFirst(HttpHeaders.AUTHORIZATION);

You can try to loop trough your headers to see the keys that are available, like this:

httpHeaders.forEach((key,value)->{
    System.out.println(key+"-->"+value);
});

This allows you to check on all the headers that you have.

However in you case you can just look up the specific header that you want, by looking at the documentation bellow (I'm assuming you are using Spring):
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/HttpHeaders.html#setBearerAuth-java.lang.String-

The header key is actually "Authorization", meaning that you can access it's value by using the code bellow:

String bearerToken = httpHeaders.getFirst(HttpHeaders.AUTHORIZATION);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文