github.com/stevelacy/daz@v0.1.4/daz_test.go (about)

     1  package daz
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  var fixture1 = "<div class='app view'><header>test 1<nav>Welcome</nav></header><div>&lt;escaped&gt;</div></div>"
     8  var fixture2 = "<div>onetwothree</div>"
     9  var fixture3 = "<div><div>one</div>one<>text</></div>"
    10  var fixture4 = "<div class='bg-grey-50' data-id='div-1'>content</div>"
    11  var fixture5 = "<div>O&#39;Brian<input type='text' value='input value&#39;s' /></div>"
    12  var fixture6 = "<div><img src='https://example.com/image.png' /><br /></div>"
    13  var fixture7 = "<div>&lt;script&gt;alert(&#39;xss&#39;)&lt;/script&gt;</div>"
    14  var fixture8 = "<div><script>alert('xss')</script></div>"
    15  
    16  func TestBasicRender(t *testing.T) {
    17  	attrs := Attr{"class": "app view"}
    18  	nav := H("nav", "Welcome")
    19  	header := H("header", "test 1", nav)
    20  	escaped := H("div", "<escaped>")
    21  	root := H("div", attrs, header, escaped)
    22  	res := root()
    23  	if res != fixture1 {
    24  		t.Errorf("got: %v wanted: %v", res, fixture1)
    25  	}
    26  }
    27  
    28  func TestStringItems(t *testing.T) {
    29  	items := []string{"one", "two", "three"}
    30  	root := H("div", items)
    31  	res := root()
    32  	if res != fixture2 {
    33  		t.Errorf("got: %v wanted: %v", res, fixture2)
    34  	}
    35  }
    36  
    37  func TestItems1(t *testing.T) {
    38  	one := H("div", "one")
    39  	two := func() string { return "one" }
    40  	three := H("", "text")
    41  	items := []HTML{one, two, three}
    42  
    43  	root := H("div", items)
    44  	res := root()
    45  	if res != fixture3 {
    46  		t.Errorf("got: %v wanted: %v", res, fixture3)
    47  	}
    48  }
    49  func TestItems2(t *testing.T) {
    50  	one := H("div", "one")
    51  	two := func() string { return "one" }
    52  	three := H("", "text")
    53  	items := []HTML{one, two, three}
    54  
    55  	root := H("div", items)
    56  	res := root()
    57  	if res != fixture3 {
    58  		t.Errorf("got: %v wanted: %v", res, fixture3)
    59  	}
    60  }
    61  
    62  func TestAttrs(t *testing.T) {
    63  	attr1 := Attr{"class": "bg-grey-50"}
    64  	attr2 := Attr{"data-id": "div-1"}
    65  	root := H("div", attr1, "content", attr2)
    66  	res := root()
    67  	if res != fixture4 {
    68  		t.Errorf("got: %v wanted: %v", res, fixture4)
    69  	}
    70  }
    71  func TestQuoted(t *testing.T) {
    72  	value := "input value's"
    73  	input := H("input", Attr{"type": "text", "value": value})
    74  	root := H("div", "O'Brian", input)
    75  	res := root()
    76  	if res != fixture5 {
    77  		t.Errorf("got: %v wanted: %v", res, fixture5)
    78  	}
    79  }
    80  
    81  func TestSelfClosing(t *testing.T) {
    82  	root := H("div", H("img", Attr{"src": "https://example.com/image.png"}), H("br"))
    83  	res := root()
    84  	if res != fixture6 {
    85  		t.Errorf("got: %v wanted: %v", res, fixture6)
    86  	}
    87  }
    88  
    89  func TestXSS1(t *testing.T) {
    90  	root := H("div", "<script>alert('xss')</script>")
    91  	res := root()
    92  	if res != fixture7 {
    93  		t.Errorf("got: %v wanted: %v", res, fixture7)
    94  	}
    95  }
    96  
    97  func TestUnsafeContent(t *testing.T) {
    98  	injection := "<script>alert('xss')</script>"
    99  	root := H("div", UnsafeContent(injection))
   100  	res := root()
   101  	if res != fixture8 {
   102  		t.Errorf("got: %v wanted: %v", res, fixture8)
   103  	}
   104  }
   105  
   106  func BenchmarkBasicRender(b *testing.B) {
   107  	attrs := Attr{"class": "app view"}
   108  	nav := H("nav", "Welcome")
   109  	header := H("header", "test 1", nav)
   110  	root := H("div", attrs, header)
   111  	root()
   112  }