-
Notifications
You must be signed in to change notification settings - Fork 7
♻️ Improve URL normalisation and fix relative links #985
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
Open
zacharykeeping
wants to merge
3
commits into
main
Choose a base branch
from
url-normalisation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+267
−8
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,242 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| urlP "net/url" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestParseUrl(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| startUrl string | ||
| url string | ||
| expected string | ||
| }{ | ||
| { | ||
| name: "Absolute URL - no change", | ||
| startUrl: "https://example.com/page", | ||
| url: "https://other.com/path", | ||
| expected: "https://other.com/path", | ||
| }, | ||
| { | ||
| name: "Protocol-relative URL", | ||
| startUrl: "https://example.com/page", | ||
| url: "//cdn.example.com/resource.js", | ||
| expected: "https://cdn.example.com/resource.js", | ||
| }, | ||
| { | ||
| name: "Root-relative URL", | ||
| startUrl: "https://example.com/some/page", | ||
| url: "/about", | ||
| expected: "https://example.com/about", | ||
| }, | ||
| { | ||
| name: "Relative URL from page with trailing slash", | ||
| startUrl: "https://example.com/blog/", | ||
| url: "post", | ||
| expected: "https://example.com/blog/post", | ||
| }, | ||
| { | ||
| name: "Relative URL from page without trailing slash", | ||
| startUrl: "https://example.com/blog", | ||
| url: "post", | ||
| expected: "https://example.com/post", // blog is a page, so resolve relative to parent | ||
| }, | ||
| { | ||
| name: "Relative URL from directory with trailing slash", | ||
| startUrl: "https://example.com/blog/", | ||
| url: "post", | ||
| expected: "https://example.com/blog/post", | ||
| }, | ||
| { | ||
| name: "Relative URL from page with extension", | ||
| startUrl: "https://example.com/blog/index.html", | ||
| url: "about.html", | ||
| expected: "https://example.com/blog/about.html", | ||
| }, | ||
| { | ||
| name: "URL with fragment - fragment removed", | ||
| startUrl: "https://example.com/page", | ||
| url: "https://example.com/other#section", | ||
| expected: "https://example.com/other", | ||
| }, | ||
| { | ||
| name: "Deep relative URL", | ||
| startUrl: "https://example.com/a/b/c/page.html", | ||
| url: "d/e/file.html", | ||
| expected: "https://example.com/a/b/c/d/e/file.html", | ||
| }, | ||
| { | ||
| name: "Relative URL with .aspx extension in startUrl", | ||
| startUrl: "https://example.com/products/list.aspx", | ||
| url: "detail", | ||
| expected: "https://example.com/products/detail", | ||
| }, | ||
| { | ||
| name: "Root path should not add trailing slash", | ||
| startUrl: "https://example.com", | ||
| url: "page", | ||
| expected: "https://example.com/page", | ||
| }, | ||
| { | ||
| name: "SSW Rules page - relative link without extension", | ||
| startUrl: "https://www.ssw.com.au/rules/best-way-to-display-code-on-your-website", | ||
| url: "set-language-on-code-blocks", | ||
| expected: "https://www.ssw.com.au/rules/set-language-on-code-blocks", | ||
| }, | ||
| { | ||
| name: "Root level page - relative link", | ||
| startUrl: "https://example.com/page", | ||
| url: "other", | ||
| expected: "https://example.com/other", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| result := parseUrl(tt.startUrl, tt.url) | ||
| if result != tt.expected { | ||
| t.Errorf("parseUrl(%q, %q) = %q; want %q", tt.startUrl, tt.url, result, tt.expected) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestParseUrl_NoTrailingSlashAdded(t *testing.T) { | ||
| // Specifically test that we don't unconditionally add trailing slashes | ||
| tests := []struct { | ||
| name string | ||
| startUrl string | ||
| url string | ||
| wantPath string | ||
| }{ | ||
| { | ||
| name: "Should not add slash to base without directory", | ||
| startUrl: "https://example.com/page", | ||
| url: "other", | ||
| wantPath: "/other", // path.Join should resolve this correctly | ||
| }, | ||
| { | ||
| name: "Should preserve path structure", | ||
| startUrl: "https://example.com/api/v1", | ||
| url: "users", | ||
| wantPath: "/api/users", // Should not become /api/v1/users | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| result := parseUrl(tt.startUrl, tt.url) | ||
| // Check that the path portion matches expectations | ||
| if !containsPath(result, tt.wantPath) { | ||
| t.Errorf("parseUrl(%q, %q) = %q; expected path to contain %q", tt.startUrl, tt.url, result, tt.wantPath) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestIsSameOriginAndPath(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| baseUrl string | ||
| targetUrl string | ||
| expected bool | ||
| }{ | ||
| { | ||
| name: "Exact match", | ||
| baseUrl: "https://example.com/blog", | ||
| targetUrl: "https://example.com/blog", | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "Exact match with trailing slash on both", | ||
| baseUrl: "https://example.com/blog/", | ||
| targetUrl: "https://example.com/blog/", | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "Match with one trailing slash", | ||
| baseUrl: "https://example.com/blog", | ||
| targetUrl: "https://example.com/blog/", | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "Child path with separator", | ||
| baseUrl: "https://example.com/blog", | ||
| targetUrl: "https://example.com/blog/post", | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "Deep child path", | ||
| baseUrl: "https://example.com/blog", | ||
| targetUrl: "https://example.com/blog/2024/01/post", | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "Same prefix but different path - should NOT match", | ||
| baseUrl: "https://example.com/api", | ||
| targetUrl: "https://example.com/api-v2", | ||
| expected: false, | ||
| }, | ||
| { | ||
| name: "Same prefix with hyphen - should NOT match", | ||
| baseUrl: "https://example.com/blog", | ||
| targetUrl: "https://example.com/blogpost", | ||
| expected: false, | ||
| }, | ||
| { | ||
| name: "Different domain - should NOT match", | ||
| baseUrl: "https://example.com/blog", | ||
| targetUrl: "https://other.com/blog", | ||
| expected: false, | ||
| }, | ||
| { | ||
| name: "Base is longer - should NOT match", | ||
| baseUrl: "https://example.com/blog/post", | ||
| targetUrl: "https://example.com/blog", | ||
| expected: false, | ||
| }, | ||
| { | ||
| name: "Query string continuation", | ||
| baseUrl: "https://example.com/search", | ||
| targetUrl: "https://example.com/search?q=test", | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "Fragment continuation", | ||
| baseUrl: "https://example.com/page", | ||
| targetUrl: "https://example.com/page#section", | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "Root path", | ||
| baseUrl: "https://example.com/", | ||
| targetUrl: "https://example.com/anything", | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "Empty path base", | ||
| baseUrl: "https://example.com", | ||
| targetUrl: "https://example.com/page", | ||
| expected: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| result := isSameOriginAndPath(tt.baseUrl, tt.targetUrl) | ||
| if result != tt.expected { | ||
| t.Errorf("isSameOriginAndPath(%q, %q) = %v; want %v", tt.baseUrl, tt.targetUrl, result, tt.expected) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // Helper function to check if a URL contains a specific path | ||
| func containsPath(url, wantPath string) bool { | ||
| parsed, err := urlP.Parse(url) | ||
| if err != nil { | ||
| return false | ||
| } | ||
| return parsed.Path == wantPath | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.