From 886a4e4637d06d620e14544829edaf95932135ae Mon Sep 17 00:00:00 2001 From: asciimoo Date: Sat, 29 Oct 2016 00:05:14 +0000 Subject: [PATCH] [enh] basic attribute tests git-svn-id: file:///srv/svn/repo/yukari/trunk@16 f3bd38d9-da89-464d-a02a-eb04e43141b5 --- morty_test.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 morty_test.go diff --git a/morty_test.go b/morty_test.go new file mode 100644 index 0000000..316e7e7 --- /dev/null +++ b/morty_test.go @@ -0,0 +1,55 @@ +package main + +import ( + "bytes" + "net/url" + "testing" +) + +type AttrTestCase struct { + AttrName []byte + AttrValue []byte + ExpectedOutput []byte +} + +var attrTestData []*AttrTestCase = []*AttrTestCase{ + &AttrTestCase{ + []byte("href"), + []byte("./x"), + []byte(` href="./?mortyurl=http%3A%2F%2F127.0.0.1%2Fx"`), + }, + &AttrTestCase{ + []byte("src"), + []byte("http://x.com/y"), + []byte(` src="./?mortyurl=http%3A%2F%2Fx.com%2Fy"`), + }, + &AttrTestCase{ + []byte("action"), + []byte("/z"), + []byte(` action="./?mortyurl=http%3A%2F%2F127.0.0.1%2Fz"`), + }, + &AttrTestCase{ + []byte("onclick"), + []byte("console.log(document.cookies)"), + nil, + }, +} + +func TestAttrSanitizer(t *testing.T) { + u, _ := url.Parse("http://127.0.0.1/") + rc := &RequestConfig{nil, u} + for _, testCase := range attrTestData { + out := bytes.NewBuffer(nil) + sanitizeAttr(rc, out, testCase.AttrName, testCase.AttrValue) + res, _ := out.ReadBytes(byte(0)) + if !bytes.Equal(res, testCase.ExpectedOutput) { + t.Errorf( + `Attribute parse error. Name: "%s", Value: "%s", Expected: %s, Got: %s`, + testCase.AttrName, + testCase.AttrValue, + testCase.ExpectedOutput, + res, + ) + } + } +}