1. Go to this page and download the library: Download donatj/mock-webserver library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use donatj\MockWebServer\MockWebServer;
use donatj\MockWebServer\Response;
e to requests of the /definedPath endpoint
$url = $server->setResponseOfPath(
'/definedPath',
new Response(
'This is our http body response',
[ 'Cache-Control' => 'no-cache' ],
200
)
);
echo "Requesting: $url\n\n";
$content = file_get_contents($url);
// $http_response_header is a little known variable magically defined
// in the current scope by file_get_contents with the response headers
echo implode("\n", $http_response_header) . "\n\n";
echo $content . "\n";
use donatj\MockWebServer\MockWebServer;
use donatj\MockWebServer\Responses\NotFoundResponse;
ponses\DefaultResponse
// which returns an HTTP 200 and a descriptive JSON payload.
//
// Change the default response to donatj\MockWebServer\Responses\NotFoundResponse
// to get a standard 404.
//
// Any other response may be specified as default as well.
$server->setDefaultResponse(new NotFoundResponse);
$content = file_get_contents($server->getServerRoot() . '/PageDoesNotExist', false, stream_context_create([
'http' => [ 'ignore_errors' => true ], // allow reading 404s
]));
// $http_response_header is a little known variable magically defined
// in the current scope by file_get_contents with the response headers
echo implode("\n", $http_response_header) . "\n\n";
echo $content . "\n";
use donatj\MockWebServer\MockWebServer;
use donatj\MockWebServer\Response;
class ExampleTest extends PHPUnit\Framework\TestCase {
/** @var MockWebServer */
protected static $server;
public static function setUpBeforeClass() : void {
self::$server = new MockWebServer;
self::$server->start();
}
public function testGetParams() : void {
$result = file_get_contents(self::$server->getServerRoot() . '/autoEndpoint?foo=bar');
$decoded = json_decode($result, true);
$this->assertSame('bar', $decoded['_GET']['foo']);
}
public function testGetSetPath() : void {
// $url = http://127.0.0.1:61874/definedEndPoint
$url = self::$server->setResponseOfPath('/definedEndPoint', new Response('foo bar content'));
$result = file_get_contents($url);
$this->assertSame('foo bar content', $result);
}
public static function tearDownAfterClass() : void {
// stopping the web server during tear down allows us to reuse the port for later tests
self::$server->stop();
}
}
use donatj\MockWebServer\DelayedResponse;
use donatj\MockWebServer\MockWebServer;
use donatj\MockWebServer\Response;
rol' => 'no-cache' ],
200
);
// Wrap the response in a DelayedResponse object, which will delay the response
$delayedResponse = new DelayedResponse(
$response,
100000 // sets a delay of 100000 microseconds (.1 seconds) before returning the response
);
$realtimeUrl = $server->setResponseOfPath('/realtime', $response);
$delayedUrl = $server->setResponseOfPath('/delayed', $delayedResponse);
echo "Requesting: $realtimeUrl\n\n";
// This request will run as quickly as possible
$start = microtime(true);
file_get_contents($realtimeUrl);
echo "Realtime Request took: " . (microtime(true) - $start) . " seconds\n\n";
echo "Requesting: $delayedUrl\n\n";
// The request will take the delayed time + the time it takes to make and transfer the request
$start = microtime(true);
file_get_contents($delayedUrl);
echo "Delayed Request took: " . (microtime(true) - $start) . " seconds\n\n";
use donatj\MockWebServer\MockWebServer;
use donatj\MockWebServer\Response;
use donatj\MockWebServer\ResponseStack;
t
$url = $server->setResponseOfPath(
'/definedPath',
new ResponseStack(
new Response("Response One"),
new Response("Response Two")
)
);
echo "Requesting: $url\n\n";
$contentOne = file_get_contents($url);
$contentTwo = file_get_contents($url);
// This third request is expected to 404 which will error if errors are not ignored
$contentThree = file_get_contents($url, false, stream_context_create([ 'http' => [ 'ignore_errors' => true ] ]));
// $http_response_header is a little known variable magically defined
// in the current scope by file_get_contents with the response headers
echo $contentOne . "\n";
echo $contentTwo . "\n";
echo $contentThree . "\n";
use donatj\MockWebServer\MockWebServer;
use donatj\MockWebServer\Response;
use donatj\MockWebServer\ResponseByMethod;
nse = new ResponseByMethod([
ResponseByMethod::METHOD_GET => new Response("This is our http GET response"),
ResponseByMethod::METHOD_POST => new Response("This is our http POST response", [], 201),
]);
$url = $server->setResponseOfPath('/foo/bar', $response);
foreach( [ ResponseByMethod::METHOD_GET, ResponseByMethod::METHOD_POST ] as $method ) {
echo "$method request to $url:\n";
$context = stream_context_create([ 'http' => [ 'method' => $method ] ]);
$content = file_get_contents($url, false, $context);
echo $content . "\n\n";
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.