PHPackages                             mjordan/test\_rest\_server - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. mjordan/test\_rest\_server

ActiveLibrary[API Development](/categories/api)

mjordan/test\_rest\_server
==========================

PHP Library for building REST servers for testing purposes.

09[1 issues](https://github.com/mjordan/test_rest_server/issues)PHP

Since Jan 26Pushed 8y ago1 watchersCompare

[ Source](https://github.com/mjordan/test_rest_server)[ Packagist](https://packagist.org/packages/mjordan/test_rest_server)[ RSS](/packages/mjordan-test-rest-server/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependenciesVersions (1)Used By (0)

Test REST Server [![Build Status](https://camo.githubusercontent.com/42b47f84357fbfbfc545039811e42f988d38899bb36ff81d222de240aaa66e44/68747470733a2f2f7472617669732d63692e6f72672f6d6a6f7264616e2f746573745f726573745f7365727665722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/mjordan/test_rest_server)
========================================================================================================================================================================================================================================================================================================================

[](#test-rest-server-)

Simple utility class for creating a local web server suitable for testing REST clients. It uses PHP's built-in web server to provide HTTP responses complete with status code, headers, and body. You provide the details of the expected response when you instantiate the server in your PHPUnit (or SimpleTest, etc.) test. Your client-under-test has complete access to this response. You can also add complex logic to your test server using "templates".

Requirements
------------

[](#requirements)

- PHP 5.5.0 or higher (tested on PHP 5.6, 7.0, and 7.1).
- [Composer](https://getcomposer.org)

Installation
------------

[](#installation)

1. `git https://github.com/mjordan/test_rest_server.git`
2. `cd test_rest_server`
3. `php composer.phar install` (or equivalent on your system, e.g., `./composer install`)

Or, use composer:

```
composer require mjordan/test_rest_server dev-master

```

and within a composer.json file:

```
    "require": {
        "mjordan/test_rest_server": "dev-master"
    }
```

Usage
-----

[](#usage)

To use this test server, you create an instance of `TestRestServer`, which takes four parameters:

- URI (string): A path relative to the root of the server. The URI is ignored by the default server template, but it is useful to provide one as a form of in-code documentation for your test. If you need a server that has to respond differently to different URIs, you can use a custom template that inspects the value of `$_SERVER['REQUEST_URI']` and responds accordingly.
- Response code (int): 200, 201, 401, etc.
- Headers (optional; array of strings): Any headers you want the server to include in the response.
- Body (optional; string): The content of the response body.

```
$this->server = new TestRestServer('/testing/foo', 201, array('Content-Type: text/plain'), 'Is this thing on?');
```

After you instantiate your server, you start it (using the `start()` method). At this point, HTTP clients can hit the server, which will respond with the values you passed it.

### A basic example using PHPUnit

[](#a-basic-example-using-phpunit)

```
