Skip to content

Commit

Permalink
add websocket smoke test
Browse files Browse the repository at this point in the history
  • Loading branch information
eternal-flame-AD committed Jul 27, 2023
1 parent 2142833 commit 2c01982
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion router/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"testing"

"github.com/gorilla/websocket"
"github.com/gotify/server/v2/config"
"github.com/gotify/server/v2/mode"
"github.com/gotify/server/v2/model"
Expand Down Expand Up @@ -38,9 +39,12 @@ func (s *IntegrationSuite) BeforeTest(string, string) {
var err error
s.db = testdb.NewDBWithDefaultUser(s.T())
assert.Nil(s.T(), err)

config := config.Configuration{PassStrength: 5}
config.Server.Stream.PingPeriodSeconds = 45
g, closable := Create(s.db.GormDatabase,
&model.VersionInfo{Version: "1.0.0", BuildDate: "2018-02-20-17:30:47", Commit: "asdasds"},
&config.Configuration{PassStrength: 5},
&config,
)
s.closable = closable
s.server = httptest.NewServer(g)
Expand Down Expand Up @@ -332,12 +336,30 @@ func (s *IntegrationSuite) TestSendMessage() {
json.NewDecoder(res.Body).Decode(token)
assert.Equal(s.T(), "backup-server", token.Name)

req = s.newRequest("POST", "client", `{"name": "mock-client"}`)
req.SetBasicAuth("admin", "pw")
res, err = client.Do(req)
assert.Nil(s.T(), err)
assert.Equal(s.T(), 200, res.StatusCode)
ct := &model.Client{}
json.NewDecoder(res.Body).Decode(ct)
assert.Equal(s.T(), "mock-client", ct.Name)
assert.Nil(s.T(), ct.LastSeen)

conn := s.connectWebSocket(fmt.Sprintf("stream?token=%s", ct.Token))
defer conn.Close()

req = s.newRequest("POST", "message", `{"message": "backup done", "title": "backup done"}`)
req.Header.Add("X-Gotify-Key", token.Token)
res, err = client.Do(req)
assert.Nil(s.T(), err)
assert.Equal(s.T(), 200, res.StatusCode)

msgThruWs := &model.MessageExternal{}
assert.Nil(s.T(), conn.ReadJSON(msgThruWs))
assert.Equal(s.T(), "backup done", msgThruWs.Message)
assert.Equal(s.T(), "backup done", msgThruWs.Title)

req = s.newRequest("GET", "message", "")
req.SetBasicAuth("admin", "pw")
res, err = client.Do(req)
Expand All @@ -348,10 +370,23 @@ func (s *IntegrationSuite) TestSendMessage() {
assert.Len(s.T(), msgs.Messages, 1)

msg := msgs.Messages[0]
assert.Equal(s.T(), msgThruWs, msg)
assert.Equal(s.T(), "backup done", msg.Message)
assert.Equal(s.T(), "backup done", msg.Title)
assert.Equal(s.T(), uint(1), msg.ID)
assert.Equal(s.T(), token.ID, msg.ApplicationID)

var clients []*model.Client
req = s.newRequest("GET", "client", "")
req.SetBasicAuth("admin", "pw")
res, err = client.Do(req)
assert.Nil(s.T(), err)
assert.Equal(s.T(), 200, res.StatusCode)
json.NewDecoder(res.Body).Decode(&clients)
assert.Len(s.T(), clients, 1)
assert.Equal(s.T(), "mock-client", clients[0].Name)
assert.Equal(s.T(), uint(1), clients[0].ID)
assert.NotNil(s.T(), clients[0].LastSeen)
}

func (s *IntegrationSuite) TestPluginLoadFail_expectPanic() {
Expand Down Expand Up @@ -400,6 +435,12 @@ func (s *IntegrationSuite) TestAuthentication() {
assert.Equal(s.T(), "android-client", token.Name)
}

func (s *IntegrationSuite) connectWebSocket(url string) *websocket.Conn {
ws, _, err := websocket.DefaultDialer.Dial(fmt.Sprintf("%s/%s", strings.Replace(s.server.URL, "http", "ws", 1), url), nil)
assert.Nil(s.T(), err)
return ws
}

func (s *IntegrationSuite) newRequest(method, url, body string) *http.Request {
req, err := http.NewRequest(method, fmt.Sprintf("%s/%s", s.server.URL, url), strings.NewReader(body))
req.Header.Add("Content-Type", "application/json")
Expand Down

0 comments on commit 2c01982

Please sign in to comment.