diff --git a/go.sum b/go.sum index 603439f..34e16eb 100644 --- a/go.sum +++ b/go.sum @@ -11,6 +11,7 @@ github.com/stretchr/testify v1.6.0 h1:jlIyCplCJFULU/01vCkhKuTyc3OorI3bJFuw6obfgh github.com/stretchr/testify v1.6.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= golang.org/x/net v0.0.0-20181220203305-927f97764cc3 h1:eH6Eip3UpmR+yM/qI9Ijluzb1bNv/cAU/n+6l8tRSis= golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/gopher.go b/gopher.go index bf6a491..9e78db7 100644 --- a/gopher.go +++ b/gopher.go @@ -646,8 +646,7 @@ func (s *Server) Serve(l net.Listener) error { for { rw, err := l.Accept() if err != nil { - fmt.Errorf("error acceptig new client: %s", err) - return err + return fmt.Errorf("error accepting new client: %s", err) } c := s.newConn(rw) diff --git a/gopher_test.go b/gopher_test.go index a0a6f62..c158c3b 100644 --- a/gopher_test.go +++ b/gopher_test.go @@ -5,15 +5,67 @@ import ( "log" "os" "testing" + "time" "github.com/prologic/go-gopher" "github.com/stretchr/testify/assert" ) +func TestMain(m *testing.M) { + ch := startTestServer() + defer stopTestServer(ch) + + // Because it can take some time for the server to spin up + // the tests are inconsistent - they'll fail if the server isn't + // ready, but pass otherwise. This problem seems more pronounced + // when running via the makefile. + // + // It seems like there should be a better way to do this + for attempts := 3; attempts > 0; attempts-- { + _, err := gopher.Get("gopher://localhost:7000") + if err == nil { + fmt.Println("Server ready") + break + } + fmt.Printf("Server not ready, going to try again in a sec. %v", err) + time.Sleep(1 * time.Millisecond) + } + ///// + + code := m.Run() + os.Exit(code) +} + func hello(w gopher.ResponseWriter, r *gopher.Request) { w.WriteInfo("Hello World!") } +func startTestServer() chan bool { + quit := make(chan bool) + go func() { + for { + select { + case <-quit: + return + default: + gopher.Handle("/", gopher.FileServer(gopher.Dir("./testdata"))) + gopher.HandleFunc("/hello", hello) + log.Println("Test server starting on 7000") + err := gopher.ListenAndServe("localhost:7000", nil) + if err != nil { + log.Fatal(err) + } + } + } + }() + + return quit +} + +func stopTestServer(c chan bool) { + c <- true +} + func Example_client() { res, err := gopher.Get("gopher://gopher.floodgap.com") if err != nil { @@ -57,7 +109,9 @@ func TestFileServer(t *testing.T) { assert.Nil(err) log.Println(string(json)) - assert.JSONEq(string(json), `{"items":[{"type":"0","description":"hello.txt","selector":"hello.txt","host":"127.0.0.1","port":7000,"extras":[]}]}`) + assert.JSONEq( + `{"items":[{"type":"0","description":"hello.txt","selector":"/hello.txt","host":"127.0.0.1","port":7000,"extras":[]}]}`, + string(json)) } func TestParseItemNull(t *testing.T) { @@ -121,13 +175,3 @@ func TestParseItemMarshalIdempotency(t *testing.T) { assert.NotNil(item1) assert.Equal(item, item1) } - -func TestMain(m *testing.M) { - gopher.Handle("/", gopher.FileServer(gopher.Dir("./testdata"))) - gopher.HandleFunc("/hello", hello) - go func() { - log.Fatal(gopher.ListenAndServe("localhost:7000", nil)) - }() - - os.Exit(m.Run()) -} diff --git a/testdata/gophermap b/testdata/gophermap new file mode 100644 index 0000000..db2a012 --- /dev/null +++ b/testdata/gophermap @@ -0,0 +1,2 @@ + +0hello.txt /hello.txt 127.0.0.1 7000