Backend Development 6 min read

Handling HTTP Headers and Cookies in Automated Testing with the FunTester Framework

This article demonstrates how to handle HTTP headers and cookies within the FunTester automated testing framework, providing step-by-step explanations, video references, and a complete Java code example that shows registering users, developer login, and request execution using Apache HttpClient.

FunTester
FunTester
FunTester
Handling HTTP Headers and Cookies in Automated Testing with the FunTester Framework

The author wraps up a basic tutorial on making HTTP requests, emphasizing that mastering API testing requires writing and practicing code, and encourages readers to apply the concepts in their own projects.

A series of video tutorials is linked, covering the FunTester framework introduction, obtaining HTTP request objects, sending requests and parsing responses, basic JSON operations, and practical GET and POST request demonstrations.

The article explains how to process header and cookie data, noting that in automated testing projects these are typically handled by the testing framework, and the presented demo is suited for relatively independent testing tasks.

Source code for the demo is hosted at the Gitee repository: https://gitee.com/fanapi/tester.

package com.fun;

import com.alibaba.fastjson.JSONObject;
import com.fun.config.HttpClientConstant;
import com.fun.frame.httpclient.FanLibrary;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.methods.HttpPost;

public class AR extends FanLibrary {
    public static String APIKEY;

    public static void main(String[] args) {
        printHeader();
        developerLogin();
        //        registerUser();

        testOver();
    }

    public static void registerUser() {
        if (StringUtils.isEmpty(APIKEY)) developerLogin();
        String url = "https://api.apiopen.top/registerUser";
        JSONObject param = new JSONObject();
        param.put("apikey", APIKEY);
        param.put("name", "FunTester0021");
        param.put("passwd", "123456");
        param.put("nikeName", "FunTester");
        param.put("headerImg", "http://pic.automancloud.com/sick-jvm-heap-1.png");
        param.put("phone", "13100001111");
        param.put("email", "[email protected]");
        param.put("vipGrade", "3");
        param.put("autograph", "abc");
        param.put("remarks", "这是测试用户!");
        HttpPost httpPost = getHttpPost(url, param);
        JSONObject response = getHttpResponse(httpPost);
        output(response);
    }

    public static void register() {
        String url = "https://api.apiopen.top/developerRegister";
        JSONObject param = new JSONObject();
        param.put("name", "FunTester");
        param.put("passwd", "FunTester");
        param.put("email", "[email protected]");
        HttpPost httpPost = getHttpPost(url, param);
        JSONObject response = getHttpResponse(httpPost);
        output(response);
    }

    public static void developerLogin() {
        String url = "https://api.apiopen.top/developerLogin";
        JSONObject params = new JSONObject();
        params.put("name", "funtester");
        params.put("passwd", "funtester");
        HttpPost httpPost = getHttpPost(url, params);
        httpPost.addHeader(HttpClientConstant.X_Requested_KWith);
        httpPost.addHeader(getHeader("name", "FunTester"));
        //        JSONObject cookie = new JSONObject();
        //        cookie.put("name", "FunTester");
        //        cookie.put("pwd", "FunTester");
        //        cookie.put("age", "22");
        //        Header cookies = getCookies(cookie);
        String dd = "_zap=3f36e41c-ea6e-4436-892e-3509be0a60be; _xsrf=0LCDSGzBzIxS6kWPEmf94J8KtpfmFti1; ISSW=1; d_c0=\"AADZkMsmABGPTqeAzeeGiltFGWN_rrYKq6s=|1584847946\"; _ga=GA1.2.910125274.1584847946; _gid=GA1.2.455967142.1584847946; capsion_ticket=\"2|1:0|10:1584847947|14:capsion_ticket|44:M2IwNmRhNmQ1MGViNGQ3Y2E4MDU0OTYwZjQ5ZDU5NDA=|33ef00769fdd6874a735867c43b2a0d0265c1ec8f9e106c2eb0d4805c64e4c8f\"; z_c0=\"2|1:0|10:1584847951|4:z_c0|92:Mi4xbDBITUFRQUFBQUFBQU5tUXl5WUFFU1lBQUFCZ0FsVk5UeXBrWHdCbFMxOGNwajI0QXgzMldCYTNqaGd3NmpkTlh3|915961266a0495f8ad137d24f81a0fe1020b8712019d6d8d355e4f9d65159868\"; tshl=; tst=r; q_c1=f36a39fa82744e4992cd32c16194783d|1584879682000|1584879682000; Hm_lvt_98beee57fd2ef70ccdd5ca52b9740c49=1584847946,1584945282; _gat_gtag_UA_149949619_1=1; Hm_lpvt_98beee57fd2ef70ccdd5ca52b9740c49=1584953227; KLBRSID=9d75f80756f65c61b0a50d80b4ca9b13|1584953228|1584953191";
        //        httpPost.addHeader(cookies);
        httpPost.addHeader(HttpClientConstant.COOKIE, dd);
        JSONObject response = getHttpResponse(httpPost);
        output(response);
        if (response.getIntValue("code") == 200) {
            APIKEY = response.getJSONObject("result").getString("apikey");
        } else {
            fail();
        }
    }
}

A disclaimer states that the article was originally published on the FunTester public account and may not be republished by third parties except Tencent Cloud.

The page also lists a collection of curated technical articles covering topics such as Linux performance monitoring, performance testing frameworks, HTTP mind maps, and best practices for automation testing.

JavaAutomationtestinghttpcookiesHeaders
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.