1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Implement env-agnostic helper to resolve proxy from URL

Co-authored-by: Janne Savolainen <janne.savolainen@live.fi>

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>
This commit is contained in:
Iku-turso 2022-06-21 10:19:31 +03:00
parent bcda046966
commit d6bbe3a969
7 changed files with 198 additions and 0 deletions

View File

@ -0,0 +1,21 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import type { RequestChannel } from "../../../../common/utils/channel/request-channel-injection-token";
import { requestChannelInjectionToken } from "../../../../common/utils/channel/request-channel-injection-token";
export type ResolveProxyChannel = RequestChannel<string, string>;
const resolveProxyChannelInjectable = getInjectable({
id: "resolve-proxy-channel",
instantiate: (): ResolveProxyChannel => ({
id: "resolve-proxy-channel",
}),
injectionToken: requestChannelInjectionToken,
});
export default resolveProxyChannelInjectable;

View File

@ -0,0 +1,12 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectionToken } from "@ogre-tools/injectable";
export type ResolveProxy = (url: string) => Promise<string>;
export const resolveProxyInjectionToken = getInjectionToken<ResolveProxy>({
id: "resolve-proxy",
});

View File

@ -0,0 +1,21 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import resolveProxyChannelInjectable from "../common/resolve-proxy-channel.injectable";
import resolveProxyInjectable from "./resolve-proxy.injectable";
import { requestChannelListenerInjectionToken } from "../../../../common/utils/channel/request-channel-listener-injection-token";
const resolveProxyChannelResponderInjectable = getInjectable({
id: "resolve-proxy-channel-responder",
instantiate: (di) => ({
channel: di.inject(resolveProxyChannelInjectable),
handler: di.inject(resolveProxyInjectable),
}),
injectionToken: requestChannelListenerInjectionToken,
});
export default resolveProxyChannelResponderInjectable;

View File

@ -0,0 +1,15 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
const resolveProxyFromElectronInjectable = getInjectable({
id: "resolve-proxy-from-electron",
instantiate: () => async (url: string) => "not-implemented-yet",
causesSideEffects: true,
});
export default resolveProxyFromElectronInjectable;

View File

@ -0,0 +1,21 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import { resolveProxyInjectionToken } from "../common/resolve-proxy-injection-token";
import resolveProxyFromElectronInjectable from "./resolve-proxy-from-electron.injectable";
const resolveProxyInjectable = getInjectable({
id: "resolve-proxy-for-main",
instantiate: (di) => {
const resolveProxyFromElectron = di.inject(resolveProxyFromElectronInjectable);
return (url) => resolveProxyFromElectron(url);
},
injectionToken: resolveProxyInjectionToken,
});
export default resolveProxyInjectable;

View File

@ -0,0 +1,23 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import { resolveProxyInjectionToken } from "../common/resolve-proxy-injection-token";
import requestFromChannelInjectable from "../../../../renderer/utils/channel/request-from-channel.injectable";
import resolveProxyChannelInjectable from "../common/resolve-proxy-channel.injectable";
const resolveProxyInjectable = getInjectable({
id: "resolve-proxy-for-renderer",
instantiate: (di) => {
const requestFromChannel = di.inject(requestFromChannelInjectable);
const resolveProxyChannel = di.inject(resolveProxyChannelInjectable);
return async (url) => requestFromChannel(resolveProxyChannel, url);
},
injectionToken: resolveProxyInjectionToken,
});
export default resolveProxyInjectable;

View File

@ -0,0 +1,85 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { AsyncFnMock } from "@async-fn/jest";
import asyncFn from "@async-fn/jest";
import type { ApplicationBuilder } from "../../../renderer/components/test-utils/get-application-builder";
import { getApplicationBuilder } from "../../../renderer/components/test-utils/get-application-builder";
import type { ResolveProxy } from "./common/resolve-proxy-injection-token";
import { resolveProxyInjectionToken } from "./common/resolve-proxy-injection-token";
import resolveProxyFromElectronInjectable from "./main/resolve-proxy-from-electron.injectable";
import { getPromiseStatus } from "../../../common/test-utils/get-promise-status";
describe("resolve-proxy", () => {
let applicationBuilder: ApplicationBuilder;
let actualPromise: Promise<string>;
let resolveProxyFromElectronMock: AsyncFnMock<ResolveProxy>;
beforeEach(async () => {
applicationBuilder = getApplicationBuilder();
resolveProxyFromElectronMock = asyncFn();
applicationBuilder.beforeApplicationStart(({ mainDi }) => {
mainDi.override(
resolveProxyFromElectronInjectable,
() => resolveProxyFromElectronMock,
);
});
await applicationBuilder.render();
});
describe("given in main, when called with URL", () => {
beforeEach(async () => {
const resolveProxyInMain = applicationBuilder.dis.mainDi.inject(
resolveProxyInjectionToken,
);
actualPromise = resolveProxyInMain("some-url");
});
it("calls for proxy of the URL from Electron", () => {
expect(resolveProxyFromElectronMock).toHaveBeenCalledWith("some-url");
});
it("does not resolve yet", async () => {
const promiseStatus = await getPromiseStatus(actualPromise);
expect(promiseStatus.fulfilled).toBe(false);
});
it("when the call for proxy resolves, resolves with the proxy", async () => {
resolveProxyFromElectronMock.resolve("some-proxy");
expect(await actualPromise).toBe("some-proxy");
});
});
describe("given in renderer, when called with URL", () => {
beforeEach(async () => {
const resolveProxyInRenderer = applicationBuilder.dis.rendererDi.inject(
resolveProxyInjectionToken,
);
actualPromise = resolveProxyInRenderer("some-url");
});
it("calls for proxy of the URL from Electron", () => {
expect(resolveProxyFromElectronMock).toHaveBeenCalledWith("some-url");
});
it("does not resolve yet", async () => {
const promiseStatus = await getPromiseStatus(actualPromise);
expect(promiseStatus.fulfilled).toBe(false);
});
it("when the call for proxy resolves, resolves with the proxy", async () => {
resolveProxyFromElectronMock.resolve("some-proxy");
expect(await actualPromise).toBe("some-proxy");
});
});
});