Create a library for serving static files
It serves files that are in a given directory, and this could be
extended to html files.
In order to validate the functionality we also added a simple
`ScoutingServer` that you can `Start()` and `Stop()`. The unit test
makes use of this. The `main` binary that ties all the components
together also makes use of this.
See the README for an overview of everything we added in this patch.
Change-Id: Id496b032d6aa70fd8502eeb347895ac52b5d1ddd
Signed-off-by: Het Satasiya <satasiyahet@gmail.com>
Signed-off-by: Philipp Schrader <philipp.schrader@gmail.com>
diff --git a/scouting/webserver/static/static_test.go b/scouting/webserver/static/static_test.go
new file mode 100644
index 0000000..15bd872
--- /dev/null
+++ b/scouting/webserver/static/static_test.go
@@ -0,0 +1,57 @@
+package static
+
+import (
+ "fmt"
+ "io/ioutil"
+ "net/http"
+ "testing"
+
+ "github.com/frc971/971-Robot-Code/scouting/webserver/server"
+)
+
+func TestServing(t *testing.T) {
+ cases := []struct {
+ // The path to request from the server.
+ path string
+ // The data that the server is expected to return at the
+ // specified path.
+ expectedData string
+ }{
+ {"/root.txt", "Hello, this is the root page!"},
+ {"/page.txt", "Hello from a page!"},
+ }
+
+ scoutingServer := server.NewScoutingServer()
+ ServePages(scoutingServer, "test_pages")
+ scoutingServer.Start(8080)
+
+ // Go through all the test cases, and run them against the running webserver.
+ for _, c := range cases {
+ dataReceived := getData(c.path, t)
+ if dataReceived != c.expectedData {
+ t.Errorf("Got %q, but expected %q", dataReceived, c.expectedData)
+ }
+ }
+
+ scoutingServer.Stop()
+}
+
+// Retrieves the data at the specified path. If an error occurs, the test case
+// is terminated and failed.
+func getData(path string, t *testing.T) string {
+ resp, err := http.Get(fmt.Sprintf("http://localhost:8080/%s", path))
+ if err != nil {
+ t.Fatalf("Failed to get data ", err)
+ }
+ // Error out if the return status is anything other than 200 OK.
+ if resp.Status != "200 OK" {
+ t.Fatalf("Received a status code other than 200")
+ }
+ // Read the response body.
+ body, err := ioutil.ReadAll(resp.Body)
+ if err != nil {
+ t.Fatalf("Failed to read body")
+ }
+ // Decode the body and return it.
+ return string(body)
+}