2017 © Pedro Peláez
 

library httpbin

httpbin port to php: test your http call and api

image

httpbin/httpbin

httpbin port to php: test your http call and api

  • Friday, February 12, 2016
  • by sneakyBobito
  • Repository
  • 1 Watchers
  • 0 Stars
  • 3 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Project Abandoned

Because mocking requests is much more easy!, (*1)

HttpBin

Latest Stable Version Build Status Test Coverage, (*2)

That was highly inspired by the python version: https://github.com/Runscope/httpbin but I needed a tool with better integration to phpunit and test environment (and the python httpbin had its limits)., (*3)

This tools aims to test API client, it was not built to use in production (though I cant imagine such an use case I prefer to warn you), (*4)

Install

Install it through composer, (*5)

Start the server...

  • ...via command line: php -S localhost:8000 -t path/to/package/www
  • ...via php:
    use HttpBin\Server\ServerInstance;
    use HttpBin\Server\ServerStartupException;
    $server = new ServerInstance("localhost", 8080);
    try{
        $server->start();
    } catch (ServerStartupException $e) {

    }
    // Additionally you can use the server internal call method
    echo $server->call("/ping")->getBody(); // outputs "pong"

and check that http://localhost:8000/ping outputs pong, (*6)

PSR-7 compliant, (*7)

If you use PSR-7 requests and responses then you can use httpbin without starting the webserver:, (*8)


use HttpBin\Application; $application = new Application(); $psr7Response = $application->dispatch($psr7ServerRequest);

Default Routes

/post

returns a json that contains some info about the request including get parameters. Requires the method to be post, (*9)

/get

returns a json that contains some info about the request including get parameters. Requires the method to be get, (*10)

/ping

returns pong everytime, (*11)

Custom routes

You can write custom routes or override an existing one:, (*12)


use HttpBin\DefaultApplication; $application = new DefaultApplication(); $application->getRouter()->fromArray([ "path" => "/customRoute", "output" => "customOutput" ]);

Now when you call /customRoute on your server you will have customOutput as response., (*13)

You can add route independently the way you started your server. The following sections explain how to: - Add route to programatically created Application - Add route to programatically created ServerInstance - Add route to server created by command line, (*14)

Add route to application

Control the status code

The option status allows to control the status code of the response:, (*15)


use HttpBin\DefaultApplication; $application = new DefaultApplication(); $application->getRouter()->fromArray([ "path" => "/customRoute", "output" => "I'm not here", "status" => "404" ]);

Now when you call /customRoute on your server you will have I'm not here as response with status code 404, (*16)

Control the headers

The option headers allows to add some headers to the response, (*17)


use HttpBin\DefaultApplication; $application = new DefaultApplication(); $application->getRouter()->fromArray([ "path" => "/customRoute", "output" => "customOutput", "headers" => ["someHeader" => "somevalue"] ]);

Now when you call /customRoute on your server you will have customOutput as response with the header someHeader: somevalue, (*18)

Control matching http methods

Defaultly the option will match for every request method. But you can change this behaviours with the option methods. In this example the request will only match POST requests:, (*19)


use HttpBin\DefaultApplication; $application = new DefaultApplication(); $application->getRouter()->fromArray([ "path" => "/postOnly", "output" => "customOutput", "methods" => ["POST"] ]);

/postOnly will only answer to request with POST method, (*20)

Setup redirect

You can create a redirect response with the responseType set to redirect, output will contain the location:, (*21)


use HttpBin\DefaultApplication; $application = new DefaultApplication(); $application->getRouter()->fromArray([ "path" => "/redirectRoute", "output" => "/redirectTo", "responseType"=> "redirect" ]);

RedirectRoute will return a http response that redirects to /redirectTo, (*22)

Json Response

You can create a json response with the responseType set to json, (*23)


use HttpBin\DefaultApplication; $application = new DefaultApplication(); $application->getRouter()->fromArray([ "path" => "/someJson", "output" => ["key" => "value"], "responseType"=> "json" ]);

/someJson will return the jsonized output:{"key":"value"} with the http header content-Type: application/json, (*24)

Add route to server instance


use HttpBin\Server\ServerInstance; $server = new ServerInstance("localhost", 8080); $server->start(); $server->getRoutes()->addRoute("/foobar", "foo bar");

Now you can call http://localhost:8080/foobar and the will output foo bar, (*25)

Use additional route with php.ini (command line)

Be aware that this feature is already managed by the ServerInstance class and in most of case you wont need it., (*26)

When you start the server you can pass a custom ini file. This ini file allow to gain more control on the application:, (*27)

php -S localhost:8000 -t www -c myphp.ini, (*28)

php.ini example:, (*29)

; Disable default routes
httpbin.skipDefaultRoutes=true

; A json file that contains additional routes of the application
httpbin.handler=/path/to/file.json

An example of the handler file (json):, (*30)

    [
        {
            "path": "/myRoute",
            "output": "Some output",
            "methods": ["POST"]
        }
    ]

The Versions