Skip to content

fix: 404 response if the server url includes /general/v0/general #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/hooks/custom/HttpsCheckHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ export class HttpsCheckHook implements SDKInitHook {
sdkInit(opts: SDKInitOptions): SDKInitOptions {
const { baseURL, client } = opts;

if (
baseURL &&
BASE_HOSTNAME_REGEX.test(baseURL.hostname) &&
baseURL.protocol !== BASE_PROTOCOL
) {
console.warn("Base URL protocol is not HTTPS. Updating to HTTPS.");
const newBaseURL = baseURL.href.replace(baseURL.protocol, BASE_PROTOCOL);
return { baseURL: new URL(newBaseURL), client: client };
if (baseURL) {
// -- pathname should always be empty
baseURL.pathname = "/";

if (BASE_HOSTNAME_REGEX.test(baseURL.hostname) && baseURL.protocol !== BASE_PROTOCOL) {
console.warn("Base URL protocol is not HTTPS. Updating to HTTPS.");
const newBaseURL = baseURL.href.replace(baseURL.protocol, BASE_PROTOCOL);
return {baseURL: new URL(newBaseURL), client: client};
}
}

return { baseURL: baseURL, client: client };
Expand Down
40 changes: 40 additions & 0 deletions test/integration/HttpsCheckHook.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { readFileSync } from "fs";

import { UnstructuredClient } from "../../src";
import { PartitionResponse } from "../../src/sdk/models/operations";
import { PartitionParameters, Strategy } from "../../src/sdk/models/shared";

describe("HttpsCheckHook integration tests", () => {
const FAKE_API_KEY = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";

it.each([
"http://localhost:8000",
"http://localhost:8000/general/v0/general",
])("should throw error when given filename is empty", async (serverURL) => {
const client = new UnstructuredClient({
serverURL: serverURL,
security: {
apiKeyAuth: FAKE_API_KEY,
},
});

const file = {
content: readFileSync("test/data/layout-parser-paper-fast.pdf"),
fileName: "test/data/layout-parser-paper-fast.pdf",
};

const requestParams: PartitionParameters = {
files: file,
strategy: Strategy.Fast,
};

const res: PartitionResponse = await client.general.partition({
partitionParameters: {
...requestParams,
splitPdfPage: false,
},
});

expect(res.statusCode).toEqual(200);
});
});
12 changes: 12 additions & 0 deletions test/unit/HttpsCheckHook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,16 @@ describe("HttpsCheckHook", () => {
expect(result.baseURL).toBeNull();
expect(consoleSpy).not.toHaveBeenCalled();
});

it("should update the pathname to empty", () => {
const baseURL = new URL("https://example.unstructuredapp.io/general/v0/general");
const client = new HTTPClient();
const opts = { baseURL, client };
const hook = new HttpsCheckHook();

const result = hook.sdkInit(opts);

expect(result.baseURL?.pathname).toBe("/");
expect(consoleSpy).not.toHaveBeenCalled();
});
});
Loading