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

Fix resolve system proxy error when no windows available (#7375)

* fix resolve system proxy error when no windows available

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* fix electronBrowserWindowInjectable id

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

---------

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
Jari Kolehmainen 2023-03-17 21:03:55 +02:00 committed by GitHub
parent 37513dee29
commit 52ede670bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 70 additions and 84 deletions

View File

@ -22,7 +22,6 @@ import electronQuitAndInstallUpdateInjectable from "./electron-app/features/elec
import electronUpdaterIsActiveInjectable from "./electron-app/features/electron-updater-is-active.injectable";
import setUpdateOnQuitInjectable from "./electron-app/features/set-update-on-quit.injectable";
import waitUntilBundledExtensionsAreLoadedInjectable from "./start-main-application/lens-window/application-window/wait-until-bundled-extensions-are-loaded.injectable";
import electronInjectable from "./utils/resolve-system-proxy/electron.injectable";
import initializeClusterManagerInjectable from "./cluster/initialize-manager.injectable";
import type { GlobalOverride } from "@k8slens/test-utils";
import { getOverrideFsWithFakes } from "../test-utils/override-fs-with-fakes";
@ -56,7 +55,6 @@ export function getDiForUnitTesting() {
di.override(globalOverride.injectable, globalOverride.overridingInstantiate);
}
di.override(electronInjectable, () => ({}));
di.override(waitUntilBundledExtensionsAreLoadedInjectable, () => async () => {});
overrideRunnablesHavingSideEffects(di);

View File

@ -0,0 +1,19 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getGlobalOverride } from "@k8slens/test-utils";
import type { BrowserWindow, Session, WebContents } from "electron";
import electronBrowserWindowInjectable from "./electron-browser-window.injectable";
export default getGlobalOverride(
electronBrowserWindowInjectable,
() => () => ({
webContents: {
session: {
resolveProxy: () => "DIRECT",
} as unknown as Session,
} as unknown as WebContents,
} as unknown as BrowserWindow),
);

View File

@ -0,0 +1,19 @@
/**
* 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 { BrowserWindowConstructorOptions } from "electron";
import { BrowserWindow } from "electron";
const electronBrowserWindowInjectable = getInjectable({
id: "electron-browser-window",
instantiate: () => {
return (opts: BrowserWindowConstructorOptions) => {
return new BrowserWindow(opts);
};
},
causesSideEffects: true,
});
export default electronBrowserWindowInjectable;

View File

@ -1,14 +0,0 @@
/**
* 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 electron from "electron";
const electronInjectable = getInjectable({
id: "electron",
instantiate: () => electron,
causesSideEffects: true,
});
export default electronInjectable;

View File

@ -3,28 +3,22 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import electronInjectable from "./electron.injectable";
import electronBrowserWindowInjectable from "./electron-browser-window.injectable";
import withErrorLoggingInjectable from "../../../common/utils/with-error-logging/with-error-logging.injectable";
const resolveSystemProxyFromElectronInjectable = getInjectable({
id: "resolve-system-proxy-from-electron",
instantiate: (di) => {
const electron = di.inject(electronInjectable);
const browserWindow = di.inject(electronBrowserWindowInjectable);
const withErrorLoggingFor = di.inject(withErrorLoggingInjectable);
const withErrorLogging = withErrorLoggingFor(() => "Error resolving proxy");
const hiddenWindow = browserWindow({
show: false,
});
return withErrorLogging(async (url: string) => {
const webContent = electron.webContents
.getAllWebContents()
.find((x) => !x.isDestroyed());
if (!webContent) {
throw new Error(`Tried to resolve proxy for "${url}", but no browser window was available`);
}
return await webContent.session.resolveProxy(url);
return await hiddenWindow.webContents.session.resolveProxy(url);
});
},
});

View File

@ -5,13 +5,13 @@
import { getDiForUnitTesting } from "../../getDiForUnitTesting";
import resolveSystemProxyFromElectronInjectable from "./resolve-system-proxy-from-electron.injectable";
import electronInjectable from "./electron.injectable";
import electronBrowserWindowInjectable from "./electron-browser-window.injectable";
import type { AsyncFnMock } from "@async-fn/jest";
import asyncFn from "@async-fn/jest";
import type electron from "electron";
import { getPromiseStatus } from "@k8slens/test-utils";
import logErrorInjectable from "../../../common/log-error.injectable";
import type { DiContainer } from "@ogre-tools/injectable";
import type { BrowserWindow, Session, WebContents } from "electron";
describe("technical: resolve-system-proxy-from-electron", () => {
let resolveSystemProxyMock: AsyncFnMock<(url: string) => Promise<string>>;
@ -26,44 +26,19 @@ describe("technical: resolve-system-proxy-from-electron", () => {
di.override(logErrorInjectable, () => logErrorMock);
});
describe("given there are non-destroyed Lens windows, when called with URL", () => {
describe("given there are no unexpected issues, when called with URL", () => {
beforeEach(() => {
resolveSystemProxyMock = asyncFn();
di.override(
electronInjectable,
() =>
({
webContents: {
getAllWebContents: () => [
{
isDestroyed: () => true,
session: {
resolveProxy: () => {
throw new Error("should never come here");
},
},
},
{
isDestroyed: () => false,
session: { resolveProxy: resolveSystemProxyMock },
},
{
isDestroyed: () => false,
session: {
resolveProxy: () => {
throw new Error("should never come here");
},
},
},
],
},
} as unknown as typeof electron),
electronBrowserWindowInjectable,
() => () => ({
webContents: {
session: {
resolveProxy: resolveSystemProxyMock,
} as unknown as Session,
} as unknown as WebContents,
} as unknown as BrowserWindow),
);
const resolveSystemProxyFromElectron = di.inject(
@ -73,7 +48,7 @@ describe("technical: resolve-system-proxy-from-electron", () => {
actualPromise = resolveSystemProxyFromElectron("some-url");
});
it("calls to resolve proxy from the first window", () => {
it("calls to resolve proxy from the browser window", () => {
expect(resolveSystemProxyMock).toHaveBeenCalledWith("some-url");
});
@ -90,28 +65,23 @@ describe("technical: resolve-system-proxy-from-electron", () => {
});
});
describe("given there are only destroyed Lens windows, when called with URL", () => {
describe("given there are unexpected issues, when called with URL", () => {
let error: any;
beforeEach(async () => {
di.override(
electronInjectable,
() =>
({
webContents: {
getAllWebContents: () => [
{
isDestroyed: () => true,
resolveSystemProxyMock = asyncFn();
session: {
resolveProxy: () => {
throw new Error("should never come here");
},
},
},
],
},
} as unknown as typeof electron),
di.override(
electronBrowserWindowInjectable,
() => () => ({
webContents: {
session: {
resolveProxy: () => {
throw new Error("unexpected error");
},
} as unknown as Session,
} as unknown as WebContents,
} as unknown as BrowserWindow),
);
resolveSystemProxyMock = asyncFn();
@ -128,7 +98,7 @@ describe("technical: resolve-system-proxy-from-electron", () => {
});
it("throws error", () => {
expect(error.message).toBe('Tried to resolve proxy for "some-url", but no browser window was available');
expect(error.message).toBe("unexpected error");
});
it("logs error", () => {