Web Support

Mock HTTP exchanges for testing web applications in BoxLang CLI runtime without a web server.

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

⚙️ Configuration

Configure in your ModuleConfig.bx:

🚀 Quick Start

🔧 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

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)

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)

💡 Examples

Basic GET Request

POST with JSON

Request with Authentication

Form Submission

🔗 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

Multiple Requests

Response Inspection

📚 Resources

Last updated

Was this helpful?