Skip to content

Fix double base_url in get_url() for absolute URLs - #185

Merged
bittner merged 1 commit into
mainfrom
fix/get-url-double-base-url
Jun 3, 2026
Merged

bittner merged 1 commit into
mainfrom
fix/get-url-double-base-url

Conversation

@bittner

@bittner bittner commented Jun 3, 2026

Copy link
Copy Markdown
Member

Summary

  • PatchedContext.get_url() blindly prepended base_url to whatever resolve_url() returned. When the resolved value was already an absolute URL — either because to was a full URL string, or because a model's get_absolute_url() returns one — the result was malformed: http://localhost:8000http://example.com/foo.
  • Now we inspect the resolved URL with urllib.parse.urlparse and skip the prefix when a scheme is present.

Closes #141 (the **kwargs/Pylint sub-tasks in that issue are no longer load-bearing: to is a named parameter so it cannot leak into **kwargs, and the project moved from Pylint to Ruff).

Test plan

  • tests/acceptance/features/context-urlhelper.feature — two new scenarios cover (a) a full URL passed as to and (b) a model whose get_absolute_url() returns a full URL.
  • tests/manage.py behave --tags=~@failing — 31 scenarios pass, 0 fail.
  • ruff check and ruff format --check clean.

@bittner
bittner force-pushed the fix/get-url-double-base-url branch from 9276c97 to 42f1452 Compare June 3, 2026 23:13
@bittner bittner self-assigned this Jun 3, 2026
@bittner
bittner requested a review from Copilot June 3, 2026 23:14

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Fixes PatchedContext.get_url() so it doesn’t incorrectly prepend base_url when resolve_url() already returns an absolute URL (e.g., to is a full URL string or get_absolute_url() returns a full URL), preventing malformed URLs like http://localhost:8000http://example.com/foo.

Changes:

  • Update PatchedContext.get_url() to detect absolute URLs (via urllib.parse.urlparse) and return them unchanged.
  • Add acceptance scenarios covering full URL inputs and models whose get_absolute_url() returns a full URL.
  • Add corresponding step definitions for the new acceptance scenarios.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
behave_django/environment.py Adjusts get_url() URL-building logic to avoid double-host concatenation for absolute URLs.
tests/acceptance/features/context-urlhelper.feature Adds scenarios validating absolute-URL behavior for string and model inputs.
tests/acceptance/steps/context-urlhelper.py Adds step implementations to support the new acceptance scenarios.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread behave_django/environment.py Resolved
resolve_url() returns its input unchanged when it's already a full URL,
and a model's get_absolute_url() may legitimately return one. The old
implementation blindly prepended base_url, producing malformed strings
like "http://localhost:8000http://example.com/foo".

Refs #141.
@bittner
bittner force-pushed the fix/get-url-double-base-url branch from 42f1452 to 7953264 Compare June 3, 2026 23:21
@bittner
bittner merged commit 4773009 into main Jun 3, 2026
13 checks passed
@bittner
bittner deleted the fix/get-url-double-base-url branch June 3, 2026 23:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Investigate potential bug in environment.get_url()

2 participants