Skip to content

fix: req.getPath() when URL uses {{BASEURL}} or other vars#7280

Open
sanjaikumar-bruno wants to merge 4 commits intousebruno:mainfrom
sanjaikumar-bruno:fix/req-getpath-with-baseurl
Open

fix: req.getPath() when URL uses {{BASEURL}} or other vars#7280
sanjaikumar-bruno wants to merge 4 commits intousebruno:mainfrom
sanjaikumar-bruno:fix/req-getpath-with-baseurl

Conversation

@sanjaikumar-bruno
Copy link
Member

@sanjaikumar-bruno sanjaikumar-bruno commented Feb 24, 2026

BRU-2778
Fixes: #7248

Pre-request scripts run before URL interpolation, so for URLs like {{BASEURL}}/path/:p1/:p2, req.url is still a template and new URL(req.url) throws, causing req.getPath() to return ''

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 24, 2026

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

Walkthrough

Reworks BrunoRequest.getPath() to parse/derive pathnames robustly, extracts interpolation into a new _interpolatePathParams(pathname) helper, adds error handling for non-string/unparseable URLs, and introduces Jest tests covering interpolation and edge cases.

Changes

Cohort / File(s) Summary
Path resolution & interpolation
packages/bruno-js/src/bruno-request.js
Reworked getPath() to use URL parsing with safe fallbacks when parsing fails, added _interpolatePathParams(pathname) to centralize :param interpolation, preserved non-interpolated paths when pathParams is missing, and added checks for non-string URLs.
Unit tests for getPath
packages/bruno-js/tests/bruno-request.spec.js
New Jest tests validating pathname extraction from full URLs, colon-style param interpolation, resolution of template-variable base URLs (e.g., {{BASEURL}}/...), query-string stripping for unparseable URLs, and behavior when req.url is undefined.

Sequence Diagram(s)

(Skipped — changes are localized refactor and tests; no multi-component sequential flow requiring visualization.)

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested labels

size/M

Suggested reviewers

  • helloanoop
  • bijin-bruno
  • lohit-bruno
  • naman-bruno

Poem

