如何在java中通过rest-assured测试中的头文件

如何在java中通过rest-assured测试中的头文件,java,rest-assured,web-api-testing,Java,Rest Assured,Web Api Testing,我对rest assured是新手,我正在尝试做一个非常基本的测试来检查响应主体和状态 但是当我运行测试时,我得到的是401条未经授权的消息,而不是在响应正文中显示结果。当我使用Postman运行相同的API时,它工作正常 下面是我尝试过的代码,但它不起作用 我包括示例url和标题值,因为无法共享真实的url和标题 package test; import org.testng.Assert; import org.testng.annotations.Test; import io.res

我对rest assured是新手,我正在尝试做一个非常基本的测试来检查响应主体和状态 但是当我运行测试时,我得到的是401条未经授权的消息,而不是在响应正文中显示结果。当我使用Postman运行相同的API时,它工作正常

下面是我尝试过的代码,但它不起作用 我包括示例url和标题值,因为无法共享真实的url和标题

package test;

import org.testng.Assert;
import org.testng.annotations.Test;

import io.restassured.RestAssured;
import io.restassured.http.Method;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;

public class SimpleGetTest {

    @Test
    public void GetWeatherDetails() {

        RestAssured.baseURI = "https://test.com";


        RequestSpecification httpRequest = RestAssured.given();

        Response response = httpRequest.given().header("Content-Type", "application/json").given()
                .header("x-api-key", "123456").
                request(Method.GET, "/hello");

        System.out.println("Response sent =>  " + response);


        String responseBody = response.getBody().asString();
        System.out.println("Response Body is =>  " + responseBody);


        int statusCode = response.getStatusCode();
        System.out.println("Actual Status Code is : " + statusCode);

        Assert.assertEquals(statusCode, 200,
                "BUG : Status code is coming as different");
        System.out.println("Expected : Status code coming correct");

    }
}
下面是我收到的控制台消息,其中显示401

已发送响应=>io.restassured.internal。RestAssuredResponseImpl@18cebaa5 响应正文为=>“消息”:“未经授权的访问”,“代码”:“401”,“数据”:null} 实际状态代码为:200 预期:状态代码正确
通过:GetWeatherDetails

我在我的eclipse上运行了您的精确测试,它按预期工作。以下是输出:

[RemoteTestNG] detected TestNG version 6.14.3
Response sent =>  io.restassured.internal.RestAssuredResponseImpl@21526f6c
Response Body is =>  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function getCookie(c_name) { // Local function for getting a cookie value
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start!=-1) {
        c_start=c_start + c_name.length + 1;
        c_end=document.cookie.indexOf(";", c_start);

        if (c_end==-1) 
            c_end = document.cookie.length;

        return unescape(document.cookie.substring(c_start,c_end));
        }
    }
    return "";
}
function setCookie(c_name, value, expiredays) { // Local function for setting a value of a cookie
    var exdate = new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie = c_name + "=" + escape(value) + ((expiredays==null) ? "" : ";expires=" + exdate.toGMTString()) + ";path=/";
}
function getHostUri() {
    var loc = document.location;
    return loc.toString();
}
setCookie('YPF8827340282Jdskjhfiw_928937459182JAX666', '86.172.130.153', 10);
try {  
    location.reload(true);  
} catch (err1) {  
    try {  
        location.reload();  
    } catch (err2) {  
        location.href = getHostUri();  
    }  
}
</script>
</head>
<body>
<noscript>This site requires JavaScript and Cookies to be enabled. Please change your browser settings or upgrade your browser.</noscript>
</body>
</html>

Actual Status Code is : 200
Expected : Status code coming correct
PASSED: GetWeatherDetails

===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
[RemoteTestNG]检测到TestNG版本6.14.3
已发送响应=>io.restassured.internal。RestAssuredResponseImpl@21526f6c
响应主体为=>
函数getCookie(c_name){//用于获取cookie值的本地函数
如果(document.cookie.length>0){
c_start=document.cookie.indexOf(c_name+“=”);
如果(c_开始!=-1){
c_start=c_start+c_name.length+1;
c_end=document.cookie.indexOf(“;”,c_start);
如果(c_end==-1)
c_end=document.cookie.length;
返回unescape(document.cookie.substring(c_start,c_end));
}
}
返回“”;
}
函数setCookie(c_name,value,expiredays){//用于设置cookie值的本地函数
var exdate=新日期();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+“=”+escape(value)+((expiredays==null)?:“expires=“+exdate.togmString())+“path=/”;
}
函数getHostUri(){
var loc=文件位置;
返回loc.toString();
}
setCookie('YPF8827340282Jdskjhfiw_928937459182JAX666','86.172.130.153',10);
试试{
位置。重新加载(true);
}捕获(错误1){
试试{
location.reload();
}捕获(错误2){
location.href=getHostUri();
}  
}
此站点需要启用JavaScript和Cookie。请更改浏览器设置或升级浏览器。
实际状态代码为:200
预期:状态代码正确
已通过:GetWeatherDetails
===============================================
默认测试
测试运行:1,失败:0,跳过:0
===============================================
===============================================
默认套件
运行的测试总数:1,失败:0,跳过:0
===============================================

