mirror of https://github.com/dirtbags/moth.git
Add unit test for themes
This commit is contained in:
parent
3eac94c70d
commit
430e44ce87
|
@ -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))
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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())
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue