BoxLang ๐ A New JVM Dynamic Language Learn More...
Mock web server for testing BoxLang web applications in CLI runtime. Simulates HTTP requests/responses without needing an actual web server.
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.
# OS Quick Installer
install-bx-module bx-web-support
# CommandBox
box install bx-web-support
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
};
// 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
Creates or retrieves cached mock server exchange.
Arguments:
webroot (string) - Web root pathhost (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 );
Creates mock request builder for fluent configuration.
Arguments:
method (string) - HTTP method (default: GET)path (string) - Request path (default: /)body (string) - Request bodycontentType (string) - Content type (default: text/html)headers (struct) - Request headersurlScope (struct) - URL parametersformScope (struct) - Form fieldscookieScope (struct) - Cookieswebroot, 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();
Executes full mock request with all-in-one configuration.
Arguments:
path, method, pathInfo,
queryString, contentType,
body, urlScope, formScope,
cookieScope, headers
responseStatus, responseContentType,
responseBody, responseHeaders
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}'
);
mockRequestRun( path: "/api/users" );
httpData = getHTTPRequestData();
println( httpData.method ); // GET
mockRequestNew(
method: "POST",
path: "/api/users",
contentType: "application/json",
body: '{"name": "John", "email": "[email protected]"}'
).execute();
mockServer = mockServerGet()
.setRequestMethod( "GET" )
.setRequestPath( "/api/protected" )
.addRequestHeader( "Authorization", "Bearer xyz789" )
.addRequestCookie( "sessionId", "sess-123" );
mockRequestRun(
path: "/contact",
method: "POST",
formScope: {
name: "Jane Doe",
email: "[email protected]",
message: "Hello!"
}
);
MockHTTPExchange provides fluent methods for request configuration:
Request Configuration:
setRequestMethod(method) - Set HTTP methodsetRequestPath(path) - Set request pathsetRequestBody(body) - Set request bodysetRequestBodyJSON(struct) - Set JSON body (auto content-type)setRequestBodyXML(string) - Set XML body (auto content-type)setRequestContentType(type) - Set content typeHeaders & Parameters:
addRequestHeader(name, value) - Add single headeraddRequestHeaders(struct) - Add multiple headersaddURLParam(name, value) - Add URL parameteraddURLParams(struct) - Add multiple URL parametersaddFormField(name, value) - Add form fieldaddFormFields(struct) - Add multiple form fieldsaddRequestCookie(name, value) - Add cookieExecution & Inspection:
execute() - Execute the requestgetResponseBody() - Get response bodygetResponseStatus() - Get status codegetMockRequestHeaders() - Get request headersgetMockResponseHeaders() - Get response headersgetMockForm() - Get form scopegetMockURL() - Get URL scopeTest Utilities:
clearRequestData() - Reset request dataclearResponseData() - Reset response dataclearAll() - Reset everything// Reset between tests
mockServer = mockServerGet();
mockServer.clearAll();
// Or force new instance
mockServer = mockServerGet( force: true );
// 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();
mockServer = mockRequestRun(
path: "/api/test",
method: "POST",
body: '{"test": true}'
);
// Inspect
status = mockServer.getResponseStatus();
body = mockServer.getResponseBody();
headers = mockServer.getMockResponseHeaders();
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.
mockRequestNew()
$
box install bx-web-support