MODX Connector and External API: Testing without Production Credentials
0 reputation · 26 Oct 2022, 18:27 UTC
0 reputation · 26 Oct 2022, 18:27 UTC
Integrating MODX with external services typically requires the configuration of an api_key within the connector settings. While these settings allow for the definition of credentials, the current implementation does not provide a native mechanism to toggle between production and sandbox environments based on the system state.
The request connector class reads credentials directly from the configuration and executes HTTP requests regardless of whether the key is a production value or a placeholder. This creates a risk of triggering rate limits or security blocks on live endpoints during development and testing phases.
Given the behavior of the connector in MODX 3.x, what is the recommended approach for isolating API calls during testing? Is there a documented way to implement a test-mode flag within the connector config to prevent live requests, or is subclassing the connector the only viable method for injecting dummy credentials?
33275 reputation · 26 Oct 2022, 18:55 UTC
The MODX 3.x connector reads the api_key directly from its configuration and sends HTTP requests regardless of whether the key is a production value or a placeholder. No built‑in toggle for sandbox vs. production exists.
connector_test_mode (type: Yes/No) via System Settings → New Setting or in core/config/config.inc.php.modX\connectors\request or the service‑specific request class). Override the method that builds the request – usually prepare() or send(). Inside the override, check $modx->getOption('connector_test_mode'). If true, either return a canned response or use a dummy api_key (or skip the HTTP call entirely).OnManagerPageInit (or OnInit) event that replaces the service in the container:
$modx->getService('request', 'myNamespace\connectors\request', $modx->getOption('core_path') . 'connectors/request.php');Replace the class name with your own.connector_test_mode is enabled no outbound HTTP request is made.Are you performing these tests through the MODX Manager (browser) or via automated PHPUnit tests? The answer determines whether a service‑plugin approach or a PHPUnit mock handler is more appropriate.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.