尝试验证 connection.I 时出现 HTTP 401 错误,我正在使用放心自动化
Getting HTTP 401 error while trying to validate the connection.I am using rest assured to automate
/*我在下面手动传递了会话 cookie code.when 我在 post 传递了会话 cookie man 它工作正常但是在 eclipse 中它给出 401 HTTP error.I 非常新休息一下 assured.please 如果 code.I 有任何问题请告诉我并附上错误 */
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import java.security.KeyStore;
import java.security.cert.X509Certificate;
import javax.security.cert.CertificateException;
import org.apache.http.HttpResponse;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.testng.annotations.Test;
import com.sun.net.ssl.SSLContext;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.response.ValidatableResponse;
public class getart {
@Test
public void Test1()
{
// TODO Auto-generated method stub
//baseURL or Host
RestAssured.useRelaxedHTTPSValidation();
RestAssured.baseURI="https://localhost";
given().
param("Cookie","JSESSIONID=B1FAC334FF60F7182D4C552ABE01A700; hi.session.co.entity=1838-PROD1; hi.session.id.identifier=xHmvClBuIBcSKAEiVP~~AAAESADWaUjq; hi.session.client.identifier=1838Viewer").
when().
get("/hi-prod/3.1.12/al/api/articles")
.then().assertThat().statusCode(200).
body("status",equalTo("OK")).log().body();
}
}
param()
用于将查询参数添加到 URL - 这不是您想要设置 cookie 的东西。必须在请求中设置 Cookie Header.
您应该使用 cookie()
或 cookies()
。 rest-assured wiki 有一节介绍如何设置 cookie。请注意,您需要根据您的示例设置四个 cookie。
编辑:根据评论添加,
方法一
Cookie cookie1 = Cookie.Builder("JSESSIONID", "B1FAC334FF60F7182D4C552ABE01A700").build();
Cookie cookie2 = Cookie.Builder("hi.session.co.entity", "1838-PROD1").build();
Cookie cookie3 = Cookie.Builder("hi.session.id.identifier", "xHmvClBuIBcSKAEiVP~~AAAESADWaUjq").build();
Cookie cookie4 = Cookie.Builder("hi.session.client.identifier", "1838Viewer").build();
Cookies cookies = new Cookies(cookie1, cookie2, cookie3, cookie4);
given().cookies(cookies)
.when().get("/hi-prod/3.1.12/al/api/articles")
方法二
given().header("Set-Cookie", "JSESSIONID=B1FAC334FF60F7182D4C552ABE01A700; hi.session.co.entity=1838-PROD1; hi.session.id.identifier=xHmvClBuIBcSKAEiVP~~AAAESADWaUjq; hi.session.client.identifier=1838Viewer"")
.when().get("/hi-prod/3.1.12/al/api/articles")
/*我在下面手动传递了会话 cookie code.when 我在 post 传递了会话 cookie man 它工作正常但是在 eclipse 中它给出 401 HTTP error.I 非常新休息一下 assured.please 如果 code.I 有任何问题请告诉我并附上错误
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import java.security.KeyStore;
import java.security.cert.X509Certificate;
import javax.security.cert.CertificateException;
import org.apache.http.HttpResponse;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.testng.annotations.Test;
import com.sun.net.ssl.SSLContext;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.response.ValidatableResponse;
public class getart {
@Test
public void Test1()
{
// TODO Auto-generated method stub
//baseURL or Host
RestAssured.useRelaxedHTTPSValidation();
RestAssured.baseURI="https://localhost";
given().
param("Cookie","JSESSIONID=B1FAC334FF60F7182D4C552ABE01A700; hi.session.co.entity=1838-PROD1; hi.session.id.identifier=xHmvClBuIBcSKAEiVP~~AAAESADWaUjq; hi.session.client.identifier=1838Viewer").
when().
get("/hi-prod/3.1.12/al/api/articles")
.then().assertThat().statusCode(200).
body("status",equalTo("OK")).log().body();
}
}
param()
用于将查询参数添加到 URL - 这不是您想要设置 cookie 的东西。必须在请求中设置 Cookie Header.
您应该使用 cookie()
或 cookies()
。 rest-assured wiki 有一节介绍如何设置 cookie。请注意,您需要根据您的示例设置四个 cookie。
编辑:根据评论添加,
方法一
Cookie cookie1 = Cookie.Builder("JSESSIONID", "B1FAC334FF60F7182D4C552ABE01A700").build();
Cookie cookie2 = Cookie.Builder("hi.session.co.entity", "1838-PROD1").build();
Cookie cookie3 = Cookie.Builder("hi.session.id.identifier", "xHmvClBuIBcSKAEiVP~~AAAESADWaUjq").build();
Cookie cookie4 = Cookie.Builder("hi.session.client.identifier", "1838Viewer").build();
Cookies cookies = new Cookies(cookie1, cookie2, cookie3, cookie4);
given().cookies(cookies)
.when().get("/hi-prod/3.1.12/al/api/articles")
方法二
given().header("Set-Cookie", "JSESSIONID=B1FAC334FF60F7182D4C552ABE01A700; hi.session.co.entity=1838-PROD1; hi.session.id.identifier=xHmvClBuIBcSKAEiVP~~AAAESADWaUjq; hi.session.client.identifier=1838Viewer"")
.when().get("/hi-prod/3.1.12/al/api/articles")