Moco API Mock Framework Video Tutorial (Part 1): Request Matching and Response Handlers
This article introduces a video tutorial series on using the Moco API mock framework, focusing on how to configure request matching (URL, parameters, headers, cookies) and provides a comprehensive set of Java response‑handler utilities for functional and performance testing.
The author previously wrote a series about the Moco API and now records a video tutorial to demonstrate automated interface testing, especially functional and performance testing, because finding suitable public APIs and documentation is difficult.
The earlier articles covered basic Moco API usage and extensions but did not systematically document each method or provide a usage standard after encapsulation; therefore, this new series is divided into three parts to lay the groundwork for interface testing.
This episode focuses on the request‑matching capabilities of Moco, including URL matching (exact, regex, start/end), parameter matching for GET/POST, as well as header and cookie matching.
Moco API Mock Framework Video Tutorial (Part 1)
Request Code
The code examples are written in Groovy (compatible with Java) and can be used directly in Java projects.
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获取
* 这里的继承关系为了更方便调用mocorequest和mocoresponse的静态方法
*/
@SuppressWarnings("all")
class MocoResponse extends MocoRequest {
static ResponseHandler textRes(String content) { with content }
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() }
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() }
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() }
static ResponseHandler setCookie(String key, String value) { cookie key, value }
static ResponseHandler setHeader(String key, String value) { header key, value }
static ResponseHandler[] setHeader(JSONObject json) { json.keySet().stream().map {x -> setHeader(x.toString(), json.getString(x))}.toArray() as ResponseHandler[] }
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 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) }
}The article concludes with a disclaimer that the “FunTester” series is original, encourages following the author, and lists related popular articles and resources.
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.