Skip to content

Commit

Permalink
Normalize non-encoded utf-8 URL
Browse files Browse the repository at this point in the history
Signed-off-by: Antoine Pelisse <[email protected]>
  • Loading branch information
apelisse authored and youyuanwu committed Jan 14, 2023
1 parent 424d68c commit 53e3421
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions internal/normalize_url.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,16 @@ var rxDupSlashes = regexp.MustCompile(`/{2,}`)
// - FlagLowercaseHost
// - FlagRemoveDefaultPort
// - FlagRemoveDuplicateSlashes (and this was mixed in with the |)
//
// This also normalizes the URL into its urlencoded form by removing RawPath and RawFragment.
func NormalizeURL(u *url.URL) {
lowercaseScheme(u)
lowercaseHost(u)
removeDefaultPort(u)
removeDuplicateSlashes(u)

u.RawPath = ""
u.RawFragment = ""
}

func lowercaseScheme(u *url.URL) {
Expand Down
15 changes: 15 additions & 0 deletions reference_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
package jsonreference

import (
"reflect"
"testing"

"github.com/go-openapi/jsonpointer"
Expand Down Expand Up @@ -421,3 +422,17 @@ func TestReferenceResolution(t *testing.T) {
}
}
}

func TestIdenticalURLEncoded(t *testing.T) {
expected, err := New("https://localhost/🌭#/🍔")
if err != nil {
t.Fatalf("Failed to create jsonreference: %v", err)
}
actual, err := New("https://localhost/%F0%9F%8C%AD#/%F0%9F%8D%94")
if err != nil {
t.Fatalf("Failed to create jsonreference: %v", err)
}
if !reflect.DeepEqual(expected, actual) {
t.Fatalf("expected %v (URL: %#v), got %v (URL: %#v)", expected.String(), expected.referenceURL, actual.String(), actual.referenceURL)
}
}

0 comments on commit 53e3421

Please sign in to comment.