{"id":15014450,"url":"https://github.com/donatj/mock-webserver","last_synced_at":"2025-05-15T11:00:22.685Z","repository":{"id":20264809,"uuid":"89284621","full_name":"donatj/mock-webserver","owner":"donatj","description":"Simple mock web server in PHP for unit testing.","archived":false,"fork":false,"pushed_at":"2025-03-20T03:31:18.000Z","size":320,"stargazers_count":134,"open_issues_count":6,"forks_count":21,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-14T16:57:59.982Z","etag":null,"topics":["mock","mockwebserver","phpunit","testing","unit-testing"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/donatj.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"custom":"https://www.paypal.me/donatj/15","ko_fi":"donatj","github":"donatj"}},"created_at":"2017-04-24T20:34:39.000Z","updated_at":"2025-03-27T00:56:32.000Z","dependencies_parsed_at":"2024-05-09T21:48:24.931Z","dependency_job_id":"1e7aec9d-d7f9-43ee-b0f7-8c2ea7247187","html_url":"https://github.com/donatj/mock-webserver","commit_stats":{"total_commits":269,"total_committers":14,"mean_commits":"19.214285714285715","dds":"0.10408921933085502","last_synced_commit":"ab9698a2fe29d28163946f6bce128feb310ad01d"},"previous_names":[],"tags_count":24,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/donatj%2Fmock-webserver","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/donatj%2Fmock-webserver/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/donatj%2Fmock-webserver/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/donatj%2Fmock-webserver/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/donatj","download_url":"https://codeload.github.com/donatj/mock-webserver/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254328384,"owners_count":22052632,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["mock","mockwebserver","phpunit","testing","unit-testing"],"created_at":"2024-09-24T19:45:39.033Z","updated_at":"2025-05-15T11:00:22.629Z","avatar_url":"https://github.com/donatj.png","language":"PHP","funding_links":["https://www.paypal.me/donatj/15","https://ko-fi.com/donatj","https://github.com/sponsors/donatj"],"categories":[],"sub_categories":[],"readme":"# Mock Web Server\n\n[![Latest Stable Version](https://poser.pugx.org/donatj/mock-webserver/version)](https://packagist.org/packages/donatj/mock-webserver)\n[![License](https://poser.pugx.org/donatj/mock-webserver/license)](https://packagist.org/packages/donatj/mock-webserver)\n[![ci.yml](https://github.com/donatj/mock-webserver/actions/workflows/ci.yml/badge.svg?)](https://github.com/donatj/mock-webserver/actions/workflows/ci.yml)\n\n\nSimple, easy to use Mock Web Server for PHP unit testing. Gets along simply with PHPUnit and other unit testing frameworks.\n\nUnit testing HTTP requests can be difficult, especially in cases where injecting a request library is difficult or not ideal. This helps greatly simplify the process.\n\nMock Web Server creates a local Web Server you can make predefined requests against.\n\n\n## Documentation\n\n[See: docs/docs.md](docs/docs.md)\n\n## Requirements\n\n- **php**: \u003e=7.1\n- **ext-sockets**: *\n- **ext-json**: *\n- **ralouphie/getallheaders**: ~2.0 || ~3.0\n\n## Installing\n\nInstall the latest version with:\n\n```bash\ncomposer require --dev 'donatj/mock-webserver'\n```\n\n## Examples\n\n### Basic Usage\n\nThe following example shows the most basic usage. If you do not define a path, the server will simply bounce a JSON body describing the request back to you.\n\n```php\n\u003c?php\n\nuse donatj\\MockWebServer\\MockWebServer;\n\nrequire __DIR__ . '/../vendor/autoload.php';\n\n$server = new MockWebServer;\n$server-\u003estart();\n\n$url = $server-\u003egetServerRoot() . '/endpoint?get=foobar';\n\necho \"Requesting: $url\\n\\n\";\necho file_get_contents($url);\n\n```\n\nOutputs:\n\n```\nRequesting: http://127.0.0.1:61874/endpoint?get=foobar\n\n{\n    \"_GET\": {\n        \"get\": \"foobar\"\n    },\n    \"_POST\": [],\n    \"_FILES\": [],\n    \"_COOKIE\": [],\n    \"HEADERS\": {\n        \"Host\": \"127.0.0.1:61874\",\n        \"Connection\": \"close\"\n    },\n    \"METHOD\": \"GET\",\n    \"INPUT\": \"\",\n    \"PARSED_INPUT\": [],\n    \"REQUEST_URI\": \"\\/endpoint?get=foobar\",\n    \"PARSED_REQUEST_URI\": {\n        \"path\": \"\\/endpoint\",\n        \"query\": \"get=foobar\"\n    }\n}\n```\n\n### Simple\n\n```php\n\u003c?php\n\nuse donatj\\MockWebServer\\MockWebServer;\nuse donatj\\MockWebServer\\Response;\n\nrequire __DIR__ . '/../vendor/autoload.php';\n\n$server = new MockWebServer;\n$server-\u003estart();\n\n// We define the server's response to requests of the /definedPath endpoint\n$url = $server-\u003esetResponseOfPath(\n\t'/definedPath',\n\tnew Response(\n\t\t'This is our http body response',\n\t\t[ 'Cache-Control' =\u003e 'no-cache' ],\n\t\t200\n\t)\n);\n\necho \"Requesting: $url\\n\\n\";\n\n$content = file_get_contents($url);\n\n// $http_response_header is a little known variable magically defined\n// in the current scope by file_get_contents with the response headers\necho implode(\"\\n\", $http_response_header) . \"\\n\\n\";\necho $content . \"\\n\";\n\n```\n\nOutputs:\n\n```\nRequesting: http://127.0.0.1:61874/definedPath\n\nHTTP/1.1 200 OK\nHost: 127.0.0.1:61874\nDate: Tue, 31 Aug 2021 19:50:15 GMT\nConnection: close\nX-Powered-By: PHP/7.3.25\nCache-Control: no-cache\nContent-type: text/html; charset=UTF-8\n\nThis is our http body response\n```\n\n### Change Default Response\n\n```php\n\u003c?php\n\nuse donatj\\MockWebServer\\MockWebServer;\nuse donatj\\MockWebServer\\Responses\\NotFoundResponse;\n\nrequire __DIR__ . '/../vendor/autoload.php';\n\n$server = new MockWebServer;\n$server-\u003estart();\n\n// The default response is donatj\\MockWebServer\\Responses\\DefaultResponse\n// which returns an HTTP 200 and a descriptive JSON payload.\n//\n// Change the default response to donatj\\MockWebServer\\Responses\\NotFoundResponse\n// to get a standard 404.\n//\n// Any other response may be specified as default as well.\n$server-\u003esetDefaultResponse(new NotFoundResponse);\n\n$content = file_get_contents($server-\u003egetServerRoot() . '/PageDoesNotExist', false, stream_context_create([\n\t'http' =\u003e [ 'ignore_errors' =\u003e true ], // allow reading 404s\n]));\n\n// $http_response_header is a little known variable magically defined\n// in the current scope by file_get_contents with the response headers\necho implode(\"\\n\", $http_response_header) . \"\\n\\n\";\necho $content . \"\\n\";\n\n```\n\nOutputs:\n\n```\nHTTP/1.1 404 Not Found\nHost: 127.0.0.1:61874\nDate: Tue, 31 Aug 2021 19:50:15 GMT\nConnection: close\nX-Powered-By: PHP/7.3.25\nContent-type: text/html; charset=UTF-8\n\nVND.DonatStudios.MockWebServer: Resource '/PageDoesNotExist' not found!\n\n```\n\n### PHPUnit\n\n```php\n\u003c?php\n\nuse donatj\\MockWebServer\\MockWebServer;\nuse donatj\\MockWebServer\\Response;\n\nclass ExampleTest extends PHPUnit\\Framework\\TestCase {\n\n\t/** @var MockWebServer */\n\tprotected static $server;\n\n\tpublic static function setUpBeforeClass() : void {\n\t\tself::$server = new MockWebServer;\n\t\tself::$server-\u003estart();\n\t}\n\n\tpublic function testGetParams() : void {\n\t\t$result  = file_get_contents(self::$server-\u003egetServerRoot() . '/autoEndpoint?foo=bar');\n\t\t$decoded = json_decode($result, true);\n\t\t$this-\u003eassertSame('bar', $decoded['_GET']['foo']);\n\t}\n\n\tpublic function testGetSetPath() : void {\n\t\t// $url = http://127.0.0.1:61874/definedEndPoint\n\t\t$url    = self::$server-\u003esetResponseOfPath('/definedEndPoint', new Response('foo bar content'));\n\t\t$result = file_get_contents($url);\n\t\t$this-\u003eassertSame('foo bar content', $result);\n\t}\n\n\tpublic static function tearDownAfterClass() : void {\n\t\t// stopping the web server during tear down allows us to reuse the port for later tests\n\t\tself::$server-\u003estop();\n\t}\n\n}\n\n```\n\n### Delayed Response Usage\n\nBy default responses will happen instantly. If you're looking to test timeouts, the DelayedResponse response wrapper may be useful.\n\n```php\n\u003c?php\n\nuse donatj\\MockWebServer\\DelayedResponse;\nuse donatj\\MockWebServer\\MockWebServer;\nuse donatj\\MockWebServer\\Response;\n\nrequire __DIR__ . '/../vendor/autoload.php';\n\n$server = new MockWebServer;\n$server-\u003estart();\n\n$response = new Response(\n\t'This is our http body response',\n\t[ 'Cache-Control' =\u003e 'no-cache' ],\n\t200\n);\n\n// Wrap the response in a DelayedResponse object, which will delay the response\n$delayedResponse = new DelayedResponse(\n\t$response,\n\t100000 // sets a delay of 100000 microseconds (.1 seconds) before returning the response\n);\n\n$realtimeUrl = $server-\u003esetResponseOfPath('/realtime', $response);\n$delayedUrl  = $server-\u003esetResponseOfPath('/delayed', $delayedResponse);\n\necho \"Requesting: $realtimeUrl\\n\\n\";\n\n// This request will run as quickly as possible\n$start = microtime(true);\nfile_get_contents($realtimeUrl);\necho \"Realtime Request took: \" . (microtime(true) - $start) . \" seconds\\n\\n\";\n\necho \"Requesting: $delayedUrl\\n\\n\";\n\n// The request will take the delayed time + the time it takes to make and transfer the request\n$start = microtime(true);\nfile_get_contents($delayedUrl);\necho \"Delayed Request took: \" . (microtime(true) - $start) . \" seconds\\n\\n\";\n\n```\n\nOutputs:\n\n```\nRequesting: http://127.0.0.1:61874/realtime\n\nRealtime Request took: 0.015669107437134 seconds\n\nRequesting: http://127.0.0.1:61874/delayed\n\nDelayed Request took: 0.10729098320007 seconds\n\n```\n\n## Multiple Responses from the Same Endpoint\n\n### Response Stack\n\nIf you need an ordered set of responses, that can be done using the ResponseStack.\n\n```php\n\u003c?php\n\nuse donatj\\MockWebServer\\MockWebServer;\nuse donatj\\MockWebServer\\Response;\nuse donatj\\MockWebServer\\ResponseStack;\n\nrequire __DIR__ . '/../vendor/autoload.php';\n\n$server = new MockWebServer;\n$server-\u003estart();\n\n// We define the servers response to requests of the /definedPath endpoint\n$url = $server-\u003esetResponseOfPath(\n\t'/definedPath',\n\tnew ResponseStack(\n\t\tnew Response(\"Response One\"),\n\t\tnew Response(\"Response Two\")\n\t)\n);\n\necho \"Requesting: $url\\n\\n\";\n\n$contentOne = file_get_contents($url);\n$contentTwo = file_get_contents($url);\n// This third request is expected to 404 which will error if errors are not ignored\n$contentThree = file_get_contents($url, false, stream_context_create([ 'http' =\u003e [ 'ignore_errors' =\u003e true ] ]));\n\n// $http_response_header is a little known variable magically defined\n// in the current scope by file_get_contents with the response headers\necho $contentOne . \"\\n\";\necho $contentTwo . \"\\n\";\necho $contentThree . \"\\n\";\n\n```\n\nOutputs:\n\n```\nRequesting: http://127.0.0.1:61874/definedPath\n\nResponse One\nResponse Two\nPast the end of the ResponseStack\n```\n\n### Response by Method\n\nIf you need to vary responses to a single endpoint by method, you can do that using the ResponseByMethod response object.\n\n```php\n\u003c?php\n\nuse donatj\\MockWebServer\\MockWebServer;\nuse donatj\\MockWebServer\\Response;\nuse donatj\\MockWebServer\\ResponseByMethod;\n\nrequire __DIR__ . '/../vendor/autoload.php';\n\n$server = new MockWebServer;\n$server-\u003estart();\n\n// Create a response for both a POST and GET request to the same URL\n\n$response = new ResponseByMethod([\n\tResponseByMethod::METHOD_GET  =\u003e new Response(\"This is our http GET response\"),\n\tResponseByMethod::METHOD_POST =\u003e new Response(\"This is our http POST response\", [], 201),\n]);\n\n$url = $server-\u003esetResponseOfPath('/foo/bar', $response);\n\nforeach( [ ResponseByMethod::METHOD_GET, ResponseByMethod::METHOD_POST ] as $method ) {\n\techo \"$method request to $url:\\n\";\n\n\t$context = stream_context_create([ 'http' =\u003e [ 'method' =\u003e $method ] ]);\n\t$content = file_get_contents($url, false, $context);\n\n\techo $content . \"\\n\\n\";\n}\n\n```\n\nOutputs:\n\n```\nGET request to http://127.0.0.1:61874/foo/bar:\nThis is our http GET response\n\nPOST request to http://127.0.0.1:61874/foo/bar:\nThis is our http POST response\n\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdonatj%2Fmock-webserver","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdonatj%2Fmock-webserver","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdonatj%2Fmock-webserver/lists"}