Skip to content

Commit 9fb9682

Browse files
committed
chore: add bodySize to har
1 parent 6cb9e05 commit 9fb9682

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

src/core/utils/harFormatter.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,14 @@ export const getPostData = (req: Request): HarRequest['postData'] => {
5151
}
5252

5353
export const getHarRequestQueryString = (req: Request): HarRequest['queryString'] => {
54-
// req.query is any, which isn't ideal; we need to ensure it's an object with string values
5554
const queryObject: Request['query'] = req.query;
56-
57-
// Convert the object into an array of name-value pairs
55+
5856
const queryString: HarRequest['queryString'] = [];
5957

6058
for (const [name, value] of Object.entries(queryObject)) {
6159
if (Array.isArray(value)) {
62-
// If the value is an array, add an entry for each value
6360
value.forEach(val => queryString.push({ name, value: val as string }));
6461
} else {
65-
// Otherwise, just add the name-value pair directly
6662
queryString.push({ name, value: value as string });
6763
}
6864
}
@@ -71,34 +67,36 @@ export const getHarRequestQueryString = (req: Request): HarRequest['queryString'
7167
}
7268

7369
export const buildHarRequest = (req: Request): HarRequest => {
70+
const requestData = getPostData(req)
7471
return {
7572
method: req.method,
7673
url: req.url,
7774
httpVersion: req.httpVersion,
7875
cookies: [],
7976
headers: getHarHeaders(req.headers),
8077
queryString: getHarRequestQueryString(req),
81-
postData: getPostData(req),
82-
headersSize: -1,
83-
bodySize: -1,
78+
postData: requestData,
79+
headersSize: -1, // not calculating for now
80+
bodySize: requestData ? Buffer.byteLength(requestData.text!) : -1,
8481
}
8582
};
8683

8784
export const buildHarResponse = (res: Response, metadata?: any): HarResponse => {
8885
const { body } = metadata;
86+
const bodySize = body ? Buffer.byteLength(JSON.stringify(body || {})) : -1;
8987
return {
9088
status: res.statusCode,
9189
statusText: res.statusMessage,
9290
httpVersion: res.req.httpVersion,
9391
cookies: [],
9492
headers: getHarHeaders(res.getHeaders()),
9593
content: {
96-
size: Buffer.byteLength(JSON.stringify(body)),
94+
size: bodySize, // same as bodySize since serving uncompressed
9795
mimeType: res.get('Content-Type') || 'application/json',
9896
text: JSON.stringify(body),
9997
},
100-
redirectURL: '',
101-
headersSize: -1,
102-
bodySize: -1,
98+
redirectURL: '', // todo: implement when we integrate rules to mocks
99+
headersSize: -1, // not calculating for now
100+
bodySize,
103101
}
104102
};

src/test/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ import firebaseConfigFetcher from "./firebaseConfigFetcher";
33
import fileLogSink from "./FileLogSink";
44

55
const server = new MockServer(3001, firebaseConfigFetcher, fileLogSink, "/mocksv2");
6-
console.log(server.app);
6+
console.debug(server.app);
77
server.start();

0 commit comments

Comments
 (0)