This repository contains a baseline source for managing multiple mockservers in a project.
-
The repository should be cloned:
git clone https://github.com/onurakkaya/mockserver_ts.git cd mockserver_ts
-
Dependencies should be installed:
yarn install
To start the mock server, the following command should be run:
yarn start
The MockManager server will be started on the default port 4040
. The available methods and MockManager server health can be browsed at http://localhost:4000.
To debug the server, the built-in TSX debugger via VS Code can be used. A breakpoint should be added to the line which needs to be debugged, then the project should be started from VS Code (F5).
Note: To use the TSX debugger, the tsx node package must be installed globally on the computer.
npm install -g tsx
To run the tests, the following command should be used:
yarn test
The tests are configured to be run with Jest, and a coverage report will be generated in the coverage
directory.
This concept basically consists of two sections.
All MockServers must be inherited from this class. The main functionality of a Mock Server is contained within the class.
Some basic information can be set or retrieved for the MockServer using the available functions listed below.
- setDefaultPort(port)
- getDefaultPort() --> returns port number
- resetDefaultPort() --> default port is set to null
- getServerName() --> returns server name
- getServerInstance() --> returns fastify server object
- getServerStatus() --> returns 1 if the server is started, 0 if the server is stopped.
- getServerStatusString() --> returns "Started" or "Stopped" as a string
- getRoutes() --> returns a list of defined custom routes as a ServerRoute array (method, path, handler)
- The serverName can only be set in the constructor. If MockServer is extended, it can be set by calling the super() function, e.g., super("serverName").
MockServer has its own predefined default route "/", which shows all available routes registered to MockServers.
Custom routes can be registered via the registerRouteIfNotExists function. The function takes method (get/post/put/delete), path: string, and handler: function parameters.
Note: The path is case-sensitive.
MockServer can be started and stopped by predefined StartServer and StopServer functions. StartServer also has an optional customPort parameter.
MockManager, which is also extended from MockServer, manages all MockServers registered to itself.
This is extended from Mock Server.
To have your service managed by MockManager, your mock server should be registered with the Mock Manager using the registerMockServer(mockServer)
function.
Multiple MockServers can be started with this request. The body should be in "MockServerRequest" type. A sample body is shown below:
POST
{
"mockServers": [
{
"mockServer": "UserService",
"customPort": null
},
{
"mockServer": "ProductService",
"customPort": null
}
]
}
Multiple MockServers can be stopped with this request. The body should be in "MockServerRequest" type. A sample body is shown below:
POST
{
"mockServers": [
{
"mockServer": "UserService",
"customPort": null
},
{
"mockServer": "ProductService",
"customPort": null
}
]
}
Server health and status information can be retrieved with this request for a specified mock server. Usage: GET /getServerInfoByName?serverName=#ServerName#
All registered mock servers can be started with this command. Usage: GET /startAll
All registered mock servers can be stopped with this command. Usage: GET /stopAll
Server health and status information for all registered mock servers can be retrieved with this command. Usage: GET /getServerInfo
Some example mock server implementations are provided.
Basic create, read, and delete commands are included in the mock server. The server is registered in index.ts
by default.
The UserServiceMockServer
will be available on port 4001
by default. It provides the following endpoints:
GET /users
: Retrieve all users.GET /users/:id
: Retrieve a user by ID.POST /users
: Create a new user.DELETE /users/:id
: Delete a user by ID.
Basic create, read, and delete commands, as well as an Authorization header check, are included in the mock server. The server is registered in index.ts
by default.
The ProductServiceMockServer
will be available on port 4002
by default. It provides the following endpoints:
GET /products
: Retrieve all products.GET /products/:id
: Retrieve a product by ID.POST /products
: Create a new product.DELETE /products/:id
: Delete a product by ID.
Contributions are welcome! Please follow these steps:
- The repository should be forked: Click the "Fork" button at the top right of the repository page.
- Your fork should be cloned:
git clone https://github.com/onurakkaya/mockserver_ts.git cd mockserver_ts
- A branch should be created:
git checkout -b your-branch-name
- Your changes should be made: Implement your feature, bug fix, or documentation update.
- Your changes should be committed:
git commit -m "Description of your changes"
- Your fork should be pushed:
git push origin your-branch-name
- A pull request should be created: Go to the original repository and click "New Pull Request".
This project is licensed under the GNU General Public License v3.0. See the LICENSE file for details.