kittymocklib-ts
v1.0.2
Published
That's lib is a kittymock abstraction in typescript
Downloads
2
Readme
KittyMockLib-TS
This lib is a Typescript KittyMock abstraction.The objective is to be an easier form to develop in typescript to use this mockers. For more information about KittyMocker, read this documentation.
Dependencies
Packages
npm install kittymocklib-ts
KittyMock Version
0.3.2
Lib Concepts Prerequisites
Short Explain
To fully use the lib some concepts need to be previously dominated, if you need some help or be interested here are a topics list and a support material for help you.
Concepts dependencies list
- HTTP Protocol
- Javascript Promises
Lib Overview
KittyMockerBuilder
Short Explain
- This layer has the objective of build new mockers according to your configuration.
Code Example
IAddressParams: This interface standardizes the params used on kittyMockerBuilder creation.
Host: Kitty Mocker server host.
Port: Kitty Mocker server port.
let serverAddress: IAddressParams = { host: "localhost", port: 6999 }
it's necessary to instantiate the object KittyMockerBuilder.
let mockerBuilder = new KittyMockerBuilder(serverAddress)
Response of MockerHttp methods
Short Explain
The manipulation method responses has an objective to help with mocker HTTP usage..
Code Example
export default interface IResponse{
status: boolean //Boolean success
statusCode?: number //statusCode of Http Methods
data?: string //Data of body kitty mock response
message?: string //Message for help users
}
Create mocker
Short Explain
The mocker creation will generate a random port(port range is configured in kittyMockerServer). using this port will be possible to use the mocker resources. On use create MOcker you will receive an object IMockerHttp with a mocker port created and configured for use them resources.
Method Signature
- mockerBuilder.createMocker (): Promise <IMockerHttp>
Code Example
let mockerHttp: MockerHttp
mockerBuilder.createMocker().then((mocker: IMockerHttp) => {
mockerHttp = mocker
)).catch((error: IResponse) => {
console.log(
"Boolean success state: ", error.status
"Error Message: ", error.message,
)
})
Response Schema Example
Success
MockerHttp { mockerHTTPClient: { [Function: wrap] request: [Function: wrap], getUri: [Function: wrap], delete: [Function: wrap], get: [Function: wrap], head: [Function: wrap], options: [Function: wrap], post: [Function: wrap], put: [Function: wrap], patch: [Function: wrap], defaults: { headers: [Object], baseURL: 'http://kittymock.com.br:7008', transformRequest: [Array], transformResponse: [Array], timeout: 1000, adapter: [Function: httpAdapter], xsrfCookieName: 'XSRF-TOKEN', xsrfHeaderName: 'X-XSRF-TOKEN', maxContentLength: -1, validateStatus: [Function: validateStatus] }, interceptors: { request: [InterceptorManager], response: [InterceptorManager] } } }
Error
Create a mocker with a inexistence host or port
{ status: false, message: 'Invalid credentials or not have connectivity with kittyMocker Server' }
MockerHttp
Short Explain
This is a mocker that allows the system simulation that obeys the HTTP.
Create Route
Short Explain
The method createRoute will create a route to the created mocker.
Method Signature
- mockerHttp.createRoute(route: IRoute): Promise<IResponse>
Code Example
IRoute: This interface is responsible flex the route creation standardize the form of how to create it.
let routeSetting: IRoute = { filters: {path: "/route", method: "GET"}, response: {code: 200, body: "Route Created"} }; mockerHttp.createRoute(routeSetting).then((success: IResponse) => { console.log( "Boolean success state: ", success.status, "Status Code: " success.statusCode ) }).catch((error: IResponse) => { console.log( "Boolean success state: ", error.status, "Error Message: ", error.message, "Status Code: ", error.statusCode, ) })
Response Schema Example
Success
{ status: true, statusCode: 200 }
Error
Invalid Path
{ status: false, statusCode: 403, message: 'request with invalid route path' }
Invalid Method
{ status: false, statusCode: 403, message: 'request with invalid route method' }
Invalid Response Code
{ status: false, statusCode: 403, message: 'request with invalid route response code' }
Request in a Route
Short Explain
The method requestRoute makes an HTTP requisition in a created route of the used mocker. method and path have been previously created.
Method Signature
- mockerHttp.requestRoute(path: string, method: Method): Promise<IResponse>
Code Example
The example using routeSetting in createRoute example only demonstrated the standard usage form, but just is necessary that the route is created in the right form in the words using valid path and methods previously created.
mockerHttp.requestRoute(routeSetting.filters.path, routeSetting.filters.method).then((success: IResponse) => { console.log( "Boolean success state: ", success.status, "Status Code: " success.statusCode "Request Body: " success.data ) }).catch((error: IResponse) => { console.log( "Boolean success state: ", error.status, "Error Message: ", error.message, "Status Code: ", error.statusCode, ) })
Response Schema Example
Success
{ status: true, statusCode: 200, data: '"oi"' }
Error
Request in a route, method valid but not created or a invalid path
{ status: false, statusCode: undefined, message: 'Request failed with status code 404' }
Get a Route History
Short Explain
The method getRouteHistory takes a created route history. for verifying the reliability of the requests from the route.
Method Signature
- mockerHttp.getRouteHistory(filter: IFilter): Promise<IResponse>
Code Example
mockerHttp.getRouteHistory(routeSetting.filters).then((success: IResponse) => {
console.log(
"Boolean success state: ", success.status,
"Success Message: ", success.message,
"Request History: " success.data
)
}).catch((error: IResponse) => {
console.log(
"Boolean: ", error.status,
"Error Message: ", error.message,
"Status Code: ", error.statusCode,
)
})
Response Schema Example
Success
{ status: true, statusCode: 200, data: '{"status":"success","data":[]}' }
Error
Get Route History using invalid path or method
{ status: false, statusCode: 404, message: 'Not Found' }
Clear a Route History
Short Explain
The method clearRouteHistory clear a created route's history.
Method Signature
- mockerHttp.clearRouteHistory(filter: IFilter): Promise<IResponse>
Code Example
mockerHttp.clearRouteHistory(routeSetting.filters).then((success: IResponse) => {
console.log(
"Boolean success state: ", success.status,
"Status Code: ", success.statusCode,
)
}).catch((error: IResponse) => {
console.log(
"Boolean success state: ", error.status,
"Error Message: ", error.message,
"Status Code: ", error.statusCode,
)
})
Response Schema Example
Success
{ status: true, statusCode: 204 }
Error
Clear Route History using invalid path or method
{ status: false, statusCode: 404, message: 'Request failed with status code 404'}
Delete a Route
Short Explain
The method deleteRoute deletes an existence route and release the route.
Method Signature
- mockerHttp.deleteRoute(path: string): Promise<IResponse>
Code Example
mockerHttp.deleteRoute(routeSetting.filters.path).then((success: IResponse) => {
console.log(
"Boolean success state: ", success.status,
"Success Message: ", success.message,
)
}).catch((error: IResponse) => {
console.log(
"Boolean: ", error.status,
"Error Message: ", error.message,
"Status Code: ", error.statusCode,
)
})
Response Schema Example
Success
{ status: true, statusCode: 204 }
Error
Delete a inexistence route
{ status: false, statusCode: 404, message: 'route does not exist' }
Delete Mocker
Short Explain
The method delete Docker finished the mocker's life cycle Releasing the occupied port and destroy any data saved in the mocker that will be destroyed.
Method Signature
- mockerHttp.deleteMocker(): Promise<IResponse>;
Code Example
mockerHttp.deleteMocker().then((success: IResponse) => {
console.log(
"Boolean success state: ", success.status,
"Success Message: ", success.message,
"Status Code: ", success.statusCode,
)
}).catch((error: IResponse) => {
console.log(
"Boolean success state: ", error.status,
"Error Message: ", error.message,
"Status Code: ", error.statusCode,
)
})
Response Schema Example
Success
{ status: true, statusCode: 204, message: 'No Content' }