-
Notifications
You must be signed in to change notification settings - Fork 27
Getting Started with HTTP Mocking
Johannes Fischer edited this page Dec 18, 2024
·
3 revisions
RFLIB's HTTP Mocking Framework provides a powerful and flexible way to simulate HTTP responses in Salesforce, allowing developers to test integrations without actual external systems. This is especially beneficial in isolated development and training environments or when the actual endpoint is under development.
- Isolated Testing: Enables testing in environments without connectivity to external systems.
- Parallel Development: Facilitates development and testing when the actual endpoint is still being developed. Mocks can use agreed-upon protocols and sample payloads, making the transition to live integration seamless once mocks are disabled.
To enable HTTP mocking:
- Navigate to RFLIB Global Settings in Salesforce Setup.
- Set
Http_Mocking_Enabled
toTRUE
to activate the mocking framework. - Additional settings include:
-
Http_Mocking_Allow_In_Production
: Allows mocking in production environments when needed. -
Http_Mocking_Throw_Error_If_Mock_Not_Found
: Configures the system to throw an error if no matching mock endpoint is found.
-
See the Global Config Settings page for more details.
To configure a mock endpoint, create a new record in the RFLIB HTTP Mock Endpoint (rflib_HTTP_Mock_Endpoint__mdt
) metadata type with the following fields:
- HTTP_Method__c: The HTTP method (GET, POST, etc.)
- URL_Pattern__c: Regex pattern to match the request URL.
- Payload_Pattern__c: Regex pattern to match the request payload.
- Response_Status__c: The HTTP status code to return.
- Response_Payload__c: The payload to return in the response.
- Is_Active__c: Flag to enable or disable the mock endpoint.
Here is a sample configuration:
// Create an instance of rflib_HttpRequest
rflib_HttpRequest request = new rflib_HttpRequest();
request.setEndpoint('https://api.example.com/data');
request.setMethod('POST');
request.setBody('{"query": "info"}');
// Send the request - the response will be mocked if mocking is enabled
HttpResponse response = request.send();
// Logging and error handling
System.debug('Status: ' + response.getStatusCode());
System.debug('Body: ' + response.getBody());
// If mocking is enabled and configured correctly, this will output mocked response details.
// If no mock matches and 'Http_Mocking_Throw_Error_If_Mock_Not_Found' is TRUE, an error is thrown.