You are here

public function XmlRpcMessagesTest::testRequestAndResponseEncodingDefinitions in xmlrpc 8

Check XML-RPC client and server encoding information.

Ensure that XML-RPC client sets correct processing instructions for XML documents.

Ensure that XML-RPC server sets correct encoding in response http headers and processing instructions for XML documents.

File

src/Tests/XmlRpcMessagesTest.php, line 80

Class

XmlRpcMessagesTest
Tests large messages and method alterations.

Namespace

Drupal\xmlrpc\Tests

Code

public function testRequestAndResponseEncodingDefinitions() {
  $url = $this
    ->getEndpoint();
  $client = \Drupal::httpClient();

  // We can't use the xmlrpc() function here, because we have to access the
  // full Guzzle response.
  module_load_include('inc', 'xmlrpc');
  $xmlrpc_request = xmlrpc_request('system.listMethods', []);
  $headers = [
    'Content-Type' => 'text/xml; charset=utf-8',
  ];
  $request = new Request('POST', $url, $headers, $xmlrpc_request->xml);

  // These may not be initialized in some exception cases.
  $data = NULL;
  $content_type = NULL;
  try {
    $response = $client
      ->send($request);
    $data = $response
      ->getBody();
    $content_type = $response
      ->getHeader('Content-Type');
    $content_type = reset($content_type);
  } catch (RequestException $e) {
    $this
      ->fail($e
      ->getMessage(), '"Normal" exception');
  } catch (\Exception $e) {
    $this
      ->fail($e
      ->getMessage(), 'Unexpected exception');
  }

  // The request string starts with the XML processing instruction.
  $this
    ->assertIdentical(0, strpos($request
    ->getBody(), '<?xml version="1.0" encoding="utf-8" ?>'), 'Request Processing Instruction is "&lt;?xml version="1.0" encoding="utf-8" ?&gt;"');

  // The response body has to start with the xml processing instruction.
  $this
    ->assertIdentical(strpos($data, '<?xml version="1.0" encoding="utf-8" ?>'), 0, 'Response Processing Instruction is "&lt;?xml version="1.0" encoding="utf-8" ?&gt;"');
  $this
    ->assertIdentical($content_type, 'text/xml; charset=utf-8');
}