Skip to content
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

CMR-10147: Removing query parameters from self url when generating Child links #362

Merged
merged 9 commits into from
Oct 15, 2024
38 changes: 38 additions & 0 deletions src/__tests__/providerCatalog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,44 @@ describe("GET /:provider", () => {
});
});
});

describe(`given the provider has a collection`, () => {
it("has a child link for that collection without query parameters", async function () {
this.timeout(5000);
william-valencia marked this conversation as resolved.
Show resolved Hide resolved

sandbox
.stub(Provider, "getProviders")
.resolves([null, [{ "provider-id": "TEST", "short-name": "TEST" }]]);

const mockCollections = generateSTACCollections(1);
sandbox.stub(Collections, "getCollectionIds").resolves({
count: mockCollections.length,
cursor: "foundCursor",
items: mockCollections.map((coll) => ({
id: `${coll.shortName}_${coll.version}`,
title: coll.title ?? faker.random.words(4),
})),
});

const { body: catalog } = await request(stacApp).get("/stac/TEST?param=value");

const children = catalog.links.filter((l: Link) => l.rel === "child");
expect(children).to.have.length(mockCollections.length);

mockCollections.forEach((collection) => {
const childLink = children.find((l: Link) => l.href.endsWith(collection.id));

expect(childLink, JSON.stringify(children, null, 2)).to.have.property(
"type",
"application/json"
);
expect(childLink.href, JSON.stringify(childLink, null, 2)).to.not.contain("?param=value");
expect(childLink.href, JSON.stringify(childLink, null, 2)).to.match(
/^https?:\/\/.*TEST\/collections/
);
});
});
});
});

describe("given CMR providers endpoint responds with an error", () => {
Expand Down
19 changes: 18 additions & 1 deletion src/__tests__/rootCatalog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe("GET /stac", () => {
});

describe("given CMR responds with providers", () => {
before(() => {
beforeEach(() => {
sandbox
.stub(Providers, "getProviders")
.resolves([null, [{ "provider-id": "TEST", "short-name": "TEST" }]]);
Expand All @@ -76,6 +76,23 @@ describe("GET /stac", () => {
expect(providerLink.title).to.equal(provider["provider-id"]);
});
});

it("should have an entry for each provider in the links without query parameters", async () => {
const { statusCode, body } = await request(app).get("/stac?param=value");

expect(statusCode).to.equal(200);
const [, expectedProviders] = cmrProvidersResponse;

expectedProviders!.forEach((provider) => {
const providerLink = body.links.find((l: Link) => l.href.includes(provider["provider-id"]));

expect(providerLink.href).to.match(/^(http)s?:\/\/.*\w+/);
expect(providerLink.href).to.not.contain("?param=value");
expect(providerLink.rel).to.equal("child");
expect(providerLink.type).to.equal("application/json");
expect(providerLink.title).to.equal(provider["provider-id"]);
});
});
});

describe("given CMR providers endpoint responds with an error", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/routes/catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export const providerCatalogHandler = async (req: Request, res: Response) => {
const selfLinks = generateSelfLinks(req);
const childLinks = (collections ?? []).map(({ id, title }) => ({
rel: "child",
href: `${self}/collections/${encodeURIComponent(id)}`,
href: `${self.replace(/\?.*$/, "")}/collections/${encodeURIComponent(id)}`,
william-valencia marked this conversation as resolved.
Show resolved Hide resolved
title,
type: "application/json",
}));
Expand Down
2 changes: 1 addition & 1 deletion src/routes/rootCatalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const providerLinks = (req: Request, providers: Provider[]): Link[] => {
rel: "child",
title,
type: "application/json",
href: `${self}/${providerId}`,
href: `${self.replace(/\?.*$/, "")}/${providerId}`,
}));
};

Expand Down
Loading