Skip to content

Commit

Permalink
fix: remove trailing slash for invalid URL (#455)
Browse files Browse the repository at this point in the history
* fix: remove trailing slash for invalid URL

* add: tests for trailing slash in expression

* Update test/endpoint.test.ts

---------

Co-authored-by: Gregor Martynus <39992+gr2m@users.noreply.github.com>
  • Loading branch information
maemaemae3 and gr2m authored Oct 27, 2023
1 parent 8205e23 commit 1578ba5
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/util/url-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export function parseUrl(template: string) {
function expand(template: string, context: object): string {
var operators = ["+", "#", ".", "/", ";", "?", "&"];

return template.replace(
template = template.replace(
/\{([^\{\}]+)\}|([^\{\}]+)/g,
function (_, expression, literal) {
if (expression) {
Expand Down Expand Up @@ -190,4 +190,10 @@ function expand(template: string, context: object): string {
}
},
);

if (template === "/") {
return template;
} else {
return template.replace(/\/$/, "");
}
}
68 changes: 67 additions & 1 deletion test/endpoint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ describe("endpoint()", () => {
expect(endpoint("/").url).toEqual("https://api.github.com/");
expect(endpoint("/").method).toEqual("GET");
expect(endpoint("https://github.acme-inc/api/v3/").url).toEqual(
"https://github.acme-inc/api/v3/",
"https://github.acme-inc/api/v3",
);
});

Expand Down Expand Up @@ -500,6 +500,72 @@ describe("endpoint()", () => {
});
});

it("Undefined placeholder value with no trailing slash in URL", () => {
const options1 = endpoint("GET /repos/{owner}/{repo}/branches/{branch}", {
owner: "owner",
repo: "repo",
});

expect(options1).toEqual({
method: "GET",
url: "https://api.github.com/repos/owner/repo/branches",
headers: {
accept: "application/vnd.github.v3+json",
"user-agent": userAgent,
},
});

const options2 = endpoint("GET /repos/{owner}/{repo}/branches{/branch}", {
owner: "owner",
repo: "repo",
});

expect(options2).toEqual({
method: "GET",
url: "https://api.github.com/repos/owner/repo/branches",
headers: {
accept: "application/vnd.github.v3+json",
"user-agent": userAgent,
},
});
});

it("Trailing slash in URL in expression is encoded", () => {
const options1 = endpoint(
"GET /repos/{owner}/{repo}/git/matching-refs/{ref}",
{
owner: "owner",
repo: "repo",
ref: "heads/",
},
);

expect(options1).toEqual({
method: "GET",
url: "https://api.github.com/repos/owner/repo/git/matching-refs/heads%2F",
headers: {
accept: "application/vnd.github.v3+json",
"user-agent": userAgent,
},
});

const options2 = endpoint("GET /repos/{owner}/{repo}/contents/{path}", {
owner: "owner",
repo: "repo",
path: "my-folder/",
ref: "feature/branch",
});

expect(options2).toEqual({
method: "GET",
url: "https://api.github.com/repos/owner/repo/contents/my-folder%2F?ref=feature%2Fbranch",
headers: {
accept: "application/vnd.github.v3+json",
"user-agent": userAgent,
},
});
});

it("Undefined header value", () => {
const options = endpoint({
method: "GET",
Expand Down

0 comments on commit 1578ba5

Please sign in to comment.