In paths where placeholders used to hide,
A helper stitches segments side by side.
Queries trimmed and values placed,
Tests ensure no steps are missed or paced,
Requests now find routes with pride. ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title accurately summarizes the main fix: handling req.getPath() when URLs use template variables like {{BASEURL}}.
Linked Issues check ✅ Passed The code changes directly address issue #7248: req.getPath() now correctly interpolates path parameters and handles template variables in URLs.
Out of Scope Changes check ✅ Passed All changes focus on fixing the req.getPath() behavior with template variable URLs; no unrelated modifications detected.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/bruno-js/src/bruno-request.js`:
- Around line 82-83: The interpolation currently skips valid falsy values
because it checks `if (pathParam && pathParam.value)`; change the guard to allow
0/false by checking for presence rather than truthiness (e.g. verify pathParam
exists and pathParam.value is not undefined/null) so the placeholder is replaced
when pathParam.value is 0 or false; update the condition around the pathParam
handling in the interpolation logic (the `pathParam` check in the code snippet)
to use an explicit undefined/null check.
- Around line 61-67: The fallback in getPath() assumes this.req.url is a string
and calls this.req.url.split('?'), which can throw if req.url is null/undefined;
add a type guard before splitting (e.g., check typeof this.req.url === 'string'
or coerce with a safe default) and handle the non-string case by setting
pathname to '' (or another safe default) instead of calling split; update the
logic around the withoutQuery variable and the existing startsWith/substring
branches to operate only when a valid string URL is present.

ℹ️ Review info

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ade4bfb and b03dcef.

📒 Files selected for processing (1)
  • packages/bruno-js/src/bruno-request.js

@sanjaikumar-bruno sanjaikumar-bruno force-pushed the fix/req-getpath-with-baseurl branch from b03dcef to 73a9047 Compare February 25, 2026 09:32
@pull-request-size pull-request-size bot added size/L and removed size/M labels Feb 25, 2026
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
packages/bruno-js/tests/bruno-request.spec.js (1)

49-57: Add one more degraded-path test for non-string url values.

Line 49 currently covers undefined, but a non-string input (e.g. number/object) is another realistic failure mode and worth pinning down in this suite.

Proposed test addition
   it('returns empty string when req.url is missing', () => {
     const req = new BrunoRequest({
       url: undefined,
       method: 'GET',
       headers: {},
       pathParams: []
     });
     expect(req.getPath()).toBe('');
   });
+
+  it('returns empty string when req.url is not a string', () => {
+    const req = new BrunoRequest({
+      url: 12345,
+      method: 'GET',
+      headers: {},
+      pathParams: []
+    });
+    expect(req.getPath()).toBe('');
+  });

As per coding guidelines: "Cover both the 'happy path' and the realistically problematic paths. Validate expected success behaviour, but also validate error handling, edge cases, and degraded-mode behaviour".

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/bruno-js/tests/bruno-request.spec.js` around lines 49 - 57, Add a
degraded-path unit test to cover non-string url values for the
BrunoRequest.getPath behavior: create a new spec (another it block) that
constructs a BrunoRequest with url set to a non-string (e.g. a number or
object), keep other fields like method/headers/pathParams as in the existing
test, and assert that req.getPath() returns an empty string; this complements
the existing undefined test and ensures BrunoRequest.getPath handles non-string
inputs consistently.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@packages/bruno-js/tests/bruno-request.spec.js`:
- Around line 49-57: Add a degraded-path unit test to cover non-string url
values for the BrunoRequest.getPath behavior: create a new spec (another it
block) that constructs a BrunoRequest with url set to a non-string (e.g. a
number or object), keep other fields like method/headers/pathParams as in the
existing test, and assert that req.getPath() returns an empty string; this
complements the existing undefined test and ensures BrunoRequest.getPath handles
non-string inputs consistently.

ℹ️ Review info

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b03dcef and f672cce.

📒 Files selected for processing (2)
  • packages/bruno-js/src/bruno-request.js
  • packages/bruno-js/tests/bruno-request.spec.js
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/bruno-js/src/bruno-request.js

Copy link
Collaborator

@sanish-bruno sanish-bruno left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Review

Found 2 issue(s): 1 minor bug, 1 test gap.

Summary

  • Good fix for the core issue ({{BASEURL}}/path pattern). The fallback string parsing and extraction of _interpolatePathParams are clean.
  • URLs with an explicit protocol + template host (e.g., https://{{HOST}}/path) produce incorrect pathnames because indexOf('/') matches the / in :// rather than the path separator.
  • The pathParam.value null-check improvement (!= null instead of truthiness) is a nice fix for edge cases like value: 0.

@sanjaikumar-bruno sanjaikumar-bruno force-pushed the fix/req-getpath-with-baseurl branch from f672cce to 8a608fe Compare February 26, 2026 08:39
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

♻️ Duplicate comments (1)
packages/bruno-js/src/bruno-request.js (1)

66-68: ⚠️ Potential issue | 🟠 Major

Bug: indexOf('/') matches the / in ://, producing incorrect pathnames.

This issue was flagged in a previous review but remains unaddressed. For a URL like https://{{HOST}}/path:

  • indexOf('/') returns 6 (the slash in https://)
  • substring(6) yields //{{HOST}}/path instead of /path
Proposed fix to strip protocol separator before searching
       } else {
-        const firstSlash = withoutQuery.indexOf('/');
-        pathname = firstSlash >= 0 ? withoutQuery.substring(firstSlash) : '';
+        let searchIn = withoutQuery;
+        const protoEnd = searchIn.indexOf('://');
+        if (protoEnd >= 0) {
+          searchIn = searchIn.substring(protoEnd + 3);
+        }
+        const firstSlash = searchIn.indexOf('/');
+        pathname = firstSlash >= 0 ? searchIn.substring(firstSlash) : '';
       }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/bruno-js/src/bruno-request.js` around lines 66 - 68, The bug is that
withoutQuery.indexOf('/') picks up the slash in the protocol ("https://"),
producing an incorrect pathname; update the logic around withoutQuery,
firstSlash and pathname to search for the first slash after the protocol
separator: detect the protocol separator position (e.g. idxProto =
withoutQuery.indexOf('://')), set the search start to idxProto >= 0 ? idxProto +
3 : 0, then compute firstSlash = withoutQuery.indexOf('/', searchStart) and
derive pathname = firstSlash >= 0 ? withoutQuery.substring(firstSlash) : ''.
Apply this change where firstSlash and pathname are defined so URLs like
"https://host/path" yield "/path".
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Duplicate comments:
In `@packages/bruno-js/src/bruno-request.js`:
- Around line 66-68: The bug is that withoutQuery.indexOf('/') picks up the
slash in the protocol ("https://"), producing an incorrect pathname; update the
logic around withoutQuery, firstSlash and pathname to search for the first slash
after the protocol separator: detect the protocol separator position (e.g.
idxProto = withoutQuery.indexOf('://')), set the search start to idxProto >= 0 ?
idxProto + 3 : 0, then compute firstSlash = withoutQuery.indexOf('/',
searchStart) and derive pathname = firstSlash >= 0 ?
withoutQuery.substring(firstSlash) : ''. Apply this change where firstSlash and
pathname are defined so URLs like "https://host/path" yield "/path".

