Skip to content
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

Fix for base path copying bug #35

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions url/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func (p *parser) BasicParser(urlOrRef string, base *Url, url *Url, stateOverride
}
} else if base.path.isOpaque() && r == '#' {
url.scheme = base.scheme
url.path = base.path // TODO: Ensure copy????
url.path = base.path.copy()
url.query = base.query
url.fragment = new(string)
state = StateFragment
Expand Down Expand Up @@ -267,7 +267,7 @@ func (p *parser) BasicParser(urlOrRef string, base *Url, url *Url, stateOverride
url.host = base.host
url.port = base.port
url.decodedPort = base.decodedPort
url.path = base.path // TODO: Ensure copy????
url.path = base.path.copy()
url.query = base.query
if r == '?' {
url.query = new(string)
Expand Down Expand Up @@ -456,7 +456,7 @@ func (p *parser) BasicParser(urlOrRef string, base *Url, url *Url, stateOverride
state = StateFileSlash
} else if base != nil && base.scheme == "file" {
url.host = base.host
url.path = base.path // TODO: Ensure copy????
url.path = base.path.copy()
url.query = base.query
if r == '?' {
url.query = new(string)
Expand Down
4 changes: 4 additions & 0 deletions url/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ func (p *path) stripTrailingSpacesIfOpaque() {
}
}

func (p *path) copy() *path {
return &path{p: append([]string{}, p.p...), opaque: p.opaque}
}

func (p *path) String() string {
if p.opaque {
return p.p[0]
Expand Down
27 changes: 27 additions & 0 deletions url/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,3 +481,30 @@ func Test_DecodedPort(t *testing.T) {
t.Errorf("DecodedPort() = %v, want %v", u.DecodedPort(), defaultPort)
}
}

func TestURL_BasePathShouldNotChange(t *testing.T) {

// Test for behaviour described in issue #32
//
// Base URL path erroneously changes during Parse(),
// due to path struct being shared between both
// base Url and newly minted Url.
//
// https://github.com/nlnwa/whatwg-url/issues/32

baseURL := "http://example.org/ns/foo"
p := NewParser()
base, _ := p.Parse(baseURL)

expected := "http://example.org/ns/bar#"
u, _ := base.Parse("bar#")
// Sanity check expected behaviour
if u.String() != expected {
t.Errorf("Parse() = %v, want %v", u, expected)
}

// Ensure the base path has not changed during Parse()
if base.String() != baseURL {
t.Errorf("base URL = %v, want %v", base.String(), baseURL)
}
}