https://github.com/natlibfi/fixugen-http-server-js
Generate unit tests for HTTP servers with fixugen
https://github.com/natlibfi/fixugen-http-server-js
Last synced: about 1 year ago
JSON representation
Generate unit tests for HTTP servers with fixugen
- Host: GitHub
- URL: https://github.com/natlibfi/fixugen-http-server-js
- Owner: NatLibFi
- License: mit
- Created: 2020-06-22T11:26:23.000Z (about 6 years ago)
- Default Branch: main
- Last Pushed: 2025-06-26T09:16:00.000Z (about 1 year ago)
- Last Synced: 2025-06-26T10:20:45.456Z (about 1 year ago)
- Language: JavaScript
- Size: 1.16 MB
- Stars: 0
- Watchers: 6
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# Generate unit tests for HTTP servers with fixugen
[](https://npmjs.org/package/@natlibfi/fixugen-http-server) []()
Generates unit tests with fixugen and [chai-http](). Starts a HTTP server which is tested with generic HTTP request and response expectations.
Uses [fixugen's](https://www.npmjs.com/package/@natlibfi/fixugen) **useMetadataFile** so your fixture directories must contain **metadata.json** file.
# Usage
```js
import generateTests from '@natlibfi/fixugen-http-server';
import startApp from './app';
generateTests({
callback: () => startApp(),
path: [__dirname, '..', '..', 'test-fixtures', 'app']
});
```
# Configuration
An array property **requests** must be present in **metadata.json** file. It supports the following properties:
- **status**: HTTP status code (Number). **Mandatory**.
- **method**: HTTP method in lowercase. **Mandatory**.
- **path**: URL path. Defaults to `/`
- **requestHeaders**: An object representing requests headers.
- **responseHeaders**: An object representing response headers.
This configuration is also passed to the callback as the property **requests**.
# Request and response payloads
The fixture directory for each unit test can have request- and response payload fixtures which must match the following filename pattern:
`/^request[0-9]+`
`/^response[0-9]+`
# Formatting the response
Pass a callback to the exported function to format response headers and payload:
```js
generateTests({
formatResponse,
callback: () => startApp(),
path: [__dirname, '..', '..', 'test-fixtures', 'app']
});
function formatResponse(headers, payload) {
const newHeaders = doSomethingWithHeaders();
return { payload, headers: newHeaders };
}
```
Where `[0-9]+` denotes the index number of the fixture (Requests and responses are mocked in that order).
## License and copyright
Copyright (c) 2020, 2022-2025 **University Of Helsinki (The National Library Of Finland)**
This project's source code is licensed under the terms of **MIT** or any later version.