Backend Development 9 min read

FunTester HTTP Interface Testing Tutorial: GET, POST, Headers, Cookies, and Response Handling in Java

This tutorial walks through building HTTP interface tests with the FunTester framework in Java, covering request types (GET, POST), headers, cookies, response parsing, JSON handling, and resource cleanup, and provides code examples for each step.

FunTester
FunTester
FunTester
FunTester HTTP Interface Testing Tutorial: GET, POST, Headers, Cookies, and Response Handling in Java

Today the author continues the FunTester testing framework tutorial, focusing on the fundamentals of HTTP interface testing, including requests (GET, POST, PUT, etc.), request headers, cookies, responses, JSON handling, and resource cleanup.

The tutorial links to earlier, less organized articles and suggests reading them for implementation details.

Preparation : Create a Java class that extends com.funtester.httpclient.FunLibrary and add a main method.

package com.funtest.study;

import com.funtester.httpclient.FunLibrary;

public class FunTester extends FunLibrary {

    public static void main(String[] args) {

    }
}

Request : Use org.apache.http.client.methods.HttpRequestBase as the base class. The tutorial demonstrates creating GET and POST request objects.

GET

GET requests are simple; FunTester provides a method without parameters and another that accepts JSON parameters.

public static void main(String[] args) {
    String url = "http://localhost:12345/m/info"; // request URL
    JSONObject params = new JSONObject();
    params.put("name", "FunTester");
    params.put("uid", 123456);
    // No‑parameter GET
    HttpGet httpGet = getHttpGet(url);
    // GET with parameters
    HttpGet httpGet1 = getHttpGet(url, params);
}

POST

POST requests can carry JSON/text or form‑encoded data (x‑www‑form‑urlencoded). The tutorial shows how to create POST objects for each case.

public static void main(String[] args) {
    String url = "http://localhost:12345/m/info"; // request URL
    JSONObject params = new JSONObject();
    params.put("name", "FunTester");
    params.put("uid", 123456);
    // No‑parameter POST
    HttpPost httpPost = getHttpPost(url);
    // JSON/text body
    HttpPost httpPost1 = getHttpPost(url, params.toString());
    // x-www-form-urlencoded body
    HttpPost httpPost2 = getHttpPost(url, params);
}

Header : A helper method com.funtester.httpclient.FunLibrary#getHeader adds headers to a request. Example for a GET request:

public static void main(String[] args) {
    String url = "http://localhost:12345/m/info"; // request URL
    HttpGet httpGet = getHttpGet(url);
    // Add header
    httpGet.addHeader(getHeader("name", "FunTester"));
}

Cookie : Cookies are handled as JSON objects and converted to headers via getCookies . Example:

public static void main(String[] args) {
    String url = "http://localhost:12345/m/info"; // request URL
    JSONObject cookie = new JSONObject();
    cookie.put("name", "FunTester");
    cookie.put("uid", 123456);
    HttpGet httpGet = getHttpGet(url);
    // Add cookie header
    httpGet.addHeader(getCookies(cookie));
}

Response : The method com.funtester.httpclient.FunLibrary#getHttpResponse takes an HttpRequestBase object and returns a JSON object. It parses JSON responses directly; non‑JSON responses are wrapped into a JSON structure, and response headers (e.g., set‑cookie ) and HTTP status code are added to the result.

/**
 * Convert parsed content to a JSONObject.
 * @param content the response body
 * @return JSONObject representation
 */
private static JSONObject getJsonResponse(String content, JSONObject cookies) {
    JSONObject jsonObject = new JSONObject();
    try {
        if (StringUtils.isEmpty(content)) ParamException.fail("响应为空!");
        jsonObject = JSONObject.parseObject(content);
    } catch (JSONException e) {
        jsonObject = new JSONObject() {{
            put(RESPONSE_CONTENT, content);
            put(RESPONSE_CODE, TEST_ERROR_CODE);
        }};
        logger.warn("响应体非json格式,已经自动转换成json格式!");
    } finally {
        if (cookies != null && !cookies.isEmpty()) jsonObject.put(HttpClientConstant.COOKIE, cookies);
        return jsonObject;
    }
}

Usage example:

public static void main(String[] args) {
    String url = "http://localhost:12345/m/info"; // request URL
    HttpGet httpGet = getHttpGet(url);
    // Get response as JSON
    JSONObject response = getHttpResponse(httpGet);
}

JSON : The tutorial references video resources for basic operations on JSONObject and JSONArray .

Resource Release : After testing, resources can be released globally via the testOver method, which closes the HTTP client pools.

/**
 * End testing and close connection pools
 */
public static void testOver() {
    try {
        ClientManage.httpsClient.close();
        ClientManage.httpAsyncClient.close();
    } catch (Exception e) {
        logger.warn("连接池关闭失败!", e);
    }
}

The author invites readers to follow upcoming articles on interface practice and other FunTester tutorials.

backendJavaTestingHTTPAPIHttpClientFunTester
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.