Philipp Schrader | 5562df7 | 2022-02-16 20:56:51 -0800 | [diff] [blame] | 1 | package static |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "io/ioutil" |
| 6 | "net/http" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/frc971/971-Robot-Code/scouting/webserver/server" |
| 10 | ) |
| 11 | |
Philipp Schrader | 45721a7 | 2022-04-02 16:27:53 -0700 | [diff] [blame] | 12 | func expectEqual(t *testing.T, actual string, expected string) { |
| 13 | if actual != expected { |
| 14 | t.Error("Expected ", actual, " to equal ", expected) |
| 15 | } |
| 16 | } |
| 17 | |
Philipp Schrader | 5562df7 | 2022-02-16 20:56:51 -0800 | [diff] [blame] | 18 | func 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 Schrader | 45721a7 | 2022-04-02 16:27:53 -0700 | [diff] [blame] | 26 | {"/", "<h1>This is the index</h1>\n"}, |
Philipp Schrader | 5562df7 | 2022-02-16 20:56:51 -0800 | [diff] [blame] | 27 | {"/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 Schrader | 45721a7 | 2022-04-02 16:27:53 -0700 | [diff] [blame] | 34 | defer scoutingServer.Stop() |
Philipp Schrader | 5562df7 | 2022-02-16 20:56:51 -0800 | [diff] [blame] | 35 | |
| 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 Schrader | 45721a7 | 2022-04-02 16:27:53 -0700 | [diff] [blame] | 39 | expectEqual(t, dataReceived, c.expectedData) |
Philipp Schrader | 5562df7 | 2022-02-16 20:56:51 -0800 | [diff] [blame] | 40 | } |
Philipp Schrader | 5562df7 | 2022-02-16 20:56:51 -0800 | [diff] [blame] | 41 | } |
| 42 | |
Philipp Schrader | 45721a7 | 2022-04-02 16:27:53 -0700 | [diff] [blame] | 43 | // Makes sure that requesting / sets the proper headers so it doesn't get |
| 44 | // cached. |
| 45 | func TestDisallowedCache(t *testing.T) { |
Alex Perry | b2f7652 | 2022-03-30 21:02:05 -0700 | [diff] [blame] | 46 | scoutingServer := server.NewScoutingServer() |
| 47 | ServePages(scoutingServer, "test_pages") |
| 48 | scoutingServer.Start(8080) |
Philipp Schrader | 45721a7 | 2022-04-02 16:27:53 -0700 | [diff] [blame] | 49 | 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. |
| 63 | func TestAllowedCache(t *testing.T) { |
| 64 | scoutingServer := server.NewScoutingServer() |
| 65 | ServePages(scoutingServer, "test_pages") |
| 66 | scoutingServer.Start(8080) |
| 67 | defer scoutingServer.Stop() |
Alex Perry | b2f7652 | 2022-03-30 21:02:05 -0700 | [diff] [blame] | 68 | |
| 69 | resp, err := http.Get("http://localhost:8080/root.txt") |
| 70 | if err != nil { |
| 71 | t.Fatalf("Failed to get data ", err) |
| 72 | } |
Philipp Schrader | 45721a7 | 2022-04-02 16:27:53 -0700 | [diff] [blame] | 73 | 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 Perry | b2f7652 | 2022-03-30 21:02:05 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Philipp Schrader | 45721a7 | 2022-04-02 16:27:53 -0700 | [diff] [blame] | 79 | func 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 Perry | b2f7652 | 2022-03-30 21:02:05 -0700 | [diff] [blame] | 93 | } |
Philipp Schrader | 45721a7 | 2022-04-02 16:27:53 -0700 | [diff] [blame] | 94 | expectEqual(t, resp.Status, "404 Not Found") |
Philipp Schrader | 67fe6d0 | 2022-04-16 15:37:40 -0700 | [diff] [blame] | 95 | |
| 96 | // Make a request with a valid checksum but invalid path and make sure |
| 97 | // we get a 400. |
| 98 | resp, err = http.Get("http://localhost:8080/sha256/553b9b29647a112136986cf93c57b988d1f12dc43d3b774f14a24e58d272dbff/not_root.txt") |
| 99 | if err != nil { |
| 100 | t.Fatal("Failed to get data ", err) |
| 101 | } |
| 102 | expectEqual(t, resp.Status, "400 Bad Request") |
Alex Perry | b2f7652 | 2022-03-30 21:02:05 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Philipp Schrader | 5562df7 | 2022-02-16 20:56:51 -0800 | [diff] [blame] | 105 | // Retrieves the data at the specified path. If an error occurs, the test case |
| 106 | // is terminated and failed. |
| 107 | func getData(path string, t *testing.T) string { |
| 108 | resp, err := http.Get(fmt.Sprintf("http://localhost:8080/%s", path)) |
| 109 | if err != nil { |
| 110 | t.Fatalf("Failed to get data ", err) |
| 111 | } |
| 112 | // Error out if the return status is anything other than 200 OK. |
| 113 | if resp.Status != "200 OK" { |
Philipp Schrader | 45721a7 | 2022-04-02 16:27:53 -0700 | [diff] [blame] | 114 | t.Fatal("Received a status code other than 200:", resp.Status) |
Philipp Schrader | 5562df7 | 2022-02-16 20:56:51 -0800 | [diff] [blame] | 115 | } |
| 116 | // Read the response body. |
| 117 | body, err := ioutil.ReadAll(resp.Body) |
| 118 | if err != nil { |
| 119 | t.Fatalf("Failed to read body") |
| 120 | } |
| 121 | // Decode the body and return it. |
| 122 | return string(body) |
| 123 | } |