public function MockHandlerStack::queueMockResponse in Apigee Edge 8
Queue a response that is in the catalog.
Dynamic values can be passed and will be replaced in the response.
Parameters
string|array $response_ids: The name of the response template to queue (without file extension) e.g. `get-developer` or `get_developer` @see /tests/response-templates.
Return value
$this
File
- tests/
modules/ apigee_mock_api_client/ src/ MockHandlerStack.php, line 88
Class
- MockHandlerStack
- The mock handler stack.
Namespace
Drupal\apigee_mock_api_clientCode
public function queueMockResponse($response_ids) {
$org_name = \Drupal::service('apigee_edge.sdk_connector')
->getOrganization();
if (empty($this->responses)) {
// Get the module path for this module.
$module_path = \Drupal::moduleHandler()
->getModule('apigee_mock_api_client')
->getPath();
$this->responses = Yaml::parseFile($module_path . '/response_catalog.yml')['responses'];
}
// Loop through responses and add each one.
foreach ((array) $response_ids as $index => $item) {
// The catalog id should either be the item itself or the keys if an
// associative array has been passed.
$id = !is_array($item) ? $item : $index;
// Body text can have elements replaced in it for certain values.
$context = is_array($item) ? $item : [];
$context['org_name'] = isset($context['org_name']) ? $context['org_name'] : $org_name;
// Add the default headers if headers aren't defined in the response
// catalog.
$headers = isset($this->responses[$id]['headers']) ? $this->responses[$id]['headers'] : [
'content-type' => 'application/json;charset=utf-8',
];
// Set the default status code.
$status_code = !empty($this->responses[$id]['status_code']) ? $this->responses[$id]['status_code'] : 200;
$status_code = !empty($context['status_code']) ? $context['status_code'] : $status_code;
if ($this->twig
->getLoader()
->exists($id)) {
$this
->addResponse($this->responseFactory
->generateResponse(new TwigSource($id, $context, $status_code, $headers)));
}
else {
$this
->addResponse(new Response($status_code, $headers, ''));
}
}
return $this;
}