java - 问题: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)
...
我搜索了错误,但找不到解决方案。如果有人可以指导我,我会很高兴。
非常感谢
解决方案
推荐阅读
- excel - 合并和中心值的 VLookup 存在问题
- matlab-figure - Matlab:来自`text()`的文本底部被切碎
- sql-server - 从 SQL Server 中删除所有程序集
- c# - 获取在 C# 事务中在对象内部创建的对象的 ID
- apache-spark - 使用 spark 独立集群部署 predictionio
- highcharts - 图例:换行,因此以下图例从下一行开始
- c++ - 静态成员错误地初始化为空字符串
- java - 我想将资产管理器用于图像
- c# - POST http 请求 C# - 请求有效负载
- python - Python 无法连接到 MySQL 数据库:OperationalError