diff --git a/morty.go b/morty.go index 054c0c3..22bd314 100644 --- a/morty.go +++ b/morty.go @@ -655,7 +655,7 @@ func (rc *RequestConfig) ProxifyURI(uri string) (string, error) { // simple internal link ? // some web pages describe the whole link https://same:auth@same.host/same.path?same.query#new.fragment if u.Scheme == rc.BaseURL.Scheme && - ((u.User == nil && rc.BaseURL.User == nil) || (u.User.String() == rc.BaseURL.User.String())) && + (rc.BaseURL.User == nil || (u.User != nil && u.User.String() == rc.BaseURL.User.String())) && u.Host == rc.BaseURL.Host && u.Path == rc.BaseURL.Path && u.RawQuery == rc.BaseURL.RawQuery { diff --git a/morty_test.go b/morty_test.go index dee5e56..799fb6b 100644 --- a/morty_test.go +++ b/morty_test.go @@ -12,6 +12,11 @@ type AttrTestCase struct { ExpectedOutput []byte } +type StringTestCase struct { + Input string + ExpectedOutput string +} + var attrTestData []*AttrTestCase = []*AttrTestCase{ &AttrTestCase{ []byte("href"), @@ -35,6 +40,17 @@ var attrTestData []*AttrTestCase = []*AttrTestCase{ }, } +var urlTestData []*StringTestCase = []*StringTestCase{ + &StringTestCase{ + "http://x.com/", + "./?mortyurl=http%3A%2F%2Fx.com%2F", + }, + &StringTestCase{ + "http://a@x.com/", + "./?mortyurl=http%3A%2F%2Fa%40x.com%2F", + }, +} + func TestAttrSanitizer(t *testing.T) { u, _ := url.Parse("http://127.0.0.1/") rc := &RequestConfig{BaseURL: u} @@ -44,7 +60,7 @@ func TestAttrSanitizer(t *testing.T) { res, _ := out.ReadBytes(byte(0)) if !bytes.Equal(res, testCase.ExpectedOutput) { t.Errorf( - `Attribute parse error. Name: "%s", Value: "%s", Expected: %s, Got: %s`, + `Attribute parse error. Name: "%s", Value: "%s", Expected: %s, Got: "%s"`, testCase.AttrName, testCase.AttrValue, testCase.ExpectedOutput, @@ -54,6 +70,24 @@ func TestAttrSanitizer(t *testing.T) { } } +func TestURLProxifier(t *testing.T) { + u, _ := url.Parse("http://127.0.0.1/") + rc := &RequestConfig{BaseURL: u} + for _, testCase := range urlTestData { + newUrl, err := rc.ProxifyURI(testCase.Input) + if err != nil { + t.Errorf("Failed to parse URL: %s", testCase.Input) + } + if newUrl != testCase.ExpectedOutput { + t.Errorf( + `URL proxifier error. Expected: "%s", Got: "%s"`, + testCase.ExpectedOutput, + newUrl, + ) + } + } +} + var BENCH_SIMPLE_HTML []byte = []byte(`