- The Problem with End-to-End Testing
- Setting up
- 3A pattern: Arrange, Act, Assert, Cleanup
- Imposters: virtual services, a lightweight operation
- Hello imposter
- Run: [Command line]
- List imposters: [curl, web browser]
- Create imposters: [Command line], file]
- Delete the imposter: [Command line]]
- Delete all imposters
- Saving multiple Imposters in the config file
- EJS: Embedded JavaScript
- Goto Node.js downloads and download Node.js for Windows
- LTS
- Current
- Run the Node.js Installer(Windows)
- Welcome to the Node.js setup wizard
- Select Next
- End-User License Agreement (EULA)
- Check I accept the terms in the License Agreement
- Select Next
- Destination Folder
- Select Next
- Custom Setup
- Select Next
- Ready to install Node.js
- Select Install
- Note: This step requires Administrator privileges.
- If prompted, authenticate as an Administrator.
- Installing Node.js
- Let the installer run to completion.
- Completed the Node.js Setup Wizard
- Click Finish
- Welcome to the Node.js setup wizard
- Install Homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
- Install Node.js
brew install node
- Run command
node -v
- Update npm
npm install npm -g
- Install mountebank
npm install mountebank -g
- Time
- Cost
- Slow feedback loop
- Setup data test
- Execute the system under test
- Assert the expected response
- Clean the state
- Mountebank provides configure virtual services, which are called imposters.
- Each imposter represents a socket that acts as the virtual service and accepts connections from the real service you are testing.
- Spinning up and shutting down imposters is a lightweight operation.
ref: Testing Microservice with Mountebank page 17
ref: Testing Microservice with Mountebank page 26
POST /products?page=2&itemsPerPage=2 HTTP/1.1
Host: api.petstore.com
Content-Type: application/json
{
"key": "asdul7890"
}
{
"method": "POST",
"path": "/products",
"query": {
"page": 2,
"itemsPrePage": 2
},
"headers": {
"Host": "api.petstore.com",
"Content-Type": "application/json"
},
"body": "{\n \"key\": \"asdul7890\"\n}"
}
ref: Testing Microservice with Mountebank page 27
HTTP/1.1 200 OK
Date: Sun, 05 Apr 2020 10:10:10 GMT
Content-Type: application/json
{
"key": "asdul7890"
}
{
"statusCode": 200,
"headers": {
"Date": "Sun, 05 Apr 2020 10:10:10 GMT",
"Content-Type": "application/json"
},
"body": "{\n \"key\": \"asdul7890\"\n}"
}
{
"statusCode": 200,
"headers": { "Content-Type": "text/plain" },
"body": "Hello, World!"
}
mb
open web browser then go to http://localhost:2525
curl
curl http://localhost:2525/imposters
curl
curl -X POST http://localhost:2525/imposters --data '
{
"protocol": "http",
"port": 3000,
"stubs": [
{
"responses": [
{
"is": {
"statusCode": 200,
"headers": { "Content-Type": "text/plain" },
"body": "Hello, World!"
}
}
]
}
]
}'
curl
curl -X DELETE http://localhost:2525/imposters/3000
response
info: [mb:2525] DELETE /imposters/3000
info: [http:3000] Ciao for now
list imposters
curl http://localhost:2525/imposters
curl
curl -X DELETE http://localhost:2525/imposters
create file call hello-world.json
{
"protocol": "http",
"port": 3000,
"stubs": [
{
"responses": [
{
"is": {
"statusCode": 200,
"headers": { "Content-Type": "text/plain" },
"body": "Hello, World!"
}
}
]
}
]
}
mb start --configfile hello-world.json
The is response type, which is the fundamental building block for a stub.
{
"statusCode": 200,
"headers": { "Content-Type": "text/plain" },
"body": "Hello, World!"
}
{
"protocol": "http",
"port": 3001,
"name": "Default Response",
"defaultResponse": {
"statusCode": 400,
"headers": {
"Connection": "Keep-Alive",
"Content-Length": 0
}
},
"stubs": [
{
"responses": [
{
"is": { "body": "BOOM!!!" }
}
]
}
]
}
{
"protocol": "http",
"port": 3002,
"stubs": [
{
"responses": [
{
"is": { "body": "1" }
},
{
"is": { "body": "2" }
},
{
"is": { "body": "3" }
}
]
}
]
}
{
"imposters": [
{
"protocol": "http",
"port": 3000,
"stubs": [
{
"responses": [
{
"is": {
"statusCode": 200,
"headers": { "Content-Type": "text/plain" },
"body": "Hello, World!"
}
}
]
}
]
},
{
"protocol": "http",
"port": 3001,
"name": "Default Response",
"defaultResponse": {
"statusCode": 400,
"headers": {
"Connection": "Keep-Alive",
"Content-Length": 0
}
},
"stubs": [
{
"responses": [
{
"is": { "body": "BOOM!!!" }
}
]
}
]
},
{
"protocol": "http",
"port": 3002,
"stubs": [
{
"responses": [
{ "is": { "body": "1" } },
{ "is": { "body": "2" } },
{ "is": { "body": "3" } }
]
}
]
}
]
}
imposter.ejs
hello-world.json
default-response.json
cycling-through-response.json
{
"imposters": [
<%- include hello-world.json %>,
<%- include default-response.json %>,
<%- include cycling-through-response.json %>,
]
}
Mountebank uses a templating language called EJS (https://ejs.co/)
{
"port": <port>,
"protocol": "http",
"stubs": [
{
"predicates": [],
"responses": [
{
"is": {
"statusCode": <statusCode>,
"headers": {},
"body": {}
}
}
]
}
]
}
mb start --configfile hello-world.json --pidfile &
mb stop
mb restart --configfile hello-world.json --pidfile &