1. ApiHug Tool Window
  2. Switch to Test tab
  3. Find Api
  4. Save Test
  5. Switch to wider test editor panel
  6. Test script
ApiHug Test Manager

Test Script

  1. Script tab
  2. Generate the code template
  3. Open Edit Panel(or inline edit)
  4. Prior change request or Post change response
ApiHug Test Script Manager

Test Environment

  1. Choice Environment
  2. Select Server
  3. Select Environment
ApiHug Test Environment Manager

API Script Management

  1. groovy Syntax
  2. Editor autocomplete
  3. Prior request manipulate before send, Post response manipulae

Embedded Context

Common Part:

MethodDescriptionComment
String getHeader(String key)get headerboth request/response
List<String> getHeaders(String key)get header as listboth request/response
List<String> getHeaderKeys()get header key listboth request/response

Request

MethodDescriptionComment
String getUrl()Get URL-
void setUrl(String url)Overwrite URL-
String getParameter(String name)Get ParameterIf list only first return
void setParameter(String name, String value)Set ParameterOverwrite exist
void setParameters(Map<String, String> parameters)Set Parameter Map-
void addParameter(String name, String value)Add Parameter-
void removeParameter(String name)Delete Parameter-
void setFormData(String name, String value)Set Form Data-
void setHeader(String name, String value)Set header-
void addHeader(String name, String value)Add header-
void removeHeader(String name)Remove header-
byte[] getRequestBody()Get request body-
void setRequestBody(String payload)Set request bodyString
void setRequestBody(byte[] payload)Set request bodybyte
void setRequestBody(InputStream input)Set request bodyStream
Map<String, List<String>> getParameterMap()Get all parameter-
List<String> getParameterValues(String name)Get all parameter name-
void setCookie(String cookie)Set cookie-

Response

MethodDescriptionComment
String getResponseBody()Get response bodyString
int getCode()Get http code-
String getCookie(String name)Get cookie by name-
String getCookie()Get cookieEntire cookie as String

Logger

Debug log to IDEA console:

MethodDescriptionComment
void clear()clear-
void warn(final String message)warn-
void info(final String message)info-
void debug(final String message)debug-
void error(final String message)error-

Runtime context

It can be used to update the current runtime environment parameters or variables based on the returned information.

MethodDescriptionComment
void updateGlobalVariable(String name, Object value)Overwrite global variable-
void updateGlobalParameter(String name, Object value)Overwrite global parameter-
void updateEnvVariable(String name, Object value)Overwrite current env variable-
void updateEnvParameter(String name, Object value)Overwrite current env parameter-

Sample Template

Pay attention for those tips:

  1. The dummy object is meant to assist you during the design phase. It will be automatically adjusted at runtime, so please do not touch it.
  2. // Place your logic here indicates the starting point for your code.
  3. It is necessary to perform a complete gradle build for the project. Otherwise, the context may not be accessible, and code suggestions may not work properly. This is a one-time task.
  4. Groovy syntax is similar to Java and generally interchangeable. It is also more concise, and there is no need to use semicolons(;) at the end.
  5. Use the context’s logger to output information instead of using System.out.

Prior Script

Context included:

  1. request, original request
  2. logger, logger to track the output

import hope.common.script.HTTPRequest
import hope.common.script.LoggerAdapter

/************************************************************************
 * 1. Do not touch code below, it will be injected at runtime
 * 2. Fellow the groovy syntax, Java syntax almost can work
 ************************************************************************/

HTTPRequest request = HTTPRequest.DUMMY
LoggerAdapter logger = LoggerAdapter.DUMMY

/*************************************************************************/

// Place your logic here

headerKey = request.getHeader("key");

if (headerKey != null) {
    logger.warn("we has header value $headerKey")
} else {
    logger.warn("we has no header value set")
}

//request.setHeader("key","value");

Post Script

Context included:

  1. response, response object
  2. logger, logger to track output
  3. runtime, interface to update runtime environment

import hope.common.script.HTTPResponse
import hope.common.script.LoggerAdapter
import hope.common.script.RuntimeContext

/************************************************************************
 * 1. Do not touch code below, it will be injected at runtime
 * 2. Fellow the groovy syntax, Java syntax almost can work
 ************************************************************************/

HTTPResponse response = HTTPResponse.DUMMY
LoggerAdapter logger = LoggerAdapter.DUMMY
RuntimeContext runtime = RuntimeContext.DUMMY

/*************************************************************************/

// Place your logic here

response.getHeader("key");

Reference

  1. ApiHug101-Bilibili
  2. ApiHug101-Youtube