From 430e44ce8774f024bb6748ba31f87041ece58ad4 Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Thu, 5 Dec 2019 22:25:03 -0700 Subject: [PATCH] Add unit test for themes --- cmd/mothd/main.go | 19 +++++++++++++------ cmd/mothd/theme.go | 12 +++++------- cmd/mothd/theme_test.go | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 13 deletions(-) create mode 100644 cmd/mothd/theme_test.go diff --git a/cmd/mothd/main.go b/cmd/mothd/main.go index 9461906..7704f65 100644 --- a/cmd/mothd/main.go +++ b/cmd/mothd/main.go @@ -4,6 +4,8 @@ import ( "github.com/namsral/flag" "github.com/spf13/afero" "log" + "mime" + "net/http" "time" ) @@ -37,17 +39,22 @@ func main() { ) stateFs := afero.NewBasePathFs(afero.NewOsFs(), *statePath) + themeFs := afero.NewBasePathFs(afero.NewOsFs(), *themePath) - theme := NewTheme(*themePath) + theme := NewTheme(themeFs) state := NewState(stateFs) puzzles := NewMothballs(*puzzlePath) - go theme.Run(*refreshInterval) go state.Run(*refreshInterval) go puzzles.Run(*refreshInterval) - log.Println("I would be binding to", *bindStr) - time.Sleep(1 * time.Second) - log.Print(state.Export("")) - time.Sleep(19 * time.Second) + // Add some MIME extensions + // Doing this avoids decompressing a mothball entry twice per request + mime.AddExtensionType(".json", "application/json") + mime.AddExtensionType(".zip", "application/zip") + + http.HandleFunc("/", theme.staticHandler) + + log.Printf("Listening on %s", *bindStr) + log.Fatal(http.ListenAndServe(*bindStr, nil)) } diff --git a/cmd/mothd/theme.go b/cmd/mothd/theme.go index 895f294..b7a24ee 100644 --- a/cmd/mothd/theme.go +++ b/cmd/mothd/theme.go @@ -1,20 +1,18 @@ package main import ( + "github.com/spf13/afero" "net/http" - "os" "strings" ) type Theme struct { - Component + fs afero.Fs } -func NewTheme(baseDir string) *Theme { +func NewTheme(fs afero.Fs) *Theme { return &Theme{ - Component: Component{ - baseDir: baseDir, - }, + fs: fs, } } @@ -28,7 +26,7 @@ func (t *Theme) staticHandler(w http.ResponseWriter, req *http.Request) { path = "/index.html" } - f, err := os.Open(t.path(path)) + f, err := t.fs.Open(path) if err != nil { http.NotFound(w, req) return diff --git a/cmd/mothd/theme_test.go b/cmd/mothd/theme_test.go new file mode 100644 index 0000000..bf667a9 --- /dev/null +++ b/cmd/mothd/theme_test.go @@ -0,0 +1,32 @@ +package main + +import ( + "github.com/spf13/afero" + "net/http" + "net/http/httptest" + "testing" +) + +func TestTheme(t *testing.T) { + fs := new(afero.MemMapFs) + afero.WriteFile(fs, "/index.html", []byte("index"), 0644) + afero.WriteFile(fs, "/moo.html", []byte("moo"), 0644) + + s := NewTheme(fs) + + req, err := http.NewRequest("GET", "/", nil) + if err != nil { + t.Fatal(err) + } + + rr := httptest.NewRecorder() + handler := http.HandlerFunc(s.staticHandler) + handler.ServeHTTP(rr, req) + if rr.Code != http.StatusOK { + t.Errorf("Handler returned wrong code: %v", rr.Code) + } + + if rr.Body.String() != "index" { + t.Errorf("Handler returned wrong content: %v", rr.Body.String()) + } +}