Skip to content

Commit

Permalink
Fix resolve system proxy error when no windows available (#7375)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
jakolehm authored Mar 17, 2023
1 parent 37513de commit 52ede67
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 84 deletions.
2 changes: 0 additions & 2 deletions packages/core/src/main/getDiForUnitTesting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -56,7 +55,6 @@ export function getDiForUnitTesting() {
di.override(globalOverride.injectable, globalOverride.overridingInstantiate);
}

di.override(electronInjectable, () => ({}));
di.override(waitUntilBundledExtensionsAreLoadedInjectable, () => async () => {});

overrideRunnablesHavingSideEffects(di);
Expand Down
Original file line number Diff line number Diff line change
@@ -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),
);
Original file line number Diff line number Diff line change
@@ -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;

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>>;
Expand All @@ -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(
Expand All @@ -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");
});

Expand All @@ -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 () => {
resolveSystemProxyMock = asyncFn();

di.override(
electronInjectable,
() =>
({
webContents: {
getAllWebContents: () => [
{
isDestroyed: () => true,

session: {
resolveProxy: () => {
throw new Error("should never come here");
},
},
},
],
},
} as unknown as typeof electron),
electronBrowserWindowInjectable,
() => () => ({
webContents: {
session: {
resolveProxy: () => {
throw new Error("unexpected error");
},
} as unknown as Session,
} as unknown as WebContents,
} as unknown as BrowserWindow),
);

resolveSystemProxyMock = asyncFn();
Expand All @@ -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", () => {
Expand Down

0 comments on commit 52ede67

Please sign in to comment.