Behave
The easiest and quickest way to mock HTTP endpoints for development and testing purposes
Quick Start » Guide » API »
What is Behave?
Behave is an HTTP API mock server that can aid in rapid application development by mocking endpoints and configuring responses from configurations.
Behave can also act as a testing server to validate what requests made by system under test.
Quick Start
npx @sayjava/behave -b '[{"request": {"path":"/todo/[0-9]+"}, "response": {"body": "a simple todo"}}]'
and test the endpoint
curl -X GET http://localhost:8080/todo/10
Features
- Declarative request matching and response
- Regex based request matching (request path, headers and body matchers)
- Alternate and limit responses on same request
-
Templated based response, e.g
"Task is {{req.queryParams.name}}"
- NodeJS HTTP/Express Middleware
- API Spec 3.0 & Swagger 2.0 complaint
- HTTP request validations
- Simulate response delays and network failures
- Serverless compatible
Examples
Here are some scenarios where Behave
can be used to mock endpoints. Start the server on the default 8080 port
npx @sayjava/behave
Regex paths
npx @sayjava/behave -b '[
{
"name": "Match any task with id",
"request": { "path": "/tasks/[0-9]+" },
"response": {
"body": "found it"
}
}
]
'
to match requests like these:
curl http://localhost:8080/tasks/2
curl http://localhost:8080/tasks/10
Request headers
e.g {"user-a∫gent": "Chrome|Apple*"}
npx @sayjava/behave -b '[
{
"name": "Match requests coming from Apple devices or Chrome",
"request": {
"path": "/tasks/[0-9]+",
"headers": {
"user-agent": "Chrome|Apple*"
}
},
"response": {
"body": "Found on a mac or chrome"
}
}
]
'
to match requests like:
curl http://localhost:8080/tasks/2 -H 'user-agent: Chrome'
Templated response
HTTP response can be templated using Handlebars syntax
@sayava/behave -b `[
{
"request": {
"path": "/greet/:name",
"pathParams": {
"name": "[a-z]+"
}
},
"response": {
"body": "Hello {{req.pathParams.name}}"
}
}
]`
to match requests and respond with the template:
curl http://localhost:8080/greet/jane
and respond with Hello jane
Request body matchers
e.g {"user":"john_[a-z]+"}
@sayjava/behave -b '[{
"name": "Match requests with users with names like john_xxx",
"request": {
"path": "/tasks",
"method": "POST",
"body": {
"user": "john_[a-z]+"
}
},
"response": {
"statusCode": "201",
"body": "Task created by {{req.body.user}}"
}
}]
'
to match requests like:
curl -X POST http://localhost:8080/tasks -H "content-type:application/json" -d '{ "user": "john_doe", "name": "pay up" }'
Asserts received requests
Asserts that Behave has received a request with that path at least twice and at most 10 times
curl -X PUT http://localhost:8080/_/api/requests/assert -H "content-type:application/json" -d '[
{
"request": {
"path": "/todo/2",
"count": {
"atLeast": 2,
"atMost": 10
}
}
}
]'
Asserts the sequence requests are received
Asserts that Behave has received a request with that path at least twice and at most 10 times
curl -X PUT http://localhost:8080/_/api/requests/sequence -H "content-type:application/json" -d '[
{
"request": {
"path": "/todo/2"
}
},
{
"request": {
"path": "/todosdss/20"
}
}
]'
see the Behavior Guide
Programmatically Use cases (Express Middleware / NodeJS HTTP Middleware)
const express = require('express');
const { behaveHandler } = require('@sayjava/behave');
const app = express();
app.use(express.static(__dirname + '/views'));
// Existing route
app.get('/', (req, res) => res.render('index', { title: 'Hey', message: 'Hello there!' }));
// Mount the middleware on /api.
app.use('/api', behaveHandler({ config: { fromFile: 'api.json' } }));
app.listen(3000, () => console.info(`App started on 3000`));