-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_test.go
More file actions
115 lines (95 loc) · 3.11 KB
/
server_test.go
File metadata and controls
115 lines (95 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/Meowcenary/information_agent/scraper"
)
func TestHomeHandlerServeHTTP(t *testing.T) {
// Create a new instance of AboutHandler
hh := NewAboutHandler()
// Create a fake HTTP request
req, err := http.NewRequest("GET", "/home", nil)
if err != nil {
t.Fatal(err)
}
// Create a fake HTTP response recorder
w := httptest.NewRecorder()
// Call the ServeHTTP method to handle the request
hh.ServeHTTP(w, req)
// Check the response status code
if w.Code != http.StatusOK {
t.Errorf("expected status code %v, got %v", http.StatusOK, w.Code)
}
}
func TestAboutHandlerServeHTTP(t *testing.T) {
// Create a new instance of AboutHandler
ah := NewAboutHandler()
// Create a fake HTTP request
req, err := http.NewRequest("GET", "/about", nil)
if err != nil {
t.Fatal(err)
}
// Create a fake HTTP response recorder
w := httptest.NewRecorder()
// Call the ServeHTTP method to handle the request
ah.ServeHTTP(w, req)
// Check the response status code
if w.Code != http.StatusOK {
t.Errorf("expected status code %v, got %v", http.StatusOK, w.Code)
}
}
func TestFormatPageHtml(t *testing.T) {
// Create a sample WikiPage
wikiPage := scraper.WikiPage{
Title: "Test Title",
Paragraphs: []scraper.WikiPageParagraph{
{Text: "Test Paragraph 1"},
{Text: "Test Paragraph 2"},
},
}
// Test the FormatPageHtml function
result := FormatPageHtml(wikiPage)
// Expected HTML for the sample WikiPage
expectedHTML := "<html><body><h1>Test Title</h1><hr></hr><p>Test Paragraph 1</p><p>Test Paragraph 2</p></body></html>"
// Check that the result matches the expected value
if result != expectedHTML {
t.Errorf("Expected HTML:\n%s\n\nGot HTML:\n%s", expectedHTML, result)
}
}
func TestRemoveScriptTags(t *testing.T) {
testCases := []struct {
input string
expected string
}{
{"<script>alert('Hello, World!');</script>", ""},
{"<p>This is some text.</p><script>console.log('Script');</script><div>More text</div>", "<p>This is some text.</p><div>More text</div>"},
{"<script src='external.js'></script>", ""},
{"<div><script>console.log('Nested Script');</script></div>", "<div></div>"},
}
for _, tc := range testCases {
result := RemoveScriptTags(tc.input)
// Check that the result matches the expected value
if result != tc.expected {
t.Errorf("For input '%s', expected '%s', got '%s'", tc.input, tc.expected, result)
}
}
}
func TestRemoveAnchorTags(t *testing.T) {
testCases := []struct {
input string
expected string
}{
{"<a href='https://example.com'>Link Text</a>", "Link Text"},
{"<p>This is <a href='#'>a link</a>.</p>", "<p>This is a link.</p>"},
{"<a href='https://example.org'>Another Link</a> <span>Text</span>", "Another Link <span>Text</span>"},
{"<a href='#'>Click me!</a><div><a href='#'>Another Link</a></div>", "Click me!<div>Another Link</div>"},
}
for _, tc := range testCases {
result := RemoveAnchorTags(tc.input)
// Check that the result matches the expected value
if result != tc.expected {
t.Errorf("For input '%s', expected '%s', got '%s'", tc.input, tc.expected, result)
}
}
}