我不熟悉使用 Rest Assured、Java 和 Api 测试,所以请对我温和一点。当我放心地测试使用 Bearer 身份验证的 api 时,测试失败导致:- java.net.ConnectException:连接被拒绝:连接
我知道这个问题可能与身份验证有关,但不确定如何使用“Bearer”。我四处搜索并认为我需要以某种方式使用我的用户名和密码发出初始请求。然后取回 token 以用于承载身份验证。 有人可以帮我用一个非常简单的例子来做到这一点吗?
我的代码是
import com.jayway.restassured.RestAssured;
import static com.jayway.restassured.RestAssured.*;
import static org.hamcrest.Matchers.hasItem;
@BeforeTest
public void setUp() {
RestAssured.enableLoggingOfRequestAndResponseIfValidationFails();
RestAssured.authentication = preemptive().basic("username","password");
}
@Test
public void successfulTest() {
given()
.contentType("application/json; charset=UTF-8");
when().
get("http://mydomain/testpath/Id=2").
then().
statusCode(200);
}
最佳答案
Response response =
given()
.headers(
"Authorization",
"Bearer " + bearerToken,
"Content-Type",
ContentType.JSON,
"Accept",
ContentType.JSON)
.when()
.get(url)
.then()
.contentType(ContentType.JSON)
.extract()
.response();
关于java - 放心承载认证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42790021/