mirror of
https://github.com/binwiederhier/ntfy.git
synced 2024-11-26 05:09:16 +01:00
Open and keepalive tests
This commit is contained in:
parent
94b70fbcb9
commit
fc16b0531a
1 changed files with 37 additions and 14 deletions
|
@ -2,6 +2,7 @@ package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"heckel.io/ntfy/config"
|
"heckel.io/ntfy/config"
|
||||||
|
@ -10,6 +11,7 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestServer_PublishAndPoll(t *testing.T) {
|
func TestServer_PublishAndPoll(t *testing.T) {
|
||||||
|
@ -29,25 +31,46 @@ func TestServer_PublishAndPoll(t *testing.T) {
|
||||||
response := request(t, s, "GET", "/mytopic/json?poll=1", "")
|
response := request(t, s, "GET", "/mytopic/json?poll=1", "")
|
||||||
messages := toMessages(t, response.Body.String())
|
messages := toMessages(t, response.Body.String())
|
||||||
assert.Equal(t, 2, len(messages))
|
assert.Equal(t, 2, len(messages))
|
||||||
|
assert.Equal(t, "my first message", messages[0].Message)
|
||||||
|
assert.Equal(t, "my second message", messages[1].Message)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestServer_PublishAndSubscribe(t *testing.T) {
|
func TestServer_SubscribeOpenAndKeepalive(t *testing.T) {
|
||||||
s := newTestServer(t, newTestConfig(t))
|
c := newTestConfig(t)
|
||||||
|
c.KeepaliveInterval = time.Second
|
||||||
|
s := newTestServer(t, c)
|
||||||
|
|
||||||
response1 := request(t, s, "PUT", "/mytopic", "my first message")
|
rr := httptest.NewRecorder()
|
||||||
msg1 := toMessage(t, response1.Body.String())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
assert.NotEmpty(t, msg1.ID)
|
req, err := http.NewRequestWithContext(ctx, "GET", "/mytopic/json", nil)
|
||||||
assert.Equal(t, "my first message", msg1.Message)
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
doneChan := make(chan bool)
|
||||||
|
go func() {
|
||||||
|
s.handle(rr, req)
|
||||||
|
doneChan <- true
|
||||||
|
}()
|
||||||
|
time.Sleep(1300 * time.Millisecond)
|
||||||
|
cancel()
|
||||||
|
<-doneChan
|
||||||
|
|
||||||
response2 := request(t, s, "PUT", "/mytopic", "my second message")
|
messages := toMessages(t, rr.Body.String())
|
||||||
msg2 := toMessage(t, response2.Body.String())
|
|
||||||
assert.NotEqual(t, msg1.ID, msg2.ID)
|
|
||||||
assert.NotEmpty(t, msg2.ID)
|
|
||||||
assert.Equal(t, "my second message", msg2.Message)
|
|
||||||
|
|
||||||
response := request(t, s, "GET", "/mytopic/json?poll=1", "")
|
|
||||||
messages := toMessages(t, response.Body.String())
|
|
||||||
assert.Equal(t, 2, len(messages))
|
assert.Equal(t, 2, len(messages))
|
||||||
|
|
||||||
|
assert.Equal(t, openEvent, messages[0].Event)
|
||||||
|
assert.Equal(t, "mytopic", messages[0].Topic)
|
||||||
|
assert.Equal(t, "", messages[0].Message)
|
||||||
|
assert.Equal(t, "", messages[0].Title)
|
||||||
|
assert.Equal(t, 0, messages[0].Priority)
|
||||||
|
assert.Nil(t, messages[0].Tags)
|
||||||
|
|
||||||
|
assert.Equal(t, keepaliveEvent, messages[1].Event)
|
||||||
|
assert.Equal(t, "mytopic", messages[1].Topic)
|
||||||
|
assert.Equal(t, "", messages[1].Message)
|
||||||
|
assert.Equal(t, "", messages[1].Title)
|
||||||
|
assert.Equal(t, 0, messages[1].Priority)
|
||||||
|
assert.Nil(t, messages[1].Tags)
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTestConfig(t *testing.T) *config.Config {
|
func newTestConfig(t *testing.T) *config.Config {
|
||||||
|
|
Loading…
Reference in a new issue