Class JavaScriptTemplateEngine

java.lang.Object
org.mockserver.templates.engine.javascript.JavaScriptTemplateEngine
All Implemented Interfaces:
TemplateEngine

public class JavaScriptTemplateEngine extends Object implements TemplateEngine
Author:
jamesdbloom
  • Constructor Details

  • Method Details

    • isPolyglotAvailable

      public static boolean isPolyglotAvailable()
    • executeTemplate

      public <T> T executeTemplate(String template, HttpRequest request, Class<? extends DTO<T>> dtoClass)
      Specified by:
      executeTemplate in interface TemplateEngine
    • executeTemplate

      public <T> T executeTemplate(String template, HttpRequest request, HttpResponse response, Class<? extends DTO<T>> dtoClass)
      Specified by:
      executeTemplate in interface TemplateEngine
    • executeTemplate

      public <T> T executeTemplate(String template, HttpRequest request, IterationContext iteration, Class<? extends DTO<T>> dtoClass)
      Load-generation only: execute a JavaScript template with a per-iteration variable (iteration) bound in the script scope. Used by the load executor so a JavaScript load-scenario step can vary its output per iteration. Identical to executeTemplate(String, HttpRequest, Class) when iteration is null.
    • renderTemplate

      public String renderTemplate(String template, HttpRequest request)
      Description copied from interface: TemplateEngine
      Renders a template against the request and returns the raw rendered text, without attempting to deserialize it into an HttpResponse / HttpRequest. Used to template the contents of a FileBody so an externally stored response body can contain template placeholders. Supported by the text-based engines (Velocity, Mustache).
      Specified by:
      renderTemplate in interface TemplateEngine
    • renderTemplateText

      public String renderTemplateText(String template, HttpRequest request)
      Description copied from interface: TemplateEngine
      Renders a template against the request and returns the raw rendered text fragment, for use as a streaming payload (an SSE event's data, a WebSocket text frame, or a gRPC stream message). Unlike TemplateEngine.renderTemplate(String, HttpRequest) — which the text-based engines (Velocity, Mustache) support but the JavaScript engine cannot, because a JavaScript template constructs a whole response object — this method is supported by all three engines: the JavaScript engine executes the template's handle(request) function and coerces its return value to text (a returned string is used verbatim; any other value is JSON.stringify'd). The default delegates to TemplateEngine.renderTemplate(String, HttpRequest) so the text-based engines need no override.
      Specified by:
      renderTemplateText in interface TemplateEngine
      Parameters:
      template - the template text
      request - the request context
      Returns:
      the rendered text fragment