Moco API Mock Framework – Configuring Response Handlers (Intermediate)
This article explains how to configure response handlers in the Moco API mock framework, covering response types (string, JSON, object) and various strategies such as fixed, random, cyclic, and limited responses, and provides a comprehensive Java class with static helper methods for implementing these behaviors.
In the previous part we introduced the Moco API mock framework; this article focuses on configuring responses for matched requests. A request is matched against the first applicable rule, and the response can be a string , JSONObject , or generic object . The framework offers fixed, random, cyclic, and limited response strategies.
The following Java class MocoResponse extends MocoRequest and provides static helper methods that return ResponseHandler instances for different scenarios, such as text, JSON, object responses, success/failure wrappers, random, cyclic, sequential, cookie/header settings, status codes, proxies, delays, and rate limiting.
package com.fun.moco;
import com.fun.base.bean.Result;
import com.fun.moco.support.CycleHandle;
import com.fun.moco.support.LimitHandle;
import com.fun.moco.support.RandomHandler;
import com.github.dreamhead.moco.ResponseHandler;
import com.github.dreamhead.moco.procedure.LatencyProcedure;
import com.google.common.collect.FluentIterable;
import com.alibaba.fastjson.JSONObject;
import java.util.concurrent.TimeUnit;
import static com.github.dreamhead.moco.Moco.*;
import static com.github.dreamhead.moco.internal.ApiUtils.textToResource;
import static com.github.dreamhead.moco.util.Iterables.asIterable;
/** responsehandle获取 */
@SuppressWarnings("all")
class MocoResponse extends MocoRequest {
/** 返回文本信息 */
static ResponseHandler textRes(String content) {
with content
}
/** 设置json格式的返回值 */
static ResponseHandler jsonRes(JSONObject json) {
with json.toString()
}
/** 返回对象 */
static ResponseHandler obRes(Result result) {
with result.toString()
}
static ResponseHandler success(Object result) {
with Result.success(result).toString()
}
static ResponseHandler fail(Object result) {
with Result.fail(result).toString()
}
/** 随机response */
static ResponseHandler randomRes(ResponseHandler handler, ResponseHandler... handlers) {
random handler, handlers
}
/** 随机返回文本 */
static ResponseHandler cycleRes(String content, String... contents) {
cycle content, contents
}
/** 随机返回文本 */
static ResponseHandler cycleRes(JSONObject content, JSONObject... contents) {
cycle content.toString(), (String[]) contents.toList().stream().map{x -> x.toString()}.toArray()
}
/** 随机response */
static ResponseHandler cycleRes(ResponseHandler handler, ResponseHandler... handlers) {
cycle handler, handlers
}
/** 随机返回文本,会停留在最后一个文本内容 */
static ResponseHandler sequenceRes(String content, String... contents) {
seq content, contents
}
/** 随机返回文本,会停留在最后一个文本内容 */
static ResponseHandler sequenceRes(JSONObject content, JSONObject... contents) {
seq content.toString(), (String[]) contents.toList().stream().map{x -> x.toString()}.toArray()
}
/** 随机返回,最后会停留在最后一个handle */
static ResponseHandler sequenceRes(ResponseHandler handler, ResponseHandler... handlers) {
seq handler, handlers
}
/** 设置cookie,只支持一个cookie设置,因为header不允许相同的key重复 */
static ResponseHandler setCookie(String key, String value) {
cookie key, value
}
/** 设置header */
static ResponseHandler setHeader(String key, String value) {
header key, value
}
/** 批量设置header */
static ResponseHandler[] setHeader(JSONObject json) {
json.keySet().stream().map{x -> setHeader(x.toString(), json.getString(x))}.toArray() as ResponseHandler[]
}
/** 设置HTTP响应码,默认200 */
static ResponseHandler setStatus(int code) {
status code
}
/** 代理地址 */
static ResponseHandler setProxy(String url) {
proxy url
}
/** 延迟 */
static LatencyProcedure delay(long duration) {
latency duration, TimeUnit.MILLISECONDS
}
/** 随机 */
static ResponseHandler random(String content, String... contents) {
RandomHandler.newSeq(FluentIterable.from(asIterable(content, contents)).transform(textToResource()))
}
/** 随机返回 */
static ResponseHandler random(JSONObject json, JSONObject... jsons) {
RandomHandler.newSeq(FluentIterable.from(asIterable(json.toString(), jsons.toList().stream().map{x -> x.toString()}.toArray as String[])).transform(textToResource()))
}
/** 随机 */
static ResponseHandler random(ResponseHandler handler, ResponseHandler... handlers) {
RandomHandler.newSeq(asIterable(handler, handlers))
}
/** 循环返回 */
static ResponseHandler cycle(String content, String... contents) {
CycleHandle.newSeq(FluentIterable.from(asIterable(content, contents)).transform(textToResource()))
}
/** 循环返回 */
static ResponseHandler cycle(JSONObject json, JSONObject... jsons) {
CycleHandle.newSeq(FluentIterable.from(asIterable(json.toString(), jsons.toList().stream().map{x -> x.toString()}.toArray as String[])).transform(textToResource()))
}
/** 循环返回 */
static ResponseHandler cycle(ResponseHandler handler, ResponseHandler... handlers) {
CycleHandle.newSeq(asIterable(handler, handlers))
}
/** 限制访问频率,默认访问间隔1000ms */
static ResponseHandler limit(String unlimited, String limited) {
limit textRes(limited), textRes(limited)
}
static ResponseHandler limit(JSONObject unlimited, JSONObject limited) {
limit jsonResponse(limited), jsonResponse(limited)
}
static ResponseHandler limit(ResponseHandler unlimited, ResponseHandler limited) {
limit unlimited, limited, 1000
}
/** 限制访问频率 */
static ResponseHandler limit(String unlimited, String limited, int interval) {
limit textRes(unlimited), textRes(limited), interval
}
static ResponseHandler limit(JSONObject unlimited, JSONObject limited, int interval) {
limit unlimited.toString(), limited.toString(), interval
}
static ResponseHandler limit(ResponseHandler unlimited, ResponseHandler limited, int interval) {
LimitHandle.newSeq(unlimited, limited, interval)
}
}FunTester
10k followers, 1k articles | completely useless
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.