Skip to content

Commit

Permalink
Merge pull request #146 from sliit-foss/fix/service-connector
Browse files Browse the repository at this point in the history
Patch(service-connector): updated request interceptor handling
  • Loading branch information
Akalanka47000 authored Feb 8, 2024
2 parents f5d6efc + 9c8f7f0 commit a68ba40
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/service-connector/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ const serviceConnector = ({ service, headerIntercepts, loggable, logs = true, ..
);
config.headers["x-correlation-id"] = context.get("correlationId");
if (headerIntercepts) {
let intercepts = headerIntercepts(config);
if (intercepts instanceof Promise)
intercepts = await intercepts.catch((e) => logger.error("Failed to intercept headers", e));
config.headers = {
...config.headers,
...(await headerIntercepts(config))
...intercepts
};
}
return config;
Expand Down
33 changes: 33 additions & 0 deletions packages/service-connector/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,39 @@ describe("service-connector", () => {
loggable
);
});
test("successful request with header interceptor", async () => {
const interceptedConnector = serviceConnector({
baseURL: "https://google.com",
headerIntercepts: () => ({
"x-api-key": "123456"
})
});
const response = await interceptedConnector.get("/");
expect(response.status).toEqual(200);
});
test("successful request with async header interceptor", async () => {
const asyncInterceptedConnector = serviceConnector({
baseURL: "https://google.com",
headerIntercepts: async () => {
await new Promise((resolve) => setTimeout(resolve, 50));
return;
}
});
const response = await asyncInterceptedConnector.get("/");
expect(response.status).toEqual(200);
});
test("successful request ignoring error in async header interceptor", async () => {
const asyncInterceptedConnector = serviceConnector({
baseURL: "https://google.com",
headerIntercepts: async () => {
await new Promise((_, reject) => setTimeout(reject, 50));
return;
}
});
const response = await asyncInterceptedConnector.get("/");
expect(response.status).toEqual(200);
expect(mockLogger.error).toBeCalledWith(`Failed to intercept headers`, undefined);
});
});

describe("service-connector resolver", () => {
Expand Down

0 comments on commit a68ba40

Please sign in to comment.