github.com/leplay/upcn@v0.7.0/internal/inject/inject_test.go (about)

     1  package inject_test
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/tj/assert"
     8  	"github.com/apex/up/internal/inject"
     9  )
    10  
    11  var html = `<!doctype html>
    12  <html>
    13    <head>
    14      <meta charset="utf-8">
    15      <title>Example</title>
    16    </head>
    17    <body>
    18      <p>Hello World</p>
    19    </body>
    20  </html>
    21  `
    22  
    23  func ExampleStyle() {
    24  	fmt.Printf("%s\n", inject.Style(`/sloth.css`))
    25  	// Output:
    26  	// <link rel="stylesheet" href="/sloth.css">
    27  }
    28  
    29  func ExampleStyleInline() {
    30  	fmt.Printf("%s\n", inject.StyleInline(`body { display: none }`))
    31  	// Output:
    32  	// <style>body { display: none }</style>
    33  }
    34  
    35  func ExampleScript() {
    36  	fmt.Printf("%s\n", inject.Script(`/sloth.js`))
    37  	// Output:
    38  	// <script src="/sloth.js"></script>
    39  }
    40  
    41  func ExampleScriptInline() {
    42  	fmt.Printf("%s\n", inject.ScriptInline(`const user = { "name": "Tobi" }`))
    43  	// Output:
    44  	// <script>const user = { "name": "Tobi" }</script>
    45  }
    46  
    47  func ExampleComment() {
    48  	fmt.Printf("%s\n", inject.Comment(`Hello World`))
    49  	// Output:
    50  	// <!-- Hello World -->
    51  }
    52  
    53  func ExampleHead() {
    54  	s := inject.Head(html, `<link rel="stylesheet" href="/style.css">`)
    55  	fmt.Printf("%s\n", s)
    56  	// Output:
    57  	// <!doctype html>
    58  	// <html>
    59  	//   <head>
    60  	//     <meta charset="utf-8">
    61  	//     <title>Example</title>
    62  	//     <link rel="stylesheet" href="/style.css">
    63  	//   </head>
    64  	//   <body>
    65  	//     <p>Hello World</p>
    66  	//   </body>
    67  	// </html>
    68  }
    69  
    70  func ExampleBody() {
    71  	s := inject.Body(html, inject.Comment("Version 1.0.3"))
    72  	fmt.Printf("%s\n", s)
    73  	// Output:
    74  	// <!doctype html>
    75  	// <html>
    76  	//   <head>
    77  	//     <meta charset="utf-8">
    78  	//     <title>Example</title>
    79  	//   </head>
    80  	//   <body>
    81  	//     <p>Hello World</p>
    82  	//     <!-- Version 1.0.3 -->
    83  	//   </body>
    84  	// </html>
    85  }
    86  
    87  func ExampleSegment() {
    88  	fmt.Printf("%s\n", inject.Segment(`KEY HERE`))
    89  	// Output:
    90  	// <script>
    91  	//   !function(){var analytics=window.analytics=window.analytics||[];if(!analytics.initialize)if(analytics.invoked)window.console&&console.error&&console.error("Segment snippet included twice.");else{analytics.invoked=!0;analytics.methods=["trackSubmit","trackClick","trackLink","trackForm","pageview","identify","reset","group","track","ready","alias","debug","page","once","off","on"];analytics.factory=function(t){return function(){var e=Array.prototype.slice.call(arguments);e.unshift(t);analytics.push(e);return analytics}};for(var t=0;t<analytics.methods.length;t++){var e=analytics.methods[t];analytics[e]=analytics.factory(e)}analytics.load=function(t){var e=document.createElement("script");e.type="text/javascript";e.async=!0;e.src=("https:"===document.location.protocol?"https://":"http://")+"cdn.segment.com/analytics.js/v1/"+t+"/analytics.min.js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(e,n)};analytics.SNIPPET_VERSION="4.0.0";
    92  	//   analytics.load("KEY HERE");
    93  	//   analytics.page();
    94  	//   }}();
    95  	// </script>
    96  }
    97  
    98  func ExampleGoogleAnalytics() {
    99  	fmt.Printf("%s\n", inject.GoogleAnalytics(`KEY HERE`))
   100  	// Output:
   101  	// <script>
   102  	//   (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
   103  	//   (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
   104  	//   m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
   105  	//   })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
   106  	//
   107  	//   ga('create', 'KEY HERE', 'auto');
   108  	//   ga('send', 'pageview');
   109  	// </script>
   110  }
   111  
   112  func ExampleVar() {
   113  	user := map[string]string{
   114  		"name": "Tobi",
   115  	}
   116  
   117  	fmt.Printf("%s\n", inject.Var("const", "user", user))
   118  	// Output:
   119  	// <script>const user = {"name":"Tobi"}</script>
   120  }
   121  
   122  func TestRule_Default(t *testing.T) {
   123  	r := inject.Rule{Value: `<script></script>`}
   124  	assert.NoError(t, r.Default(), "default")
   125  	assert.NoError(t, r.Validate(), "validate")
   126  	assert.Equal(t, "literal", r.Type)
   127  }
   128  
   129  func TestRule_Validate(t *testing.T) {
   130  	r := inject.Rule{Type: "whatever"}
   131  	assert.NoError(t, r.Default(), "default")
   132  	assert.EqualError(t, r.Validate(), `invalid .type: "whatever" is invalid, must be one of:
   133  
   134    • literal
   135    • comment
   136    • style
   137    • script
   138    • inline style
   139    • inline script
   140    • google analytics
   141    • segment`)
   142  }
   143  
   144  func TestRules_Default(t *testing.T) {
   145  	t.Run("type literal", func(t *testing.T) {
   146  		rules := inject.Rules{
   147  			"head": []*inject.Rule{
   148  				{
   149  					Value: `<script>var user = {}</script>`,
   150  				},
   151  			},
   152  		}
   153  
   154  		assert.NoError(t, rules.Default(), "default")
   155  		assert.NoError(t, rules.Validate(), "validate")
   156  	})
   157  }
   158  
   159  func TestRules_Validate(t *testing.T) {
   160  	t.Run("missing value", func(t *testing.T) {
   161  		rules := inject.Rules{
   162  			"head": []*inject.Rule{
   163  				{
   164  					Type: "inline script",
   165  					// Value: "var user = {}",
   166  				},
   167  			},
   168  		}
   169  
   170  		assert.NoError(t, rules.Default(), "default")
   171  		assert.EqualError(t, rules.Validate(), `head rule #1: .value is required`)
   172  	})
   173  }