{"library":"mockserver-client","title":"Node.js Client for MockServer","description":"The `mockserver-client` is a Node.js and browser client library designed to facilitate interaction with a running MockServer instance. It allows developers to programmatically define, update, and verify HTTP/HTTPS expectations against a MockServer. Key functionalities include setting simple or complex request/response pairs, forwarding requests, and verifying received requests. The current stable version is 5.15.0, which was last published over three years ago. While the core MockServer project remains actively developed, this specific client library exhibits a slower release cadence, suggesting it is in maintenance mode rather than active development for new features or compatibility updates. A key characteristic is that MockServer itself is a Java-based application, which needs to be running separately, often managed by `mockserver-node` or other methods. This client is a declarative mocking tool, meaning expectations are hand-written, which can lead to 'mock drift' if the real APIs evolve significantly without corresponding updates to the mock definitions.","language":"javascript","status":"maintenance","last_verified":"Sun Apr 19","install":{"commands":["npm install mockserver-client"],"cli":null},"imports":["import { mockServerClient } from 'mockserver-client';","import { HttpRequest } from 'mockserver-client';","import { HttpResponse } from 'mockserver-client';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { mockServerClient, HttpRequest, HttpResponse, Expectation } from 'mockserver-client';\n\nconst host = 'localhost';\nconst port = 1080;\nconst client = mockServerClient(host, port);\n\nasync function setupAndVerifyMocks() {\n  try {\n    // Ensure a clean slate before setting new expectations\n    await client.reset();\n    console.log('MockServer reset to clear previous expectations.');\n\n    // 1. Set up a simple expectation for a GET request\n    const simpleRequest: HttpRequest = {\n      method: 'GET',\n      path: '/api/users/123',\n    };\n    const simpleResponse: HttpResponse = {\n      statusCode: 200,\n      body: JSON.stringify({ id: 123, name: 'Jane Doe' }),\n      headers: [{ name: 'Content-Type', values: ['application/json'] }],\n    };\n    await client.mockSimpleResponse(simpleRequest.path, simpleResponse.body, simpleResponse.statusCode);\n    console.log('Simple GET expectation set for /api/users/123.');\n\n    // 2. Set up a more complex expectation for a POST request with specific body matching\n    const complexExpectation: Expectation = {\n      httpRequest: {\n        method: 'POST',\n        path: '/api/orders',\n        body: {\n          type: 'JSON',\n          json: { productId: 'P456', quantity: 2 },\n          matchType: 'STRICT',\n        },\n      },\n      httpResponse: {\n        statusCode: 201,\n        body: JSON.stringify({ orderId: 'ORD789', status: 'created' }),\n        delay: { timeUnit: 'MILLISECONDS', value: 100 },\n      },\n      times: { remainingTimes: 1 }, // This expectation will only be matched once\n    };\n    await client.mockAnyResponse(complexExpectation);\n    console.log('Complex POST expectation set for /api/orders.');\n\n    // In a real testing scenario, you would now make HTTP requests via your application\n    // which would hit the MockServer based on the configured expectations.\n\n    // Example: Verify that a specific request was made to the mock server\n    // (This step would typically follow the actual application calls).\n    await client.verify(simpleRequest, 1); // Verify /api/users/123 was called once\n    console.log('Verified that /api/users/123 was called once.');\n\n    // Clean up specific expectations\n    await client.clear({ path: '/api/users/123' });\n    await client.clear({ path: '/api/orders' });\n    console.log('Specific expectations cleared.');\n\n  } catch (error: any) {\n    console.error('An error occurred during MockServer interaction:', error);\n    if (error.code === 'ECONNREFUSED') {\n      console.error(`ERROR: MockServer is likely not running or inaccessible at ${host}:${port}.`);\n      console.error('Please ensure MockServer is started, for example, using ' +\n                    '`npm install -g mockserver-node` and then `mockserver-node -p 1080`.');\n    }\n  }\n}\n\nsetupAndVerifyMocks();","lang":"typescript","description":"Demonstrates how to initialize the MockServer client, set up both simple and complex HTTP expectations, and verify that requests were received, including error handling for connection issues.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}