首页 > 解决方案 > 问题:io.restassured.internal.http.HttpResponseException:未经授权

问题描述

我是 Rest API 的新手。我正在尝试在 IntelliJ IDEA 中运行这个测试函数来测试 OMDB API:

@Test
    public void shouldNotGetResponseWithoutApiKey(){
        Response response = RestAssured.given()
                .queryParam("t", "Harry Potter")
                .get("http://www.omdbapi.com")
                .then()
                .statusCode(401)
                .extract().response();

        assertThat(response.getBody().jsonPath().getString("Error"), Matchers.containsString(errorMessage));
    }

这里的逻辑是:我希望这个测试给出状态码 401,因为 API 密钥没有提供给 Rest Assured。这是非常直接的。但我收到此错误:

    io.restassured.internal.http.HttpResponseException: Unauthorized
    
        at io.restassured.internal.http.HTTPBuilder.defaultFailureHandler(HTTPBuilder.java:627)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:107)
        at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:323)
        at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1268) 
        ...

我搜索了错误,但找不到解决方案。如果有人可以指导我,我会很高兴。

非常感谢

标签: javarestintellij-idearest-assured

解决方案


推荐阅读