【问题标题】:Assertion that value is set in map in java and testng or junit断言该值是在 java 和 testng 或 junit 中的 map 中设置的
【发布时间】:2016-12-23 19:03:45
【问题描述】:

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

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

响应标头之一的示例:如果“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();
    }
  }}

【问题讨论】:

  • 你有什么问题?
  • 我现在添加了一个“?”对我上面的句子。请看一下。
  • 可能类似于 assertEquals("12227",map.get("Content-Length"))?
  • 考虑使用像okhttp 这样的库,它可以让您更轻松地访问响应标头
  • 感谢 BrianPipa - 成功了!它应该是 conn.getHeaderField 而不是 map.get,我使用 map.get 得到了一些意想不到的结果,然后我尝试使用 conn.getHeaderField > 它起作用了。感谢您为我指明正确的方向!谢谢 larsgrefer - 我很快就会试一试!

标签: java unit-testing junit http-headers testng


【解决方案1】:

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

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

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-23
    • 1970-01-01
    • 2011-10-05
    • 1970-01-01
    • 2018-12-23
    • 1970-01-01
    • 2013-10-19
    • 2013-01-25
    相关资源
    最近更新 更多