Backend Development 3 min read

Custom JSON Request Extractor for Moco to Handle POST Parameters

The article describes a problem with Moco's default request extractor that fails to retrieve JSON parameters from POST requests and provides a custom Java extractor implementation that reads the request content and extracts the desired JSON field.

FunTester
FunTester
FunTester
Custom JSON Request Extractor for Moco to Handle POST Parameters

The author discovered that when using the Moco testing framework, the built‑in request.getQueries() method returns an empty result for POST requests with JSON bodies, causing parameter filtering to fail.

Original extractor implementation:

package com.github.dreamhead.moco.extractor;

import com.github.dreamhead.moco.HttpRequest;
import com.github.dreamhead.moco.HttpRequestExtractor;
import com.google.common.base.Optional;
import static com.google.common.base.Optional.fromNullable;

public class ParamRequestExtractor extends HttpRequestExtractor
{
    private final String param;
    public ParamRequestExtractor(final String param) {
        this.param = param;
    }
    @Override
    protected Optional
doExtract(final HttpRequest request) {
        String[] reference = request.getQueries().get(this.param);
        return fromNullable(reference);
    }
}

Because request.getQueries() yields no data for POST JSON payloads, the author created a custom extractor that parses the request content as JSON and returns the specified field.

Custom JSON extractor implementation:

package com.fun.moco;

import com.github.dreamhead.moco.HttpRequest;
import com.github.dreamhead.moco.HttpRequestExtractor;
import com.github.dreamhead.moco.RequestExtractor;
import com.google.common.base.Optional;
import net.sf.json.JSONObject;
import static com.github.dreamhead.moco.util.Preconditions.checkNotNullOrEmpty;
import static com.google.common.base.Optional.fromNullable;

/**
 * json数据格式参数值的获取
 */
public class JsonRequestExtractor extends HttpRequestExtractor
{
    private final String param;
    public JsonRequestExtractor(final String param) {
        this.param = param;
    }
    @Override
    protected Optional
doExtract(HttpRequest request) {
        try {
            String s = request.getContent().toString();
            String value = JSONObject.fromObject(s).getString(param);
            return fromNullable(new String[]{value});
        } catch (Exception e) {
            return fromNullable(new String[]{""});
        }
    }
    /**
     * 获取参数的value
     */
    public static RequestExtractor
queryJson(final String param) {
        return new JsonRequestExtractor(checkNotNullOrEmpty(param, "参数不能为空!"));
    }
}

This custom extractor can be used via queryJson("parameterName") to correctly obtain JSON parameters from POST requests in Moco tests.

The author invites comments and further testing.

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