确保您具有正确的依赖项,并检查您正在使用的TestNG版本。

我在我的eclipse上运行了您的精确测试,它按预期工作。以下是输出:

[RemoteTestNG] detected TestNG version 6.14.3
Response sent =>  io.restassured.internal.RestAssuredResponseImpl@21526f6c
Response Body is =>  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function getCookie(c_name) { // Local function for getting a cookie value
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start!=-1) {
        c_start=c_start + c_name.length + 1;
        c_end=document.cookie.indexOf(";", c_start);

        if (c_end==-1) 
            c_end = document.cookie.length;

        return unescape(document.cookie.substring(c_start,c_end));
        }
    }
    return "";
}
function setCookie(c_name, value, expiredays) { // Local function for setting a value of a cookie
    var exdate = new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie = c_name + "=" + escape(value) + ((expiredays==null) ? "" : ";expires=" + exdate.toGMTString()) + ";path=/";
}
function getHostUri() {
    var loc = document.location;
    return loc.toString();
}
setCookie('YPF8827340282Jdskjhfiw_928937459182JAX666', '86.172.130.153', 10);
try {  
    location.reload(true);  
} catch (err1) {  
    try {  
        location.reload();  
    } catch (err2) {  
        location.href = getHostUri();  
    }  
}
</script>
</head>
<body>
<noscript>This site requires JavaScript and Cookies to be enabled. Please change your browser settings or upgrade your browser.</noscript>
</body>
</html>

Actual Status Code is : 200
Expected : Status code coming correct
PASSED: GetWeatherDetails

===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
[RemoteTestNG]检测到TestNG版本6.14.3
已发送响应=>io.restassured.internal。RestAssuredResponseImpl@21526f6c
响应主体为=>
函数getCookie(c_name){//用于获取cookie值的本地函数
如果(document.cookie.length>0){
c_start=document.cookie.indexOf(c_name+“=”);
如果(c_开始!=-1){
c_start=c_start+c_name.length+1;
c_end=document.cookie.indexOf(“;”,c_start);
如果(c_end==-1)
c_end=document.cookie.length;
返回unescape(document.cookie.substring(c_start,c_end));
}
}
返回“”;
}
函数setCookie(c_name,value,expiredays){//用于设置cookie值的本地函数
var exdate=新日期();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+“=”+escape(value)+((expiredays==null)?:“expires=“+exdate.togmString())+“path=/”;
}
函数getHostUri(){
var loc=文件位置;
返回loc.toString();
}
setCookie('YPF8827340282Jdskjhfiw_928937459182JAX666','86.172.130.153',10);
试试{
位置。重新加载(true);
}捕获(错误1){
试试{
location.reload();
}捕获(错误2){
location.href=getHostUri();
}  
}
此站点需要启用JavaScript和Cookie。请更改浏览器设置或升级浏览器。
实际状态代码为:200
预期:状态代码正确
已通过:GetWeatherDetails
===============================================
默认测试
测试运行:1,失败:0,跳过:0
===============================================
===============================================
默认套件
运行的测试总数:1,失败:0,跳过:0
===============================================

确保您具有正确的依赖项,并检查您正在使用的TestNG版本。

尝试下面的代码将起作用。

Map headers=newhashmap();headers.put(“身份验证服务Id”、“我的应用程序”);headers.put(“Auth标识符”、“blabla”)


Response Response=RestAssured.given(this.requestSpecification).headers(headers).when().get(“http=://xyz.com”)

试试下面的代码就行了

Map headers=newhashmap();headers.put(“身份验证服务Id”、“我的应用程序”);headers.put(“Auth标识符”、“blabla”)

Response Response=RestAssured.given(this.requestSpecification).headers(headers).when().get(“http=://xyz.com”)