|
| 1 | +package site |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/anaskhan96/soup" |
| 5 | + "github.com/stretchr/testify/require" |
| 6 | + "os" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func loadExampleHTML(fileName string) string { |
| 11 | + data, err := os.ReadFile(fileName) |
| 12 | + if err != nil { |
| 13 | + panic(err) |
| 14 | + } |
| 15 | + return string(data) |
| 16 | +} |
| 17 | + |
| 18 | +func TestFullPage(t *testing.T) { |
| 19 | + html := loadExampleHTML("example.html") |
| 20 | + root := soup.HTMLParse(html) |
| 21 | + res := fetchText(&root) |
| 22 | + require.LessOrEqual(t, len(res), MaxBodyLength, "Expected empty string, got %s", res) |
| 23 | +} |
| 24 | + |
| 25 | +func TestFetchMeta(t *testing.T) { |
| 26 | + tests := []struct { |
| 27 | + name string |
| 28 | + input string |
| 29 | + expected map[string]string |
| 30 | + }{ |
| 31 | + { |
| 32 | + name: "Test basic", |
| 33 | + input: `<meta name="description" content="This is a test description">`, |
| 34 | + expected: map[string]string{ |
| 35 | + "description": "This is a test description", |
| 36 | + }, |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "Test with multiple meta", |
| 40 | + input: `<meta name="description" content="This is a test description"><meta name="author" content="Alex Collie">`, |
| 41 | + expected: map[string]string{ |
| 42 | + "description": "This is a test description", |
| 43 | + "author": "Alex Collie", |
| 44 | + }, |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "Test with no meta", |
| 48 | + input: `<title>This is a test</title>`, |
| 49 | + expected: map[string]string{}, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "Test with no content", |
| 53 | + input: `<meta name="description">`, |
| 54 | + expected: map[string]string{}, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "Test with no name", |
| 58 | + input: `<meta content="This is a test description">`, |
| 59 | + expected: map[string]string{}, |
| 60 | + }, |
| 61 | + } |
| 62 | + |
| 63 | + for _, test := range tests { |
| 64 | + t.Run(test.name, func(t *testing.T) { |
| 65 | + root := soup.HTMLParse(test.input) |
| 66 | + res := fetchMeta(&root) |
| 67 | + require.Equal(t, test.expected, res) |
| 68 | + }) |
| 69 | + |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func TestStripJavascript(t *testing.T) { |
| 74 | + |
| 75 | + tests := []struct { |
| 76 | + name string |
| 77 | + input string |
| 78 | + expected string |
| 79 | + }{ |
| 80 | + { |
| 81 | + name: "Test basic", |
| 82 | + input: "This is a test <script>console.log('test')</script>", |
| 83 | + expected: "This is a test ", |
| 84 | + }, |
| 85 | + { |
| 86 | + name: "Test with multiple scripts", |
| 87 | + input: "This is a test <script>console.log('test')</script> <script>console.log('test')</script>", |
| 88 | + expected: "This is a test ", |
| 89 | + }, |
| 90 | + { |
| 91 | + name: "Test with no script", |
| 92 | + input: "This is a test", |
| 93 | + expected: "This is a test", |
| 94 | + }, |
| 95 | + { |
| 96 | + name: "Complex ref test", |
| 97 | + input: `<!DOCTYPE html><html lang="en"><head><meta charSet="utf-8"/><meta name="viewport" content="width=device-width"/><title>Alex Collie's personal site</title><meta name="description" content="Alex Collie's personal website, a site for a software developer based in London"/><meta name="next-head-count" content="4"/><link rel="preload" href="/_next/static/css/090c144453b7cc79.css" as="style"/><link rel="stylesheet" href="/_next/static/css/090c144453b7cc79.css" data-n-g=""/><link rel="preload" href="/_next/static/css/ae4ed9c503fd1e33.css" as="style"/><link rel="stylesheet" href="/_next/static/css/ae4ed9c503fd1e33.css" data-n-p=""/><noscript data-n-css=""></noscript><script defer="" nomodule="" src="/_next/static/chunks/polyfills-78c92fac7aa8fdd8.js"></script><script src="/_next/static/chunks/webpack-b8f8d6679aaa5f42.js" defer=""></script><script src="/_next/static/chunks/framework-66d32731bdd20e83.js" defer=""></script><script src="/_next/static/chunks/main-d190bcef2284c937.js" defer=""></script>`, |
| 98 | + expected: `<!DOCTYPE html><html lang="en"><head><meta charSet="utf-8"/><meta name="viewport" content="width=device-width"/><title>Alex Collie's personal site</title><meta name="description" content="Alex Collie's personal website, a site for a software developer based in London"/><meta name="next-head-count" content="4"/><link rel="preload" href="/_next/static/css/090c144453b7cc79.css" as="style"/><link rel="stylesheet" href="/_next/static/css/090c144453b7cc79.css" data-n-g=""/><link rel="preload" href="/_next/static/css/ae4ed9c503fd1e33.css" as="style"/><link rel="stylesheet" href="/_next/static/css/ae4ed9c503fd1e33.css" data-n-p=""/><noscript data-n-css=""></noscript>`, |
| 99 | + }, |
| 100 | + { |
| 101 | + name: "Complex ref test 2", |
| 102 | + input: `<script>window.env={"AD_SLOT_CLIENT_INJECTOR_REGISTRY":"https://test.com"};</script>`, |
| 103 | + expected: ``, |
| 104 | + }, |
| 105 | + } |
| 106 | + |
| 107 | + for _, test := range tests { |
| 108 | + t.Run(test.name, func(t *testing.T) { |
| 109 | + result := test.input |
| 110 | + stripJavascript(&result) |
| 111 | + require.Equal(t, test.expected, result) |
| 112 | + }) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +func TestRemoveCSS(t *testing.T) { |
| 117 | + tests := []struct { |
| 118 | + name string |
| 119 | + input string |
| 120 | + expected string |
| 121 | + }{ |
| 122 | + { |
| 123 | + name: "Test basic", |
| 124 | + input: "This is a test <style>body{color: red}</style>", |
| 125 | + expected: "This is a test ", |
| 126 | + }, |
| 127 | + { |
| 128 | + name: "Test with multiple styles", |
| 129 | + input: "This is a test <style>body{color: red}</style> <style>body{color: red}</style>", |
| 130 | + expected: "This is a test ", |
| 131 | + }, |
| 132 | + { |
| 133 | + name: "Test with no style", |
| 134 | + input: "This is a test", |
| 135 | + expected: "This is a test", |
| 136 | + }, |
| 137 | + } |
| 138 | + |
| 139 | + for _, test := range tests { |
| 140 | + t.Run(test.name, func(t *testing.T) { |
| 141 | + result := test.input |
| 142 | + stripCSS(&result) |
| 143 | + require.Equal(t, test.expected, result) |
| 144 | + }) |
| 145 | + } |
| 146 | +} |
0 commit comments