-
Notifications
You must be signed in to change notification settings - Fork 6
feat: add storeLog in Config
#21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from 17 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
7139dcf
Add support to dump logs
CodHeK c6d9f43
Remove .log files
CodHeK 55287d4
Add Har middleware
CodHeK c9a9c5e
Move formatting methods to utils
CodHeK 1623e2c
Return HarEntry instead of Har
CodHeK 6cb9e05
[ENGG-2064] - Merge branch 'master' on top of existing log-poc
nsrCodes 9fb9682
chore: add bodySize to har
nsrCodes fbf76e9
refactor:
nsrCodes 52cf014
refactor:
nsrCodes 9564d0a
refactor: as discussed, added and to config
nsrCodes e1537a5
refactor: config is much more consumable as a class
nsrCodes 8237aad
feat: expose log type from package
nsrCodes 2768d50
Merge branch 'ENGG-2064' into ENGG-2064-refactor
nsrCodes af35675
chore: address review commits:
nsrCodes fed0caf
chore: rename locals.metadata to locals.rq_metadata
nsrCodes eb5a87d
Merge pull request #22 from requestly/ENGG-2064-refactor
nsrCodes a611ae4
chore: merged refactor PR for ease of review
nsrCodes 243356f
refactor: address review comments
nsrCodes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,4 @@ | ||
| node_modules/ | ||
| build/ | ||
| build/ | ||
| .DS_Store | ||
| *.log |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| import type { | ||
| Request as HarRequest, | ||
| Response as HarResponse, | ||
| Header as HarHeader, | ||
| } from "har-format"; | ||
| import { IncomingHttpHeaders, OutgoingHttpHeaders } from "http"; | ||
| import { Request, Response } from "express"; | ||
| import { RequestMethod } from "../../types"; | ||
|
|
||
| export const getHarHeaders = (headers: IncomingHttpHeaders | OutgoingHttpHeaders): HarHeader[] => { | ||
| const harHeaders: HarHeader[] = []; | ||
|
|
||
| for (const headerName in headers) { | ||
| const headerValue = headers[headerName]; | ||
| // Header values can be string | string[] according to Node.js typings, | ||
| // but HAR format requires a string, so we need to handle this. | ||
| if (headerValue) { | ||
| const value = Array.isArray(headerValue) ? headerValue.join('; ') : headerValue; | ||
| harHeaders.push({ name: headerName, value: value.toString() }); | ||
| } | ||
| } | ||
|
|
||
| return harHeaders; | ||
| }; | ||
|
|
||
| export const getPostData = (req: Request): HarRequest['postData'] => { | ||
| if ([RequestMethod.POST, RequestMethod.PUT, RequestMethod.PATCH].includes(req.method as RequestMethod)) { | ||
| const postData: any = { | ||
| mimeType: req.get('Content-Type') || 'application/json', | ||
| text: '', | ||
| params: [], | ||
| }; | ||
|
|
||
| // When the body is URL-encoded, the body should be converted into params | ||
| if (postData.mimeType === 'application/x-www-form-urlencoded' && typeof req.body === 'object') { | ||
| postData.params = Object.keys(req.body).map(key => ({ | ||
| name: key, | ||
| value: req.body[key], | ||
| })); | ||
| } else if (req.body) { | ||
| try { | ||
| postData.text = typeof req.body === 'string' ? req.body : JSON.stringify(req.body); | ||
| } catch (error) { | ||
| postData.text = ""; | ||
| } | ||
| } | ||
|
|
||
| return postData; | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| export const getHarRequestQueryString = (req: Request): HarRequest['queryString'] => { | ||
| const queryObject: Request['query'] = req.query; | ||
|
|
||
| const queryString: HarRequest['queryString'] = []; | ||
|
|
||
| for (const [name, value] of Object.entries(queryObject)) { | ||
| if (Array.isArray(value)) { | ||
| value.forEach(val => queryString.push({ name, value: val as string })); | ||
| } else { | ||
| queryString.push({ name, value: value as string }); | ||
| } | ||
| } | ||
|
|
||
| return queryString; | ||
| } | ||
|
|
||
| export const buildHarRequest = (req: Request): HarRequest => { | ||
| const requestData = getPostData(req) | ||
| return { | ||
| method: req.method, | ||
| url: req.url, | ||
| httpVersion: req.httpVersion, | ||
| cookies: [], | ||
| headers: getHarHeaders(req.headers), | ||
| queryString: getHarRequestQueryString(req), | ||
| postData: requestData, | ||
| headersSize: -1, // not calculating for now | ||
| bodySize: requestData ? Buffer.byteLength(requestData.text!) : -1, | ||
| } | ||
| }; | ||
|
|
||
| export const buildHarResponse = (res: Response, metadata?: any): HarResponse => { | ||
| const { body } = metadata; | ||
| const bodySize = body ? Buffer.byteLength(JSON.stringify(body || {})) : -1; | ||
| return { | ||
| status: res.statusCode, | ||
| statusText: res.statusMessage, | ||
| httpVersion: res.req.httpVersion, | ||
| cookies: [], | ||
| headers: getHarHeaders(res.getHeaders()), | ||
| content: { | ||
| size: bodySize, // same as bodySize since serving uncompressed | ||
| mimeType: res.get('Content-Type') || 'application/json', | ||
| text: JSON.stringify(body), | ||
| }, | ||
| redirectURL: '', // todo: implement when we integrate rules to mocks | ||
| headersSize: -1, // not calculating for now | ||
| bodySize, | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,12 @@ | ||
| import IConfigFetcher from "./interfaces/configFetcherInterface"; | ||
| import {IConfig, ISink, ISource} from "./interfaces/config"; | ||
| import MockServer from "./core/server"; | ||
| import { Mock as MockSchema, MockMetadata as MockMetadataSchema, Response as MockResponseSchema } from "./types/mock"; | ||
|
|
||
| import {Log as MockLog} from "./types"; | ||
| export { | ||
| MockServer, | ||
| IConfigFetcher, | ||
| IConfig, ISink, ISource, | ||
| MockSchema, | ||
| MockMetadataSchema, | ||
| MockResponseSchema, | ||
| MockLog, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These both can be merged into 1 as both are same.
Also IConfig is incorrect name as prefix
Iis used for marking abstract classesThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be converted to this
constructor (config: MockServerConfig)