source: code/trunk/morty_test.go@ 19

Last change on this file since 19 was 16, checked in by asciimoo, 9 years ago

[enh] basic attribute tests

File size: 1.1 KB
Line 
1package main
2
3import (
4 "bytes"
5 "net/url"
6 "testing"
7)
8
9type AttrTestCase struct {
10 AttrName []byte
11 AttrValue []byte
12 ExpectedOutput []byte
13}
14
15var attrTestData []*AttrTestCase = []*AttrTestCase{
16 &AttrTestCase{
17 []byte("href"),
18 []byte("./x"),
19 []byte(` href="./?mortyurl=http%3A%2F%2F127.0.0.1%2Fx"`),
20 },
21 &AttrTestCase{
22 []byte("src"),
23 []byte("http://x.com/y"),
24 []byte(` src="./?mortyurl=http%3A%2F%2Fx.com%2Fy"`),
25 },
26 &AttrTestCase{
27 []byte("action"),
28 []byte("/z"),
29 []byte(` action="./?mortyurl=http%3A%2F%2F127.0.0.1%2Fz"`),
30 },
31 &AttrTestCase{
32 []byte("onclick"),
33 []byte("console.log(document.cookies)"),
34 nil,
35 },
36}
37
38func TestAttrSanitizer(t *testing.T) {
39 u, _ := url.Parse("http://127.0.0.1/")
40 rc := &RequestConfig{nil, u}
41 for _, testCase := range attrTestData {
42 out := bytes.NewBuffer(nil)
43 sanitizeAttr(rc, out, testCase.AttrName, testCase.AttrValue)
44 res, _ := out.ReadBytes(byte(0))
45 if !bytes.Equal(res, testCase.ExpectedOutput) {
46 t.Errorf(
47 `Attribute parse error. Name: "%s", Value: "%s", Expected: %s, Got: %s`,
48 testCase.AttrName,
49 testCase.AttrValue,
50 testCase.ExpectedOutput,
51 res,
52 )
53 }
54 }
55}
Note: See TracBrowser for help on using the repository browser.