ℹ️ Review info

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f672cce and 8a608fe.

📒 Files selected for processing (2)
  • packages/bruno-js/src/bruno-request.js
  • packages/bruno-js/tests/bruno-request.spec.js
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/bruno-js/tests/bruno-request.spec.js

refactor: enhance path parameter interpolation and URL handling in BrunoRequest class

fix: enhance URL path extraction in BrunoRequest class to handle protocols correctly
@sanjaikumar-bruno sanjaikumar-bruno force-pushed the fix/req-getpath-with-baseurl branch from 8a608fe to dfdd00a Compare February 26, 2026 08:46
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
packages/bruno-js/src/bruno-request.js (1)

79-96: Add JSDoc for _interpolatePathParams(pathname).

This is a new abstraction and would benefit from a brief contract (pathname expectations + return behavior).

As per coding guidelines: "Add JSDoc comments to abstractions for additional details."

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/bruno-js/src/bruno-request.js` around lines 79 - 96, Add a JSDoc
comment for the _interpolatePathParams(pathname) method describing the expected
input and return behavior: document that pathname is a string (or falsy), that
this.req.pathParams should be an array of {name, value} objects, that any
segment starting with ':' will be replaced by the matching pathParam.value, and
that the function returns the interpolated pathname (or the original pathname if
no interpolation occurs). Include parameter and return annotations and note that
null/undefined pathname is returned unchanged, and that pathParams entries with
null/undefined values are skipped.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@packages/bruno-js/src/bruno-request.js`:
- Around line 79-96: Add a JSDoc comment for the
_interpolatePathParams(pathname) method describing the expected input and return
behavior: document that pathname is a string (or falsy), that
this.req.pathParams should be an array of {name, value} objects, that any
segment starting with ':' will be replaced by the matching pathParam.value, and
that the function returns the interpolated pathname (or the original pathname if
no interpolation occurs). Include parameter and return annotations and note that
null/undefined pathname is returned unchanged, and that pathParams entries with
null/undefined values are skipped.

ℹ️ Review info

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8a608fe and dfdd00a.

📒 Files selected for processing (2)
  • packages/bruno-js/src/bruno-request.js
  • packages/bruno-js/tests/bruno-request.spec.js
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/bruno-js/tests/bruno-request.spec.js

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
packages/bruno-js/src/bruno-request.js (1)

75-92: Add JSDoc for _interpolatePathParams() helper.

Line 75 introduces a new abstraction without JSDoc. Please document expected input/output and behavior when params are missing to match project standards.

As per coding guidelines: "Add JSDoc comments to abstractions for additional details."

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/bruno-js/src/bruno-request.js` around lines 75 - 92, Add a JSDoc
block for the _interpolatePathParams(pathname) helper that documents the
expected inputs and outputs: describe that pathname is a string (may be falsy),
this.req.pathParams should be an array of objects with {name:string,
value:string|number|null|undefined}, and the function returns a string with
colon-prefixed segments replaced by matching pathParam.value or left unchanged
if no match or value is null/undefined; explicitly note behavior when pathname
is falsy or this.req.pathParams is missing/not an array (returns pathname
unchanged), include `@param` and `@returns` tags, and mark visibility (e.g.
`@private`) to match project standards.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@packages/bruno-js/src/bruno-request.js`:
- Around line 75-92: Add a JSDoc block for the _interpolatePathParams(pathname)
helper that documents the expected inputs and outputs: describe that pathname is
a string (may be falsy), this.req.pathParams should be an array of objects with
{name:string, value:string|number|null|undefined}, and the function returns a
string with colon-prefixed segments replaced by matching pathParam.value or left
unchanged if no match or value is null/undefined; explicitly note behavior when
pathname is falsy or this.req.pathParams is missing/not an array (returns
pathname unchanged), include `@param` and `@returns` tags, and mark visibility (e.g.
`@private`) to match project standards.

ℹ️ Review info

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between dfdd00a and 0fa0360.

📒 Files selected for processing (1)
  • packages/bruno-js/src/bruno-request.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

req.getPath() is empty if the address of the request starts with {{VAR}}

3 participants