forked from itsron143/ParticleGround-Portfolio
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain_test.go
139 lines (109 loc) · 3.72 KB
/
main_test.go
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"encoding/json"
"log"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/PuerkitoBio/goquery"
"github.com/gorilla/mux"
)
// TestAPIEndpoint tests the /api/mirrorlist/:version/json endpoint.
func TestAPIEndpoint(t *testing.T) {
// Create a request to pass to the handler.
req, err := http.NewRequest("GET", "/api/mirrorlist/v1/json", nil)
if err != nil {
t.Fatalf("Could not create request: %v", err)
}
// Add the Content-Type header to simulate a JSON request.
req.Header.Set("Content-Type", "application/json")
// Create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
rr := httptest.NewRecorder()
// Create a new router using Gorilla Mux.
r := setupRouter()
// Serve the request
r.ServeHTTP(rr, req)
// Check if the status code is 200 OK.
if rr.Code != http.StatusOK {
t.Errorf("Expected status code 200, got %d", rr.Code)
}
// Check if the Content-Type is application/json in the response.
contentType := rr.Header().Get("Content-Type")
if contentType != "application/json" {
t.Errorf("Expected Content-Type application/json, got %s", contentType)
}
// Check if the response is valid JSON.
var response MirrorListResponse
if err := json.Unmarshal(rr.Body.Bytes(), &response); err != nil {
t.Errorf("Response was not valid JSON: %v", err)
}
}
// TestRootPath tests the root / path.
func TestRootPath(t *testing.T) {
// Create a request to pass to the handler.
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatalf("Could not create request: %v", err)
}
// Create a ResponseRecorder to record the response.
rr := httptest.NewRecorder()
// Create a new router using Gorilla Mux.
r := setupRouter()
// Serve the request
r.ServeHTTP(rr, req)
// Check if the status code is 200 OK.
if rr.Code != http.StatusOK {
t.Errorf("Expected status code 200, got %d", rr.Code)
}
// Convert the body to a string
body := rr.Body.String()
// Check if the body is non-empty.
if len(body) == 0 {
t.Fatalf("Expected non-empty body, but got an empty body")
}
// Parse the HTML response using goquery
doc, err := goquery.NewDocumentFromReader(strings.NewReader(body))
if err != nil {
t.Fatalf("Could not parse HTML: %v", err)
}
// Validate that the document has the correct DOCTYPE
bodyToTest := strings.ToLower(strings.TrimSpace(body))
if !strings.HasPrefix(bodyToTest, "<!doctype html>") {
t.Errorf("Expected response to start with <!DOCTYPE html>, got %s", body)
}
// Validate the presence of important HTML tags
if doc.Find("html").Length() == 0 {
t.Error("Expected to find <html> tag, but none found")
}
if doc.Find("head").Length() == 0 {
t.Error("Expected to find <head> tag, but none found")
}
if doc.Find("body").Length() == 0 {
t.Error("Expected to find <body> tag, but none found")
}
}
// setupRouter sets up the router for testing.
func setupRouter() *mux.Router {
// Generate templates from configs
if err := GenerateTemplates(); err != nil {
log.Fatalf("Error generating templates: %v", err)
}
// Load runtime templates
err := loadTemplates("./templates/runtime")
if err != nil {
log.Fatalf("Error loading runtime templates: %v", err)
}
r := mux.NewRouter()
// Serve main.tmpl on the root path "/"
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
serveTemplate(w, "home", nil)
}).Methods("GET")
// Serve all static files from the "static" directory
staticFileDirectory := http.Dir("./static")
staticFileHandler := http.StripPrefix("/static/", http.FileServer(staticFileDirectory))
r.PathPrefix("/static/").Handler(staticFileHandler)
// API endpoint for /api/mirrorlist/{version}/json
r.HandleFunc("/api/mirrorlist/{version}/json", MirrorListHandler).Methods("GET")
return r
}