@requestly/mock-server
Advanced tools
Comparing version 0.1.6 to 0.2.7
@@ -5,4 +5,3 @@ import { Request } from "express"; | ||
static handleEndpoint: (req: Request) => Promise<MockServerResponse>; | ||
static cleanupEndpoint: (endpoint: string) => string; | ||
} | ||
export default MockServerHandler; |
@@ -28,3 +28,2 @@ "use strict"; | ||
const queryParams = req.query || {}; | ||
endpoint = MockServerHandler.cleanupEndpoint(endpoint); | ||
const kwargs = { | ||
@@ -36,7 +35,3 @@ queryParams: queryParams | ||
console.debug("[Debug] Mock Selected with data", mockData); | ||
const mockResponse = yield mockProcessor_1.default.process(mockData, { | ||
endpoint, | ||
method, | ||
password: queryParams[queryParams_1.RQ_PASSWORD] | ||
}); | ||
const mockResponse = yield mockProcessor_1.default.process(mockData, req, queryParams[queryParams_1.RQ_PASSWORD]); | ||
return mockResponse; | ||
@@ -47,11 +42,2 @@ } | ||
}); | ||
MockServerHandler.cleanupEndpoint = (endpoint) => { | ||
// Stripping front slash. Eg: /users/123/ -> users/123/ | ||
endpoint = endpoint.slice(1); | ||
// Stripping end slash. Eg: users/123/ -> users/123 | ||
if (endpoint.slice(-1) === "/") { | ||
endpoint = endpoint.slice(0, -1); | ||
} | ||
return endpoint; | ||
}; | ||
exports.default = MockServerHandler; |
@@ -0,11 +1,13 @@ | ||
import { Request } from "express"; | ||
import { MockServerResponse } from "../../types"; | ||
import { Mock, MockMetadata, Response } from "../../types/mock"; | ||
import { MockContextParams } from "../../types/internal"; | ||
import { Mock, Response } from "../../types/mock"; | ||
declare class MockProcessor { | ||
static process: (mockData: Mock, requestContextParams: Partial<MockMetadata>) => Promise<MockServerResponse>; | ||
static renderMockServerResponse: (mockData: Mock) => Promise<MockServerResponse>; | ||
static process: (mockData: Mock, request: Request, password?: string) => Promise<MockServerResponse>; | ||
static renderMockServerResponse: (mockData: Mock, request: Request) => Promise<MockServerResponse>; | ||
static renderStatusCode: (responseTemplate: Response) => number; | ||
static renderHeaders: (responseTemplate: Response) => any; | ||
static renderBody: (responseTemplate: Response) => string; | ||
static renderBody: (responseTemplate: Response, mockContextParams: MockContextParams) => string; | ||
static addDelay: (delay?: number) => Promise<unknown>; | ||
} | ||
export default MockProcessor; |
@@ -20,21 +20,28 @@ "use strict"; | ||
const pathMatcher_1 = __importDefault(require("../utils/pathMatcher")); | ||
const templating_1 = require("../utils/templating"); | ||
class MockProcessor { | ||
} | ||
_a = MockProcessor; | ||
MockProcessor.process = (mockData, requestContextParams) => __awaiter(void 0, void 0, void 0, function* () { | ||
const { endpoint, method, password } = requestContextParams; | ||
MockProcessor.process = (mockData, request, password) => __awaiter(void 0, void 0, void 0, function* () { | ||
if (!(0, mockProcessor_1.validatePassword)(mockData.password, password)) { | ||
return (0, mockServerResponseHelper_1.getServerMockResponse)(mockServerResponse_1.HttpStatusCode.UNAUTHORIZED); | ||
} | ||
const urlParams = (0, pathMatcher_1.default)(mockData.endpoint, endpoint).params || {}; | ||
return _a.renderMockServerResponse(mockData); | ||
return _a.renderMockServerResponse(mockData, request); | ||
}); | ||
MockProcessor.renderMockServerResponse = (mockData) => __awaiter(void 0, void 0, void 0, function* () { | ||
MockProcessor.renderMockServerResponse = (mockData, request) => __awaiter(void 0, void 0, void 0, function* () { | ||
// TODO: Right now we select only first response. | ||
// In future this needs to be selected on the basis of rules | ||
const responseTemplate = mockData.responses[0]; | ||
const urlParams = (0, pathMatcher_1.default)(mockData.endpoint, request.path).params || {}; | ||
const contextParams = { | ||
method: request.method, | ||
statusCode: responseTemplate.statusCode, | ||
urlParams, | ||
headers: request.headers || {}, | ||
}; | ||
console.log({ contextParams }); | ||
const mockServerResponse = { | ||
statusCode: _a.renderStatusCode(responseTemplate), | ||
headers: _a.renderHeaders(responseTemplate), | ||
body: _a.renderBody(responseTemplate), | ||
body: _a.renderBody(responseTemplate, contextParams), | ||
}; | ||
@@ -51,3 +58,3 @@ yield _a.addDelay(responseTemplate.latency); | ||
const headers = {}; | ||
Object.keys(responseTemplate.headers).map(key => { | ||
Object.keys(responseTemplate.headers).map((key) => { | ||
headers[key] = responseTemplate.headers[key]; | ||
@@ -59,5 +66,6 @@ }); | ||
// TODO: Do template rendering here | ||
MockProcessor.renderBody = (responseTemplate) => { | ||
MockProcessor.renderBody = (responseTemplate, mockContextParams) => { | ||
let finalBody = null; | ||
finalBody = responseTemplate.body; | ||
let bodyTemplate = responseTemplate.body; | ||
finalBody = (0, templating_1.renderTemplate)(bodyTemplate, mockContextParams); | ||
return finalBody; | ||
@@ -69,4 +77,4 @@ }; | ||
console.debug(`[Debug] Adding delay of ${delay}`); | ||
return new Promise(resolve => setTimeout(resolve, delay)); | ||
return new Promise((resolve) => setTimeout(resolve, delay)); | ||
}); | ||
exports.default = MockProcessor; |
@@ -19,2 +19,3 @@ "use strict"; | ||
const storageService_1 = __importDefault(require("../services/storageService")); | ||
const utils_1 = require("./utils"); | ||
class MockServer { | ||
@@ -55,6 +56,6 @@ constructor(port = 3000, configFetcher, pathPrefix = "") { | ||
Object.defineProperty(req, 'path', { | ||
value: req.path.slice(this.config.pathPrefix.length), | ||
value: (0, utils_1.cleanupPath)(req.path.slice(this.config.pathPrefix.length)), | ||
writable: true | ||
}); | ||
console.log(`Path after stripping prefix: ${req.path}`); | ||
console.log(`Path after stripping prefix and cleanup: ${req.path}`); | ||
} | ||
@@ -61,0 +62,0 @@ const mockResponse = yield mockHandler_1.default.handleEndpoint(req); |
@@ -15,3 +15,3 @@ "use strict"; | ||
latency: 1000, | ||
statusCode: 404, | ||
statusCode: 201, | ||
headers: { | ||
@@ -21,3 +21,3 @@ "foo": "bar", | ||
}, | ||
body: "{\"Hello\":\"There\",\"mockId\":\"1\"}" | ||
body: "{\"Hello\":\"There\",\"mockId\":\"1\", \"statusCode\": {{ statusCode }}, \"method\": \"{{ method }}\", \"urlParams\": \"{{ urlParam 'userId' }}\", \"header\": \"{{ header 'userid' 'test' }}\" }}" | ||
} | ||
@@ -24,0 +24,0 @@ ] |
@@ -8,4 +8,4 @@ "use strict"; | ||
const firebaseConfigFetcher_1 = __importDefault(require("./firebaseConfigFetcher")); | ||
const server = new server_1.default(3000, firebaseConfigFetcher_1.default, "/mocksv2"); | ||
const server = new server_1.default(3001, firebaseConfigFetcher_1.default, "/mocksv2"); | ||
console.log(server.app); | ||
server.start(); |
{ | ||
"name": "@requestly/mock-server", | ||
"version": "0.1.6", | ||
"version": "0.2.7", | ||
"description": "- Methods: GET, POST, PUT, OPTIONS - Description - Endpoint (can be full path) (/api/v1/users) - Multiple Responses - Shuffle Response - Sequential Response - Rules in Response - Status (Any status code 2xx, 4xx) - Latency - Body - Templating - Faker js - Headers", | ||
@@ -19,3 +19,3 @@ "main": "build/index.js", | ||
"@types/path-to-regexp": "^1.7.0", | ||
"nodemon": "^2.0.20", | ||
"nodemon": "^3.1.0", | ||
"rimraf": "^3.0.2", | ||
@@ -22,0 +22,0 @@ "ts-node": "^10.9.1", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
69329
45
790