1.3.0 • Published 11 months ago
http-mock-json v1.3.0
HTTP MOCK JSON
Allows to create a mock server and test the frontend without depending on the backend.
Installation and use 🔧
Install library.
npm install http-mock-json --save-devRun the command.
mock-server initFollow the instructions as in the example image.
A mocks folder will be created that will contain a first endpoint.
Mock structure
Key Example Description endpoint data/animals,data/animal/:parameterAPI route httpVerb GET,POSTGET,POST,PUT,PATCH,DELETEnameResponse success,error,error-401Response name that the mock will use responses A mock can have multiple responses (array), each identified with a name.name Response name (unique) statusCode HTTP Status Codes headers Headers in json format body Response in json format Add the responses you want to simulate in the body. (You can change the mock response by changing the
nameResponse).Execute command
mock-server start
Commands ⚙️
initCreate the folder that will contain the mocks.
mock-server initFlag Default Description -p --path rootIndicates the location of the mocks in a specific folder. -m --mock trueCreate a first mock. -s --script trueAdd script to start the mock in the package.json file. Example:
mock-server init --path apps/folder1 --mock false --script falsestartStart mock server.
mock-server startFlag Default Description -p --port 3500Indicates the port where the mock will be executed -f --path rootIndicates the location of the mocks in a specific folder. Example:
mock-server start --port 3001 --path apps/folder1addCreate a mock.
mock-server addFlag Default Description -p --path rootIndicates the location of the mocks in a specific folder. Example:
mock-server add --path apps/folder1
Recommendations 📋
- Review the advanced examples.
- A single json file can contain many mocks.
- There can be many json files each with their respective mocks.
Advanced examples
{
"data/animals": {
"GET": {
"nameResponse": "AnimalsError",
"responses": [
{
"name": "AnimalsList",
"statusCode": "200",
"body": {
"example": "data"
}
},
{
"name": "AnimalsError",
"statusCode": "404",
"body": {
"example-error": "error"
}
}
]
},
"POST": {
"nameResponse": "AnimalsError",
"responses": [
{
"name": "AnimalsSave",
"statusCode": "201",
"body": {
"example": "data"
}
},
{
"name": "AnimalsError",
"statusCode": "404",
"body": {
"example-error": "error"
}
}
]
}
}
}{
"data/animals/:id": {
"GET": {
"nameResponse": "AnimalsList",
"responses": [
{
"name": "AnimalsList",
"statusCode": "200",
"body": {
"example": "data"
}
},
{
"name": "AnimalsError",
"statusCode": "404",
"body": {
"example-error": "error"
}
}
]
},
"POST": {
"nameResponse": "AnimalsSave",
"responses": [
{
"name": "AnimalsSave",
"statusCode": "201",
"body": {
"example": "data"
}
},
{
"name": "AnimalsError",
"statusCode": "404",
"body": {
"example-error": "error"
}
}
]
}
},
"data/brands": {
"GET": {
"nameResponse": "BrandsList3",
"responses": [
{
"name": "BrandsList",
"statusCode": "200",
"body": {
"example": "data1"
}
},
{
"name": "BrandsList2",
"statusCode": "200",
"body": {
"example": "data2"
}
},
{
"name": "BrandsList3",
"statusCode": "200",
"body": {
"example": "data3"
}
}
]
}
}
}License 📖
http-mock-json is MIT licensed.