如何在REST Assured中发送带有Authentication头的GET请求?

时间:2018-05-04 16:08:31

标签: rest authentication junit junit4 rest-assured

我正在尝试使用标头" Auth-Service-Id:my_app"向网址发送GET请求," Auth-Identifier:blaBlaBlabla" ," Auth-Secret:myauth-secret"使用REST Assured,JUnit。

public class RestAPITest {

@Test
public void getRequestTime() throws URISyntaxException {

    // Specify the base URL to the RESTful web service
    Response response = RestAssured.get("https://myurl.com");        
    logger.log(Level.INFO, response.time() + " milliseconds");

    int statusCode = response.getStatusCode();
    logger.log(Level.INFO, "Expected response status code is 200. Recieved response status code is " + statusCode);
    Assert.assertEquals(statusCode, 200);
    logger.log(Level.INFO, "Response Body is " + response.body().asString());
}

1 个答案:

答案 0 :(得分:0)

您可以使用RestAssured的 headers() 方法传递标题,如:

Map<String, String> headers = new HashMap<>();
    headers.put("Auth-Service-Id", "my-app");
    headers.put("Auth-Identifier", "blaBlaBlabla");

Response response = RestAssured.given(this.requestSpecification)
            .headers(headers)
            .when()
            .get("https://myurl.com");

如需进一步阅读,请按this链接。