weew / http-blueprint
Very basic blueprint server for API prototyping.
Requires
- weew/http: ^1.11
- weew/http-server: ^1.1
- weew/router: ^2.8
Requires (Dev)
- phpunit/phpunit: ^4.7
- satooshi/php-coveralls: ^0.6.1
README
Table of contents
Installation
composer require weew/http-blueprint
Introduction
This little package allows you to easily spin up a server, maybe during your unit tests. It consists of two parts, the server and the proxy. The server is responsible for starting and stopping of, big surprise, the http server. The proxy is basically responsible for grabbing all of the registered routes, figuring out which one should be called and returning an http response.
This package was mainly built for testing of the http layer and the http client where I could not simply mock the endpoints, but had instead to actually test the whole http communication and the resulting requests and responses. But at the end I think this is a really useful package and might be used elsewhere as well.
Creating a blueprint
Create a file that will be used as your blueprint. Register your routes in there. Let the proxy to the rest.
// file: blueprint.php // create a proxy $proxy = new BlueprintProxy(); // register all of your routes $proxy->getRouter() ->get('/', 'hello world') ->get('about', new HttpResponse(HttpStatusCode::OK, 'foo')) ->post('post', function() { return 'hello world'; }) ->put('users/{id}', function(IHttpRequest $request, array $parameters) { return new HttpResponse(HttpStatusCode::OK, $parameters['id']); }); // send a response $proxy->sendResponse();
Starting and stopping the server
It is very easy. Just pass in the hostname, your desired port, and the path to the blueprint file you've created.
$server = new BlueprintServer('localhost', 9000, '/path/to/blueprint.php'); $server->start();
When you're done, simply stop the server.
$server->stop();
Related projects
- HTTP Layer: offers response and request objects, handles cookies, headers and much more.
- HTTP Server: allows you to start an http server in a directory of your choice.
- HTTP Client: allows you to send HttpRequest and to receive HttpResponse objects.
- Router: allows you to create complex routes and map them to a response.