gpt4 book ai didi

java - 如何在java中放心测试中传递 header

转载 作者:太空宇宙 更新时间:2023-11-04 09:30:15 24 4
gpt4 key购买 nike

我是新手,请放心,我正在尝试进行一个非常基本的测试来检查响应正文和状态。我将按照此 API 调用中的要求传递 header 但是当我运行测试时,我收到 401 未经授权的消息,而不是在响应正文中显示结果。当我使用 Postman 运行相同的 API 时,它工作正常

下面是我尝试过的代码,但它不起作用我添加了示例 url 和 header 值,因为无法共享真实的值

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响应正文 => "message":"UNAUTHORIZED_ACCESS","code":"401","data":null}实际状态代码是:200预期:状态代码正确通过:获取天气详细信息

最佳答案

我在我的 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
===============================================

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

关于java - 如何在java中放心测试中传递 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57162969/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com