gpt4 book ai didi

java - 断言该值在java和testng或junit中的map中设置

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

我有一个用 java 编写的测试,其中包含发送到服务器的简单 HTTP 请求,然后捕获服务器响应。

这里我想要一种方法来断言我从服务器接收到的响应 header 的控制台输出?

响应 header 之一的示例:如果“Content-Length”= 12227,则测试通过,否则测试失败。

这是我的代码:

public class ResponseHeaderLoggedInUser {

@Test
public void ResponseHeadersforUSA() {

try {


//System.setOut(myconsole);
URL obj = new URL("http://www.ivivva.com/?locale=en_US");
URLConnection conn = obj.openConnection();


//Setting Request Headers//
conn.setRequestProperty("Host", "www.ivivva.com");
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0");
conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
conn.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
conn.setRequestProperty("Accept-Encoding", "gzip, deflate");
conn.setRequestProperty("Cookie", "NSC_JOv4n55lbq2w4zpexoskqbexxcrzicc=ffffffff09d205ab45525d5f4f58455e445a4a423660; UsrLocale=en_US; Country=US; ca_ord=gGYBmQjaBCVj65TJ+vOBJw==; isLoggedin=true; cartCount=0; NSC_mvmvmfnpo_tubhfw10_jwjwwb_mc=ffffffff09d205ab45525d5f4f58455e445a4a4229a0; omniID=1482345027850BoER; mbox=session#1482345027884-6794#1482347607|check#true#1482345807; __utma=210616138.1023825236.1482345028.1482345028.1482345028.1; __utmb=210616138.5.10.1482345028; __utmc=210616138; __utmz=210616138.1482345028.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); s_cc=true; s_fid=752E92B2B20003FF-05DED4FECDE6B525; c_m2=1; c_m=undefinedDirect%20LoadDirect%20Load; s_nr=1482345746223; s_vnum=1484937028265%26vn%3D1; s_invisit=true; s_lv=1482345746225; s_lv_s=First%20Visit; s_sq=%5B%5BB%5D%5D; sandy-session-id=0fa3f19b82e69e2a; sandy-client-id=93c64748da80825e; BTT_X0siD=807679838813198396; BTT_Collect=on; s_vi=[CS]v1|2C2D6522051D272B-6000016360010F9A[CE]; _ga=GA1.2.1023825236.1482345028; sl=CA; BTT_WCD_Collect=off; JSESSIONID=61AE25BF6E33856CB6FDD67DC939D9B6; regionMsgShown=true; us_ord=27qFcsniy0Bv/8EVzdZzNw==; _gat_mobifyTracker=1; __utmt_ga=1");

conn.setRequestProperty("Connection", "keep-alive");
conn.setRequestProperty("Upgrade-Insecure-Requests", "1");
conn.setRequestProperty("Cache-Control", "max-age=0");


Map<String, List<String>> map = conn.getHeaderFields();

System.out.println("Printing Response Header...\n");

System.out.println("This test is for Logged in user with userlocale present\n");



for (Map.Entry<String, List<String>> entry : map.entrySet()) {
System.out.println("Key : " + entry.getKey()
+ " ,Value : " + entry.getValue());


}

} catch (Exception e) {
e.printStackTrace();
}
}}

最佳答案

在此处研究使用 JUnit 测试用例。如果您知道如何检索 Content-Length 的值,则可以使用 AssertEquals 功能,该功能采用您想要比较的两个参数。

在 JUnit 中,您的代码将如下所示:

AssertEquals("Expected Value","Actual Value");

因此,对于您的内容长度:

AssertEquals("12227", map.get(Content-Length));

您唯一需要做的就是正确检索实际的内容长度。

关于java - 断言该值在java和testng或junit中的map中设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41306665/

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