Changeset 22 in code for trunk/morty_test.go


Ignore:
Timestamp:
Oct 30, 2016, 4:51:27 PM (9 years ago)
Author:
asciimoo
Message:

[enh] add sanitizer benchmark

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/morty_test.go

    r16 r22  
    3838func TestAttrSanitizer(t *testing.T) {
    3939        u, _ := url.Parse("http://127.0.0.1/")
    40         rc := &RequestConfig{nil, u}
     40        rc := &RequestConfig{BaseURL: u}
    4141        for _, testCase := range attrTestData {
    4242                out := bytes.NewBuffer(nil)
    43                 sanitizeAttr(rc, out, testCase.AttrName, testCase.AttrValue)
     43                sanitizeAttr(rc, out, testCase.AttrName, testCase.AttrValue, testCase.AttrValue)
    4444                res, _ := out.ReadBytes(byte(0))
    4545                if !bytes.Equal(res, testCase.ExpectedOutput) {
     
    5454        }
    5555}
     56
     57var BENCH_SIMPLE_HTML []byte = []byte(`<!doctype html>
     58<html>
     59 <head>
     60  <title>test</title>
     61 </head>
     62 <body>
     63  <h1>Test heading</h1>
     64 </body>
     65</html>`)
     66
     67func BenchmarkSanitizeSimpleHTML(b *testing.B) {
     68        u, _ := url.Parse("http://127.0.0.1/")
     69        rc := &RequestConfig{BaseURL: u}
     70        b.ResetTimer()
     71        for i := 0; i < b.N; i++ {
     72                out := bytes.NewBuffer(nil)
     73                sanitizeHTML(rc, out, BENCH_SIMPLE_HTML)
     74        }
     75}
     76
     77var BENCH_COMPLEX_HTML []byte = []byte(`<!doctype html>
     78<html>
     79 <head>
     80  <noscript><meta http-equiv="refresh" content="0; URL=./xy"></noscript>
     81  <title>test 2</title>
     82  <script> alert('xy'); </script>
     83  <link rel="stylesheet" href="./core.bundle.css">
     84  <style>
     85   html { background: url(./a.jpg); }
     86  </style
     87 </head>
     88 <body>
     89  <h1>Test heading</h1>
     90  <img src="b.png" alt="imgtitle" />
     91  <form action="/z">
     92  <input type="submit" style="background: url(http://aa.bb/cc)" >
     93  </form>
     94 </body>
     95</html>`)
     96
     97func BenchmarkSanitizeComplexHTML(b *testing.B) {
     98        u, _ := url.Parse("http://127.0.0.1/")
     99        rc := &RequestConfig{BaseURL: u}
     100        b.ResetTimer()
     101        for i := 0; i < b.N; i++ {
     102                out := bytes.NewBuffer(nil)
     103                sanitizeHTML(rc, out, BENCH_COMPLEX_HTML)
     104        }
     105}
Note: See TracChangeset for help on using the changeset viewer.