BoxLang ๐Ÿš€ A New JVM Dynamic Language Learn More...

BoxLang Web Support Module

v1.1.1+3 BoxLang Modules

description: >- Mock HTTP exchanges for testing web applications in BoxLang CLI runtime without a web server. icon: globe-pointer

๐ŸŒ BoxLang Web Support Module

Mock web server for testing BoxLang web applications in CLI runtime. Simulates HTTP requests/responses without needing an actual web server.

๐Ÿ“‹ Table of Contents

โš ๏ธ Warning

DO NOT install this module into a BoxLang web-runtime! This module is for CLI runtime testing or mocking only and will cause JAR conflicts in web server environments. Not needed for CommandBox or MiniServer.

๐Ÿ“ฆ Installation

# OS Quick Installer
install-bx-module bx-web-support

# CommandBox
box install bx-web-support

โš™๏ธ Configuration

Configure in your ModuleConfig.bx:

settings = {
    port       : 8080,                          // Mock server port
    host       : "localhost",                   // Mock server host
    webRoot    : server.java.executionPath,    // Web root path
    secure     : false,                         // Enable HTTPS
    requestKey : "bxMockServer"                 // Request scope key
};

๐Ÿš€ Quick Start

// Get mock server
mockServer = mockServerGet()
    .setRequestMethod( "POST" )
    .setRequestPath( "/api/users" )
    .addRequestHeader( "Content-Type", "application/json" )
    .setRequestBody( '{"name": "John"}' );

// Use web-aware BIFs
httpData = getHTTPRequestData();
println( httpData.method ); // POST

๐Ÿ”ง BIF Reference

mockServerGet()

Creates or retrieves cached mock server exchange.

Arguments:

  • webroot (string) - Web root path
  • host (string) - Server host (default: localhost)
  • port (numeric) - Server port (default: 8080)
  • secure (boolean) - Enable HTTPS (default: false)
  • force (boolean) - Force new instance (default: false)

Returns: MockHTTPExchange

// Get or create server
server = mockServerGet();

// Force new instance
server = mockServerGet( force: true );

// Custom configuration
server = mockServerGet( host: "example.com", port: 9090, secure: true );

mockRequestNew()

Creates mock request builder for fluent configuration.

Arguments:

  • method (string) - HTTP method (default: GET)
  • path (string) - Request path (default: /)
  • body (string) - Request body
  • contentType (string) - Content type (default: text/html)
  • headers (struct) - Request headers
  • urlScope (struct) - URL parameters
  • formScope (struct) - Form fields
  • cookieScope (struct) - Cookies
  • Plus: webroot, host, port, secure

Returns: MockHTTPExchange (builder pattern)

// Builder pattern
mockRequestNew()
    .setRequestMethod( "POST" )
    .setRequestBodyJSON( { name: "John" } )
    .addRequestHeader( "Authorization", "Bearer token" )
    .execute();

// Inline configuration
mockRequestNew(
    method: "PUT",
    path: "/api/users/123",
    body: '{"name": "Jane"}',
    headers: { "Authorization": "Bearer token" }
).execute();

mockRequestRun()

Executes full mock request with all-in-one configuration.

Arguments:

  • Request: path, method, pathInfo, queryString, contentType, body, urlScope, formScope, cookieScope, headers
  • Response: responseStatus, responseContentType, responseBody, responseHeaders
  • Server: webroot, host, port, secure, force

Returns: MockHTTPExchange (executed)

// Simple request
mockRequestRun( path: "/api/users", method: "GET" );

// Complex request
mockRequestRun(
    path: "/api/data",
    method: "POST",
    body: '{"key": "value"}',
    contentType: "application/json",
    headers: { "Authorization": "Bearer token" },
    responseStatus: 201,
    responseBody: '{"success": true}'
);

๐Ÿ’ก Examples

Basic GET Request

mockRequestRun( path: "/api/users" );
httpData = getHTTPRequestData();
println( httpData.method ); // GET

POST with JSON

mockRequestNew(
    method: "POST",
    path: "/api/users",
    contentType: "application/json",
    body: '{"name": "John", "email": "[email protected]"}'
).execute();

Request with Authentication

mockServer = mockServerGet()
    .setRequestMethod( "GET" )
    .setRequestPath( "/api/protected" )
    .addRequestHeader( "Authorization", "Bearer xyz789" )
    .addRequestCookie( "sessionId", "sess-123" );

Form Submission

mockRequestRun(
    path: "/contact",
    method: "POST",
    formScope: {
        name: "Jane Doe",
        email: "[email protected]",
        message: "Hello!"
    }
);

๐Ÿ”— Fluent API

MockHTTPExchange provides fluent methods for request configuration:

Request Configuration:

  • setRequestMethod(method) - Set HTTP method
  • setRequestPath(path) - Set request path
  • setRequestBody(body) - Set request body
  • setRequestBodyJSON(struct) - Set JSON body (auto content-type)
  • setRequestBodyXML(string) - Set XML body (auto content-type)
  • setRequestContentType(type) - Set content type

Headers & Parameters:

  • addRequestHeader(name, value) - Add single header
  • addRequestHeaders(struct) - Add multiple headers
  • addURLParam(name, value) - Add URL parameter
  • addURLParams(struct) - Add multiple URL parameters
  • addFormField(name, value) - Add form field
  • addFormFields(struct) - Add multiple form fields
  • addRequestCookie(name, value) - Add cookie

Execution & Inspection:

  • execute() - Execute the request
  • getResponseBody() - Get response body
  • getResponseStatus() - Get status code
  • getMockRequestHeaders() - Get request headers
  • getMockResponseHeaders() - Get response headers
  • getMockForm() - Get form scope
  • getMockURL() - Get URL scope

Test Utilities:

  • clearRequestData() - Reset request data
  • clearResponseData() - Reset response data
  • clearAll() - Reset everything

๐Ÿงช Testing Patterns

Test Isolation

// Reset between tests
mockServer = mockServerGet();
mockServer.clearAll();

// Or force new instance
mockServer = mockServerGet( force: true );

Multiple Requests

// Test different methods
mockRequestNew( method: "GET", path: "/api/users" ).execute();
mockRequestNew( method: "POST", path: "/api/users", body: "{}" ).execute();
mockRequestNew( method: "DELETE", path: "/api/users/123" ).execute();

Response Inspection

mockServer = mockRequestRun(
    path: "/api/test",
    method: "POST",
    body: '{"test": true}'
);

// Inspect
status = mockServer.getResponseStatus();
body = mockServer.getResponseBody();
headers = mockServer.getMockResponseHeaders();

๐Ÿ“š Resources

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.


Unreleased

1.1.1 - 2025-12-10

Fixed

  • Removed by accident the web support libraries when upgrading to 1.1.0

1.1.0 - 2025-12-05

Added

  • New BIF to get a fluent MockHTTPExchange builder: mockRequestNew()
  • Mocking of web request context with current calling context as parent so all BIFS work as expected
  • Updated all internal versions
  • Updated dependencies to latest versions
  • Updated all github actions

1.0.0 - 2025-07-13

  • First iteration of this module

$ box install bx-web-support

No collaborators yet.
     
  • {{ getFullDate("2024-09-16T21:13:19Z") }}
  • {{ getFullDate("2025-12-10T12:38:59Z") }}
  • 1,273
  • 732