blob: 09ed940a1018611e3048ad1435d527bf10b97d48 [file] [log] [blame]
Philipp Schrader5562df72022-02-16 20:56:51 -08001package static
2
3import (
4 "fmt"
5 "io/ioutil"
6 "net/http"
7 "testing"
8
9 "github.com/frc971/971-Robot-Code/scouting/webserver/server"
10)
11
Philipp Schrader45721a72022-04-02 16:27:53 -070012func expectEqual(t *testing.T, actual string, expected string) {
13 if actual != expected {
14 t.Error("Expected ", actual, " to equal ", expected)
15 }
16}
17
Philipp Schrader5562df72022-02-16 20:56:51 -080018func TestServing(t *testing.T) {
19 cases := []struct {
20 // The path to request from the server.
21 path string
22 // The data that the server is expected to return at the
23 // specified path.
24 expectedData string
25 }{
Philipp Schrader45721a72022-04-02 16:27:53 -070026 {"/", "<h1>This is the index</h1>\n"},
Philipp Schrader5562df72022-02-16 20:56:51 -080027 {"/root.txt", "Hello, this is the root page!"},
28 {"/page.txt", "Hello from a page!"},
29 }
30
31 scoutingServer := server.NewScoutingServer()
32 ServePages(scoutingServer, "test_pages")
33 scoutingServer.Start(8080)
Philipp Schrader45721a72022-04-02 16:27:53 -070034 defer scoutingServer.Stop()
Philipp Schrader5562df72022-02-16 20:56:51 -080035
36 // Go through all the test cases, and run them against the running webserver.
37 for _, c := range cases {
38 dataReceived := getData(c.path, t)
Philipp Schrader45721a72022-04-02 16:27:53 -070039 expectEqual(t, dataReceived, c.expectedData)
Philipp Schrader5562df72022-02-16 20:56:51 -080040 }
Philipp Schrader5562df72022-02-16 20:56:51 -080041}
42
Philipp Schrader45721a72022-04-02 16:27:53 -070043// Makes sure that requesting / sets the proper headers so it doesn't get
44// cached.
45func TestDisallowedCache(t *testing.T) {
Alex Perryb2f76522022-03-30 21:02:05 -070046 scoutingServer := server.NewScoutingServer()
47 ServePages(scoutingServer, "test_pages")
48 scoutingServer.Start(8080)
Philipp Schrader45721a72022-04-02 16:27:53 -070049 defer scoutingServer.Stop()
50
51 resp, err := http.Get("http://localhost:8080/")
52 if err != nil {
53 t.Fatal("Failed to get data ", err)
54 }
55 expectEqual(t, resp.Header.Get("Expires"), "Thu, 01 Jan 1970 00:00:00 UTC")
56 expectEqual(t, resp.Header.Get("Cache-Control"), "no-cache, private, max-age=0")
57 expectEqual(t, resp.Header.Get("Pragma"), "no-cache")
58 expectEqual(t, resp.Header.Get("X-Accel-Expires"), "0")
59}
60
61// Makes sure that requesting anything other than / doesn't set the "do not
62// cache" headers.
63func TestAllowedCache(t *testing.T) {
64 scoutingServer := server.NewScoutingServer()
65 ServePages(scoutingServer, "test_pages")
66 scoutingServer.Start(8080)
67 defer scoutingServer.Stop()
Alex Perryb2f76522022-03-30 21:02:05 -070068
69 resp, err := http.Get("http://localhost:8080/root.txt")
70 if err != nil {
71 t.Fatalf("Failed to get data ", err)
72 }
Philipp Schrader45721a72022-04-02 16:27:53 -070073 expectEqual(t, resp.Header.Get("Expires"), "")
74 expectEqual(t, resp.Header.Get("Cache-Control"), "")
75 expectEqual(t, resp.Header.Get("Pragma"), "")
76 expectEqual(t, resp.Header.Get("X-Accel-Expires"), "")
Alex Perryb2f76522022-03-30 21:02:05 -070077}
78
Philipp Schrader45721a72022-04-02 16:27:53 -070079func TestSha256(t *testing.T) {
80 scoutingServer := server.NewScoutingServer()
81 ServePages(scoutingServer, "test_pages")
82 scoutingServer.Start(8080)
83 defer scoutingServer.Stop()
84
85 // Validate a valid checksum.
86 dataReceived := getData("sha256/553b9b29647a112136986cf93c57b988d1f12dc43d3b774f14a24e58d272dbff/root.txt", t)
87 expectEqual(t, dataReceived, "Hello, this is the root page!")
88
89 // Make a request with an invalid checksum and make sure we get a 404.
90 resp, err := http.Get("http://localhost:8080/sha256/0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef/root.txt")
91 if err != nil {
92 t.Fatal("Failed to get data ", err)
Alex Perryb2f76522022-03-30 21:02:05 -070093 }
Philipp Schrader45721a72022-04-02 16:27:53 -070094 expectEqual(t, resp.Status, "404 Not Found")
Alex Perryb2f76522022-03-30 21:02:05 -070095}
96
Philipp Schrader5562df72022-02-16 20:56:51 -080097// Retrieves the data at the specified path. If an error occurs, the test case
98// is terminated and failed.
99func getData(path string, t *testing.T) string {
100 resp, err := http.Get(fmt.Sprintf("http://localhost:8080/%s", path))
101 if err != nil {
102 t.Fatalf("Failed to get data ", err)
103 }
104 // Error out if the return status is anything other than 200 OK.
105 if resp.Status != "200 OK" {
Philipp Schrader45721a72022-04-02 16:27:53 -0700106 t.Fatal("Received a status code other than 200:", resp.Status)
Philipp Schrader5562df72022-02-16 20:56:51 -0800107 }
108 // Read the response body.
109 body, err := ioutil.ReadAll(resp.Body)
110 if err != nil {
111 t.Fatalf("Failed to read body")
112 }
113 // Decode the body and return it.
114 return string(body)
115}