A bit cleaner test interface, maybe

This commit is contained in:
Neale Pickett 2022-05-10 19:48:51 -06:00
parent 85f5b96a40
commit bde4b2c86d
2 changed files with 21 additions and 12 deletions

View File

@ -36,8 +36,7 @@ func (hs *HTTPServer) TestRequest(path string, args map[string]string) *httptest
func TestHttpd(t *testing.T) {
server := NewTestServer()
hs := NewHTTPServer("/", server)
stateProvider := server.State.(*State)
hs := NewHTTPServer("/", server.MothServer)
if r := hs.TestRequest("/", nil); r.Result().StatusCode != 200 {
t.Error(r.Result())
@ -74,7 +73,7 @@ func TestHttpd(t *testing.T) {
t.Error("Register failed", r.Body.String())
}
stateProvider.refresh()
server.refresh()
if r := hs.TestRequest("/state", nil); r.Result().StatusCode != 200 {
t.Error(r.Result())
@ -131,7 +130,7 @@ func TestHttpd(t *testing.T) {
t.Error("Unexpected body", r.Body.String())
}
stateProvider.refresh()
server.refresh()
if r := hs.TestRequest("/content/pategory/2/puzzle.json", nil); r.Result().StatusCode != 200 {
t.Error(r.Result())
@ -183,7 +182,7 @@ func TestDevelMemHttpd(t *testing.T) {
srv := NewTestServer()
{
hs := NewHTTPServer("/", srv)
hs := NewHTTPServer("/", srv.MothServer)
if r := hs.TestRequest("/mothballer/pategory.md", nil); r.Result().StatusCode != 404 {
t.Error("Should have gotten a 404 for mothballer in prod mode")
@ -192,7 +191,7 @@ func TestDevelMemHttpd(t *testing.T) {
{
srv.Config.Devel = true
hs := NewHTTPServer("/", srv)
hs := NewHTTPServer("/", srv.MothServer)
if r := hs.TestRequest("/mothballer/pategory.md", nil); r.Result().StatusCode != 500 {
t.Log(r.Body.String())

View File

@ -9,10 +9,14 @@ import (
const TestTeamID = "teamID"
type TestServer struct {
*MothServer
}
// NewTestServer creates a new MothServer with NewTestMothballs and some initial state.
//
// See function definition for details.
func NewTestServer() *MothServer {
func NewTestServer() TestServer {
puzzles := NewTestMothballs()
puzzles.refresh()
@ -24,7 +28,14 @@ func NewTestServer() *MothServer {
theme := NewTestTheme()
afero.WriteFile(theme.Fs, "/index.html", []byte("index.html"), 0644)
return NewMothServer(Configuration{}, theme, state, puzzles)
return TestServer{NewMothServer(Configuration{}, theme, state, puzzles)}
}
func (ts TestServer) refresh() {
ts.State.(*State).refresh()
for _, pp := range ts.PuzzleProviders {
pp.(*Mothballs).refresh()
}
}
func TestDevelServer(t *testing.T) {
@ -49,7 +60,6 @@ func TestProdServer(t *testing.T) {
teamID := TestTeamID
server := NewTestServer()
state := server.State.(*State)
handler := server.NewHandler(participantID, teamID)
anonHandler := server.NewHandler("badParticipantId", "badTeamId")
@ -81,7 +91,7 @@ func TestProdServer(t *testing.T) {
t.Error("index.html wrong contents", contents)
}
state.refresh()
server.refresh()
{
es := handler.ExportState()
@ -134,7 +144,7 @@ func TestProdServer(t *testing.T) {
t.Error("Right answer marked wrong", err)
}
state.refresh()
server.refresh()
{
es := handler.ExportState()
@ -163,7 +173,7 @@ func TestProdServer(t *testing.T) {
t.Error("Right answer marked wrong:", err)
}
state.refresh()
server.refresh()
{
es := anonHandler.ExportState()