Skip to content

Commit

Permalink
Adds getLastRequest functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
donatj committed Apr 25, 2017
1 parent 9a8a2e3 commit 2d87095
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 17 deletions.
10 changes: 10 additions & 0 deletions docs/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ Set a specified path to provide a specific response

---

### Method: `MockWebServer->getLastRequest()`

Get the previous requests associated request data.

#### Returns:

- ***array*** | ***null***

---

### Method: `MockWebServer->getHost()`

Get the host of the server.
Expand Down
37 changes: 20 additions & 17 deletions server/server.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,25 @@
$tmp = getenv(MockWebServer::TMP_ENV);
$PARSED_REQUEST_URI = parse_url($_SERVER['REQUEST_URI']);

$HEADERS = getallheaders();
$INPUT = file_get_contents("php://input");
parse_str($INPUT, $PARSED_INPUT);

$request = [
'_GET' => $_GET,
'_POST' => $_POST,
'_FILES' => $_FILES,
'_COOKIE' => $_COOKIE,
'HEADERS' => $HEADERS,
'METHOD' => $_SERVER['REQUEST_METHOD'],
'INPUT' => $INPUT,
'PARSED_INPUT' => $PARSED_INPUT,
'REQUEST_URI' => $_SERVER['REQUEST_URI'],
'PARSED_REQUEST_URI' => $PARSED_REQUEST_URI,
];

file_put_contents($tmp . DIRECTORY_SEPARATOR . 'last.request', json_encode($request));

$path = false;
$alias = 'alias.' . md5($PARSED_REQUEST_URI['path']);
if( file_exists($tmp . DIRECTORY_SEPARATOR . $alias) ) {
Expand Down Expand Up @@ -55,20 +74,4 @@
header('Content-Type: application/json');
}

$headers = getallheaders();

$INPUT = file_get_contents("php://input");
parse_str($INPUT, $PARSED_INPUT);

echo json_encode([
'_GET' => $_GET,
'_POST' => $_POST,
'_FILES' => $_FILES,
'_COOKIE' => $_COOKIE,
'HEADERS' => $headers,
'METHOD' => $_SERVER['REQUEST_METHOD'],
'INPUT' => $INPUT,
'PARSED_INPUT' => $PARSED_INPUT,
'REQUEST_URI' => $_SERVER['REQUEST_URI'],
'PARSED_REQUEST_URI' => $PARSED_REQUEST_URI,
], JSON_PRETTY_PRINT);
echo json_encode($request, JSON_PRETTY_PRINT);
18 changes: 18 additions & 0 deletions src/MockWebServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,24 @@ private function getTmpDir() {
return $tmpPath;
}

/**
* Get the previous requests associated request data.
*
* @return array|null
*/
public function getLastRequest() {
$path = $this->tmpDir . DIRECTORY_SEPARATOR . 'last.request';
if( file_exists($path) ) {
$content = file_get_contents($path);
$data = @json_decode($content, true);
if( json_last_error() === JSON_ERROR_NONE ) {
return $data;
}
}

return null;
}

/**
* Get the host of the server.
*
Expand Down

0 comments on commit 2d87095

Please sign in to comment.