Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move response xml parser to helper #33991

Merged
merged 1 commit into from
Dec 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions tests/TestHelpers/HttpRequestHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\Message\ResponseInterface;
use SimpleXMLElement;
use Sabre\Xml\LibXMLException;
use Sabre\Xml\Reader;

/**
* Helper for HTTP requests
Expand Down Expand Up @@ -245,4 +247,33 @@ public static function delete(
public static function getResponseXml($response) {
return $response->xml();
}

/**
* parses the body content of $response and returns an array representing the XML
* This function returns an array with the following three elements:
* * name - The root element name.
* * value - The value for the root element.
* * attributes - An array of attributes.
*
* @param ResponseInterface $response
*
* @return array
*/
public static function parseResponseAsXml($response) {
$body = $response->getBody()->getContents();
$parsedResponse = [];
if ($body && \substr($body, 0, 1) === '<') {
try {
$reader = new Reader();
$reader->xml($body);
$parsedResponse = $reader->parse();
} catch (LibXMLException $e) {
// Sometimes the body can be a real page of HTML and text.
// So it may not be a complete ordinary piece of XML.
// The XML parse might fail with an exception message like:
// Opening and ending tag mismatch: link line 31 and head.
}
}
return $parsedResponse;
}
}
8 changes: 6 additions & 2 deletions tests/acceptance/features/bootstrap/CalDavContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ public function userRequestsCalendarUsingTheAPI($user, $calendar) {
$this->response = HttpRequestHelper::get(
$davUrl, $user, $this->featureContext->getPasswordForUser($user)
);
$this->featureContext->parseResponseIntoXml($this->response);
$this->featureContext->setResponseXml(
HttpRequestHelper::parseResponseAsXml($this->response)
);
}

/**
Expand Down Expand Up @@ -148,7 +150,9 @@ public function userHasCreatedACalendarNamed($user, $name) {
$this->featureContext->getPasswordForUser($user), null, $body
);
$this->theCalDavHttpStatusCodeShouldBe(201);
$this->featureContext->parseResponseIntoXml($this->response);
$this->featureContext->setResponseXml(
HttpRequestHelper::parseResponseAsXml($this->response)
);
}

/**
Expand Down
8 changes: 6 additions & 2 deletions tests/acceptance/features/bootstrap/CardDavContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ public function userRequestsAddressBookUsingTheAPI($user, $addressBook) {
$this->response = HttpRequestHelper::get(
$davUrl, $user, $this->featureContext->getPasswordForUser($user)
);
$this->featureContext->parseResponseIntoXml($this->response);
$this->featureContext->setResponseXml(
HttpRequestHelper::parseResponseAsXml($this->response)
);
}

/**
Expand Down Expand Up @@ -129,7 +131,9 @@ public function userHasCreatedAnAddressBookNamed($user, $addressBook) {
$headers, $body
);
$this->theCardDavHttpStatusCodeShouldBe(201);
$this->featureContext->parseResponseIntoXml($this->response);
$this->featureContext->setResponseXml(
HttpRequestHelper::parseResponseAsXml($this->response)
);
}

/**
Expand Down
61 changes: 27 additions & 34 deletions tests/acceptance/features/bootstrap/WebDav.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
use Guzzle\Http\Exception\BadResponseException;
use Sabre\DAV\Client as SClient;
use Sabre\DAV\Xml\Property\ResourceType;
use Sabre\Xml\LibXMLException;
use TestHelpers\OcsApiHelper;
use TestHelpers\SetupHelper;
use TestHelpers\UploadHelper;
Expand Down Expand Up @@ -93,6 +92,15 @@ trait WebDav {

private $chunkingToUse = null;

/**
* @param array $responseXml
*
* @return void
*/
public function setResponseXml($responseXml) {
$this->responseXml = $responseXml;
}

/**
* @param ResponseInterface[] $uploadResponses
*
Expand Down Expand Up @@ -239,33 +247,6 @@ public function getDavPath($for = null) {
return $this->getNewDavPath();
}

/**
* parses the body content of $response and sets $this->responseXml
*
* @param ResponseInterface|null $response if null $this->response will be used
*
* @return void
*/
public function parseResponseIntoXml($response = null) {
if ($response === null) {
$response = $this->response;
}
$body = $response->getBody()->getContents();
if ($body && \substr($body, 0, 1) === '<') {
try {
$reader = new Sabre\Xml\Reader();
$reader->xml($body);
$this->responseXml = $reader->parse();
} catch (LibXMLException $e) {
// Sometimes the body can be a real page of HTML and text.
// So it may not be a complete ordinary piece of XML.
// The XML parse might fail with an exception message like:
// Opening and ending tag mismatch: link line 31 and head.
$this->responseXml = [];
}
}
}

/**
* @param string $user
* @param string $method
Expand Down Expand Up @@ -460,7 +441,9 @@ public function userMovesFileUsingTheAPI(
$this->response = $this->makeDavRequest(
$user, "MOVE", $fileSource, $headers, null, "files", null, null, $stream
);
$this->parseResponseIntoXml();
$this->setResponseXml(
HttpRequestHelper::parseResponseAsXml($this->response)
);
} catch (ConnectException $e) {
}
}
Expand Down Expand Up @@ -502,7 +485,9 @@ public function userCopiesFileUsingTheAPI(
$this->response = $this->makeDavRequest(
$user, "COPY", $fileSource, $headers
);
$this->parseResponseIntoXml();
$this->setResponseXml(
HttpRequestHelper::parseResponseAsXml($this->response)
);
}

/**
Expand Down Expand Up @@ -1544,7 +1529,9 @@ public function userUploadsAFileTo($user, $source, $destination) {
$user, "PUT", $destination, [], $file
);
$this->lastUploadTime = \time();
$this->parseResponseIntoXml();
$this->setResponseXml(
HttpRequestHelper::parseResponseAsXml($this->response)
);
}

/**
Expand Down Expand Up @@ -2062,7 +2049,9 @@ public function userCreatesFolder($user, $destination) {
$this->response = $this->makeDavRequest(
$user, "MKCOL", $destination, []
);
$this->parseResponseIntoXml();
$this->setResponseXml(
HttpRequestHelper::parseResponseAsXml($this->response)
);
}

/**
Expand Down Expand Up @@ -2663,7 +2652,9 @@ public function propfindResultShouldContainNumEntries($user, $numFiles) {
//if we are using that step the second time in a scenario e.g. 'But ... should not'
//then don't parse the result again, because the result in a ResponseInterface
if (empty($this->responseXml)) {
$this->parseResponseIntoXml();
$this->setResponseXml(
HttpRequestHelper::parseResponseAsXml($this->response)
);
}
$multistatusResults = $this->responseXml["value"];
PHPUnit_Framework_Assert::assertEquals((int)$numFiles, \count($multistatusResults));
Expand Down Expand Up @@ -2704,7 +2695,9 @@ public function findEntryFromPropfindResponse($user, $entryNameToSearch = null)
//if we are using that step the second time in a scenario e.g. 'But ... should not'
//then don't parse the result again, because the result in a ResponseInterface
if (empty($this->responseXml)) {
$this->parseResponseIntoXml();
$this->setResponseXml(
HttpRequestHelper::parseResponseAsXml($this->response)
);
}
$multistatusResults = $this->responseXml["value"];
$results = [];
Expand Down