blob: 3524c05221c2fc238c27c4e4ac8fbaf60b5608cd [file] [log] [blame]
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()
}
func TestCache(t *testing.T) {
scoutingServer := server.NewScoutingServer()
ServePages(scoutingServer, "test_pages")
scoutingServer.Start(8080)
resp, err := http.Get("http://localhost:8080/root.txt")
if err != nil {
t.Fatalf("Failed to get data ", err)
}
compareString(resp.Header.Get("Expires"), "Thu, 01 Jan 1970 00:00:00 UTC", t)
compareString(resp.Header.Get("Cache-Control"), "no-cache, private, max-age=0", t)
compareString(resp.Header.Get("Pragma"), "no-cache", t)
compareString(resp.Header.Get("X-Accel-Expires"), "0", t)
}
func compareString(actual string, expected string, t *testing.T) {
if actual != expected {
t.Errorf("Expected ", actual, " to equal ", expected)
}
}
// 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)
}