donatj / mock-webserver

Simple mock web server in PHP for unit testing.
MIT License
131 stars 21 forks source link

Add support for multiple HTTP methods #8

Closed stevenrombauts closed 6 years ago

stevenrombauts commented 6 years ago

Purpose

This PR adds support for multiple HTTP methods (and solves the existing issue #3). It does this by adding the HTTP method as the file extensions for the response "alias path". By default it now only supports GET requests.

Example

<?php
$server = new MockWebServer;
$server->start();

$url = $server->setResponseOfPath('/foo/bar', new Response("This is our http POST response"), RequestInfo::POST);

echo "Request to $url:\n";

$context = stream_context_create(['http' => ['method'  => 'POST']]);
$content = file_get_contents($url, false, $context);

echo $content;

I also added a new methods.php example in the examples directory.

Tests

A new test is added to MockWebServer_IntegrationTest called testHttpMethods.

Misc

If there are any changes you'd like to see to this PR, just let me know. Thanks for an excellent tool!

donatj commented 6 years ago

Thank you very much for this, I really appreciate it.

I have opened a PR for myself, #10 which contains your work to get this ready to be merged into master.

I have made a small number of changes, such as making the default behaviour the same as it is currently. That is to say by default an endpoint executes regardless of method rather than strictly on GET as a. the change would be breaking and b. I think it's a better default behaviour.

I have also prefixed the constants with METHOD_ and removed the strtoupper on $method because methods may technically be in any casing, it just so happens that all registered methods are upper case - and I'd hate for that to interfere with someone's testing.

I have a little more testing and cleanup to do, but I expect to have this merged in the next day or two.

Again, I just want to thank you for the work you put into this and for adding a test. That honestly goes a long way in expediting merges.

stevenrombauts commented 6 years ago

Thanks for your reply, and for including the changes into your project! I was blindly assuming the default behavior would be GET and didn't consider it should be replying to all methods, so that change makes total sense.

If you need some more changes or testing, I'm happy to help!