[enh] add url proxifier tests ++ fix proxification user url part handling

git-svn-id: file:///srv/svn/repo/yukari/trunk@53 f3bd38d9-da89-464d-a02a-eb04e43141b5
This commit is contained in:
asciimoo 2016-11-29 23:11:00 +00:00
parent 46c188539b
commit c951f11200
2 changed files with 36 additions and 2 deletions

View File

@ -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 {

View File

@ -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(`<!doctype html>
<html>
<head>