Tests, client tests WIP

This commit is contained in:
Philipp Heckel 2021-12-22 14:17:50 +01:00
parent 68d881291c
commit 6a7e9071b6
7 changed files with 104 additions and 30 deletions

42
client/client_test.go Normal file
View file

@ -0,0 +1,42 @@
package client_test
import (
"github.com/stretchr/testify/require"
"heckel.io/ntfy/client"
"heckel.io/ntfy/server"
"net/http"
"testing"
"time"
)
func TestClient_Publish(t *testing.T) {
s := startTestServer(t)
defer s.Stop()
c := client.New(newTestConfig())
time.Sleep(time.Second) // FIXME Wait for port up
_, err := c.Publish("mytopic", "some message")
require.Nil(t, err)
}
func newTestConfig() *client.Config {
c := client.NewConfig()
c.DefaultHost = "http://127.0.0.1:12345"
return c
}
func startTestServer(t *testing.T) *server.Server {
conf := server.NewConfig()
conf.ListenHTTP = ":12345"
s, err := server.New(conf)
if err != nil {
t.Fatal(err)
}
go func() {
if err := s.Run(); err != nil && err != http.ErrServerClosed {
panic(err) // 'go vet' complains about 't.Fatal(err)'
}
}()
return s
}

View file

@ -1,7 +1,8 @@
package client
package client_test
import (
"github.com/stretchr/testify/require"
"heckel.io/ntfy/client"
"os"
"path/filepath"
"testing"
@ -21,7 +22,7 @@ subscribe:
priority: high,urgent
`), 0600))
conf, err := LoadConfig(filename)
conf, err := client.LoadConfig(filename)
require.Nil(t, err)
require.Equal(t, "http://localhost", conf.DefaultHost)
require.Equal(t, 3, len(conf.Subscribe))