Skip to content

Commit

Permalink
Merge pull request #114 from smartive/fix/ignore-headers-config
Browse files Browse the repository at this point in the history
fix: ignore headers config
  • Loading branch information
nickredmark authored Aug 27, 2024
2 parents 7ffb921 + f55eb6e commit 5d4dbb5
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ let recordingsContext = {
active: false,
deleteBodyAttributesForHash: [],
forwardHeadersForRoute: [],
deleteHeadersForHash: [],
recordings: {},
};

Expand Down Expand Up @@ -116,6 +117,7 @@ route.post('/recordings', (req, res) => {
recordingsContext.deleteBodyAttributesForHash = req.body.deleteBodyAttributesForHash || [];
recordingsContext.forwardHeadersForRoute = req.body.forwardHeadersForRoute || [];
recordingsContext.recordings = req.body.recordings || {};
recordingsContext.deleteHeadersForHash = req.body.deleteHeadersForHash || [];
res.sendStatus(204);
});

Expand Down Expand Up @@ -217,8 +219,10 @@ app.all('*', async (req, res) => {

const headers = {
...req.headers,
...(recordingsContext.forwardHeadersForRoute.find((forwardHeaders) => req.url.startsWith(forwardHeaders.route))
?.headers || {}),
...(recordingsContext.forwardHeadersForRoute
// sort longest route first
.sort((a, b) => b.route.length - a.route.length)
.find((forwardHeaders) => req.url.startsWith(forwardHeaders.route))?.headers || {}),
};

const dataToHash = {
Expand All @@ -232,6 +236,10 @@ app.all('*', async (req, res) => {
},
};

recordingsContext.deleteHeadersForHash.forEach((header) => {
delete dataToHash.headers[header];
});

const hash = getShaFromData(dataToHash);

if (recordingsContext.active) {
Expand Down Expand Up @@ -296,7 +304,7 @@ app.all('*', async (req, res) => {
res.status(400).send({
error: {
routes: `Request ${req.url} didn't match any registered route. ${JSON.stringify(req.url, null, 2)}`,
recordings: `Hash ${dataToHash} didn't match any recordings. Request data: ${JSON.stringify(dataToHash, null, 2)}`,
recordings: `Hash ${hash} didn't match any recordings. Request data: ${JSON.stringify(dataToHash, null, 2)}`,
},
url: req.url,
});
Expand Down

0 comments on commit 5d4dbb5

Please sign in to comment.