Skip to content
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: 17 additions & 0 deletions src/shared/uriTemplate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ describe("UriTemplate", () => {
expect(template.expand({ username: "fred" })).toBe(
"http://example.com/users/fred",
);
expect(template.variableNames).toEqual(['username'])
});

it("should handle multiple variables", () => {
const template = new UriTemplate("{x,y}");
expect(template.expand({ x: "1024", y: "768" })).toBe("1024,768");
expect(template.variableNames).toEqual(['x', 'y'])
});

it("should encode reserved characters", () => {
Expand All @@ -43,41 +45,47 @@ describe("UriTemplate", () => {
it("should not encode reserved characters with + operator", () => {
const template = new UriTemplate("{+path}/here");
expect(template.expand({ path: "/foo/bar" })).toBe("/foo/bar/here");
expect(template.variableNames).toEqual(['path'])
});
});

describe("fragment expansion", () => {
it("should add # prefix and not encode reserved chars", () => {
const template = new UriTemplate("X{#var}");
expect(template.expand({ var: "/test" })).toBe("X#/test");
expect(template.variableNames).toEqual(['var'])
});
});

describe("label expansion", () => {
it("should add . prefix", () => {
const template = new UriTemplate("X{.var}");
expect(template.expand({ var: "test" })).toBe("X.test");
expect(template.variableNames).toEqual(['var'])
});
});

describe("path expansion", () => {
it("should add / prefix", () => {
const template = new UriTemplate("X{/var}");
expect(template.expand({ var: "test" })).toBe("X/test");
expect(template.variableNames).toEqual(['var'])
});
});

describe("query expansion", () => {
it("should add ? prefix and name=value format", () => {
const template = new UriTemplate("X{?var}");
expect(template.expand({ var: "test" })).toBe("X?var=test");
expect(template.variableNames).toEqual(['var'])
});
});

describe("form continuation expansion", () => {
it("should add & prefix and name=value format", () => {
const template = new UriTemplate("X{&var}");
expect(template.expand({ var: "test" })).toBe("X&var=test");
expect(template.variableNames).toEqual(['var'])
});
});

Expand Down Expand Up @@ -133,13 +141,15 @@ describe("UriTemplate", () => {
resource: "users",
id: "123"
})).toBe("/api/v1/users/123");
expect(template.variableNames).toEqual(['version', 'resource', 'id'])
});

it("should handle query parameters with arrays", () => {
const template = new UriTemplate("/search{?tags*}");
expect(template.expand({
tags: ["nodejs", "typescript", "testing"]
})).toBe("/search?tags=nodejs,typescript,testing");
expect(template.variableNames).toEqual(['tags'])
});

it("should handle multiple query parameters", () => {
Expand All @@ -149,6 +159,7 @@ describe("UriTemplate", () => {
page: "1",
limit: "10"
})).toBe("/search?q=test&page=1&limit=10");
expect(template.variableNames).toEqual(['q', 'page', 'limit'])
});
});

Expand All @@ -161,18 +172,21 @@ describe("UriTemplate", () => {
resource: "users",
id: "123"
});
expect(template.variableNames).toEqual(['version', 'resource', 'id'])
});

it("should match query parameters", () => {
const template = new UriTemplate("/search{?q}");
const match = template.match("/search?q=test");
expect(match).toEqual({ q: "test" });
expect(template.variableNames).toEqual(['q'])
});

it("should match multiple query parameters", () => {
const template = new UriTemplate("/search{?q,page}");
const match = template.match("/search?q=test&page=1");
expect(match).toEqual({ q: "test", page: "1" });
expect(template.variableNames).toEqual(['q', 'page'])
});

it("should handle partial matches correctly", () => {
Expand Down Expand Up @@ -229,17 +243,20 @@ describe("UriTemplate", () => {
it("should handle repeated operators", () => {
const template = new UriTemplate("{?a}{?b}{?c}");
expect(template.expand({ a: "1", b: "2", c: "3" })).toBe("?a=1&b=2&c=3");
expect(template.variableNames).toEqual(['a', 'b', 'c'])
});

it("should handle overlapping variable names", () => {
const template = new UriTemplate("{var}{vara}");
expect(template.expand({ var: "1", vara: "2" })).toBe("12");
expect(template.variableNames).toEqual(['var', 'vara'])
});

it("should handle empty segments", () => {
const template = new UriTemplate("///{a}////{b}////");
expect(template.expand({ a: "1", b: "2" })).toBe("///1////2////");
expect(template.match("///1////2////")).toEqual({ a: "1", b: "2" });
expect(template.variableNames).toEqual(['a', 'b'])
});

it("should handle maximum template expression limit", () => {
Expand Down
4 changes: 4 additions & 0 deletions src/shared/uriTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export class UriTemplate {
| { name: string; operator: string; names: string[]; exploded: boolean }
>;

get variableNames(): string[] {
return this.parts.flatMap((part) => typeof part === 'string' ? [] : part.names);
}

constructor(template: string) {
UriTemplate.validateLength(template, MAX_TEMPLATE_LENGTH, "Template");
this.template = template;
Expand Down