Philipp Schrader | 5562df7 | 2022-02-16 20:56:51 -0800 | [diff] [blame] | 1 | package static |
| 2 | |
| 3 | import ( |
Emily Markova | faecfe1 | 2023-07-01 12:40:03 -0700 | [diff] [blame] | 4 | "errors" |
Philipp Schrader | 5562df7 | 2022-02-16 20:56:51 -0800 | [diff] [blame] | 5 | "fmt" |
| 6 | "io/ioutil" |
| 7 | "net/http" |
| 8 | "testing" |
| 9 | |
Emily Markova | faecfe1 | 2023-07-01 12:40:03 -0700 | [diff] [blame] | 10 | "github.com/frc971/971-Robot-Code/scouting/db" |
Philipp Schrader | 5562df7 | 2022-02-16 20:56:51 -0800 | [diff] [blame] | 11 | "github.com/frc971/971-Robot-Code/scouting/webserver/server" |
| 12 | ) |
| 13 | |
Emily Markova | faecfe1 | 2023-07-01 12:40:03 -0700 | [diff] [blame] | 14 | type MockDatabase struct { |
| 15 | images []db.PitImage |
| 16 | } |
| 17 | |
| 18 | func (database *MockDatabase) QueryPitImageByChecksum(checksum string) (db.PitImage, error) { |
| 19 | for _, data := range database.images { |
| 20 | if data.CheckSum == checksum { |
| 21 | return data, nil |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | return db.PitImage{}, errors.New("Could not find pit image") |
| 26 | } |
| 27 | |
Philipp Schrader | 45721a7 | 2022-04-02 16:27:53 -0700 | [diff] [blame] | 28 | func expectEqual(t *testing.T, actual string, expected string) { |
| 29 | if actual != expected { |
| 30 | t.Error("Expected ", actual, " to equal ", expected) |
| 31 | } |
| 32 | } |
| 33 | |
Philipp Schrader | 5562df7 | 2022-02-16 20:56:51 -0800 | [diff] [blame] | 34 | func TestServing(t *testing.T) { |
Emily Markova | faecfe1 | 2023-07-01 12:40:03 -0700 | [diff] [blame] | 35 | database := MockDatabase{ |
| 36 | images: []db.PitImage{ |
| 37 | { |
| 38 | TeamNumber: "971", CheckSum: "3yi32rhewd23", |
| 39 | ImagePath: "abc.png", ImageData: []byte("hello"), |
| 40 | }, |
| 41 | }, |
| 42 | } |
| 43 | |
Philipp Schrader | 5562df7 | 2022-02-16 20:56:51 -0800 | [diff] [blame] | 44 | cases := []struct { |
| 45 | // The path to request from the server. |
| 46 | path string |
| 47 | // The data that the server is expected to return at the |
| 48 | // specified path. |
| 49 | expectedData string |
| 50 | }{ |
Philipp Schrader | 45721a7 | 2022-04-02 16:27:53 -0700 | [diff] [blame] | 51 | {"/", "<h1>This is the index</h1>\n"}, |
Philipp Schrader | 5562df7 | 2022-02-16 20:56:51 -0800 | [diff] [blame] | 52 | {"/root.txt", "Hello, this is the root page!"}, |
| 53 | {"/page.txt", "Hello from a page!"}, |
Emily Markova | faecfe1 | 2023-07-01 12:40:03 -0700 | [diff] [blame] | 54 | {"/sha256/3yi32rhewd23/abc.png", "hello"}, |
Philipp Schrader | 5562df7 | 2022-02-16 20:56:51 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | scoutingServer := server.NewScoutingServer() |
Emily Markova | faecfe1 | 2023-07-01 12:40:03 -0700 | [diff] [blame] | 58 | ServePages(scoutingServer, "test_pages", &database) |
Philipp Schrader | 5562df7 | 2022-02-16 20:56:51 -0800 | [diff] [blame] | 59 | scoutingServer.Start(8080) |
Philipp Schrader | 45721a7 | 2022-04-02 16:27:53 -0700 | [diff] [blame] | 60 | defer scoutingServer.Stop() |
Philipp Schrader | 5562df7 | 2022-02-16 20:56:51 -0800 | [diff] [blame] | 61 | |
| 62 | // Go through all the test cases, and run them against the running webserver. |
| 63 | for _, c := range cases { |
| 64 | dataReceived := getData(c.path, t) |
Philipp Schrader | 45721a7 | 2022-04-02 16:27:53 -0700 | [diff] [blame] | 65 | expectEqual(t, dataReceived, c.expectedData) |
Philipp Schrader | 5562df7 | 2022-02-16 20:56:51 -0800 | [diff] [blame] | 66 | } |
Philipp Schrader | 5562df7 | 2022-02-16 20:56:51 -0800 | [diff] [blame] | 67 | } |
| 68 | |
Philipp Schrader | 45721a7 | 2022-04-02 16:27:53 -0700 | [diff] [blame] | 69 | // Makes sure that requesting / sets the proper headers so it doesn't get |
| 70 | // cached. |
| 71 | func TestDisallowedCache(t *testing.T) { |
Emily Markova | faecfe1 | 2023-07-01 12:40:03 -0700 | [diff] [blame] | 72 | database := MockDatabase{} |
Alex Perry | b2f7652 | 2022-03-30 21:02:05 -0700 | [diff] [blame] | 73 | scoutingServer := server.NewScoutingServer() |
Emily Markova | faecfe1 | 2023-07-01 12:40:03 -0700 | [diff] [blame] | 74 | ServePages(scoutingServer, "test_pages", &database) |
Alex Perry | b2f7652 | 2022-03-30 21:02:05 -0700 | [diff] [blame] | 75 | scoutingServer.Start(8080) |
Philipp Schrader | 45721a7 | 2022-04-02 16:27:53 -0700 | [diff] [blame] | 76 | defer scoutingServer.Stop() |
| 77 | |
| 78 | resp, err := http.Get("http://localhost:8080/") |
| 79 | if err != nil { |
| 80 | t.Fatal("Failed to get data ", err) |
| 81 | } |
| 82 | expectEqual(t, resp.Header.Get("Expires"), "Thu, 01 Jan 1970 00:00:00 UTC") |
| 83 | expectEqual(t, resp.Header.Get("Cache-Control"), "no-cache, private, max-age=0") |
| 84 | expectEqual(t, resp.Header.Get("Pragma"), "no-cache") |
| 85 | expectEqual(t, resp.Header.Get("X-Accel-Expires"), "0") |
| 86 | } |
| 87 | |
| 88 | // Makes sure that requesting anything other than / doesn't set the "do not |
| 89 | // cache" headers. |
| 90 | func TestAllowedCache(t *testing.T) { |
Emily Markova | faecfe1 | 2023-07-01 12:40:03 -0700 | [diff] [blame] | 91 | database := MockDatabase{} |
Philipp Schrader | 45721a7 | 2022-04-02 16:27:53 -0700 | [diff] [blame] | 92 | scoutingServer := server.NewScoutingServer() |
Emily Markova | faecfe1 | 2023-07-01 12:40:03 -0700 | [diff] [blame] | 93 | ServePages(scoutingServer, "test_pages", &database) |
Philipp Schrader | 45721a7 | 2022-04-02 16:27:53 -0700 | [diff] [blame] | 94 | scoutingServer.Start(8080) |
| 95 | defer scoutingServer.Stop() |
Alex Perry | b2f7652 | 2022-03-30 21:02:05 -0700 | [diff] [blame] | 96 | |
| 97 | resp, err := http.Get("http://localhost:8080/root.txt") |
| 98 | if err != nil { |
| 99 | t.Fatalf("Failed to get data ", err) |
| 100 | } |
Philipp Schrader | 45721a7 | 2022-04-02 16:27:53 -0700 | [diff] [blame] | 101 | expectEqual(t, resp.Header.Get("Expires"), "") |
| 102 | expectEqual(t, resp.Header.Get("Cache-Control"), "") |
| 103 | expectEqual(t, resp.Header.Get("Pragma"), "") |
| 104 | expectEqual(t, resp.Header.Get("X-Accel-Expires"), "") |
Alex Perry | b2f7652 | 2022-03-30 21:02:05 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Philipp Schrader | 45721a7 | 2022-04-02 16:27:53 -0700 | [diff] [blame] | 107 | func TestSha256(t *testing.T) { |
Emily Markova | faecfe1 | 2023-07-01 12:40:03 -0700 | [diff] [blame] | 108 | database := MockDatabase{ |
| 109 | images: []db.PitImage{ |
| 110 | { |
| 111 | TeamNumber: "971", CheckSum: "3yi32rhewd23", |
| 112 | ImagePath: "abc.png", ImageData: []byte{32, 54, 23, 00}, |
| 113 | }, |
| 114 | }, |
| 115 | } |
Philipp Schrader | 45721a7 | 2022-04-02 16:27:53 -0700 | [diff] [blame] | 116 | scoutingServer := server.NewScoutingServer() |
Emily Markova | faecfe1 | 2023-07-01 12:40:03 -0700 | [diff] [blame] | 117 | ServePages(scoutingServer, "test_pages", &database) |
Philipp Schrader | 45721a7 | 2022-04-02 16:27:53 -0700 | [diff] [blame] | 118 | scoutingServer.Start(8080) |
| 119 | defer scoutingServer.Stop() |
| 120 | |
Emily Markova | faecfe1 | 2023-07-01 12:40:03 -0700 | [diff] [blame] | 121 | //Validate receiving the correct byte sequence from image request. |
| 122 | byteDataReceived := getData("sha256/3yi32rhewd23/abc.png", t) |
| 123 | expectEqual(t, string(byteDataReceived), string([]byte{32, 54, 23, 00})) |
| 124 | |
Philipp Schrader | 45721a7 | 2022-04-02 16:27:53 -0700 | [diff] [blame] | 125 | // Validate a valid checksum. |
| 126 | dataReceived := getData("sha256/553b9b29647a112136986cf93c57b988d1f12dc43d3b774f14a24e58d272dbff/root.txt", t) |
| 127 | expectEqual(t, dataReceived, "Hello, this is the root page!") |
| 128 | |
| 129 | // Make a request with an invalid checksum and make sure we get a 404. |
| 130 | resp, err := http.Get("http://localhost:8080/sha256/0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef/root.txt") |
| 131 | if err != nil { |
| 132 | t.Fatal("Failed to get data ", err) |
Alex Perry | b2f7652 | 2022-03-30 21:02:05 -0700 | [diff] [blame] | 133 | } |
Philipp Schrader | 45721a7 | 2022-04-02 16:27:53 -0700 | [diff] [blame] | 134 | expectEqual(t, resp.Status, "404 Not Found") |
Philipp Schrader | 67fe6d0 | 2022-04-16 15:37:40 -0700 | [diff] [blame] | 135 | |
| 136 | // Make a request with a valid checksum but invalid path and make sure |
| 137 | // we get a 400. |
| 138 | resp, err = http.Get("http://localhost:8080/sha256/553b9b29647a112136986cf93c57b988d1f12dc43d3b774f14a24e58d272dbff/not_root.txt") |
| 139 | if err != nil { |
| 140 | t.Fatal("Failed to get data ", err) |
| 141 | } |
| 142 | expectEqual(t, resp.Status, "400 Bad Request") |
Alex Perry | b2f7652 | 2022-03-30 21:02:05 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Philipp Schrader | 5562df7 | 2022-02-16 20:56:51 -0800 | [diff] [blame] | 145 | // Retrieves the data at the specified path. If an error occurs, the test case |
| 146 | // is terminated and failed. |
| 147 | func getData(path string, t *testing.T) string { |
| 148 | resp, err := http.Get(fmt.Sprintf("http://localhost:8080/%s", path)) |
| 149 | if err != nil { |
| 150 | t.Fatalf("Failed to get data ", err) |
| 151 | } |
| 152 | // Error out if the return status is anything other than 200 OK. |
| 153 | if resp.Status != "200 OK" { |
Philipp Schrader | 45721a7 | 2022-04-02 16:27:53 -0700 | [diff] [blame] | 154 | t.Fatal("Received a status code other than 200:", resp.Status) |
Philipp Schrader | 5562df7 | 2022-02-16 20:56:51 -0800 | [diff] [blame] | 155 | } |
| 156 | // Read the response body. |
| 157 | body, err := ioutil.ReadAll(resp.Body) |
| 158 | if err != nil { |
| 159 | t.Fatalf("Failed to read body") |
| 160 | } |
| 161 | // Decode the body and return it. |
| 162 | return string(body) |
| 163 | } |