websocket_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * EliasDB
  3. *
  4. * Copyright 2016 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. */
  10. package ecal
  11. import (
  12. "bytes"
  13. "encoding/json"
  14. "fmt"
  15. "net/http"
  16. "sync"
  17. "testing"
  18. "devt.de/krotik/common/errorutil"
  19. "devt.de/krotik/common/httputil"
  20. "devt.de/krotik/ecal/engine"
  21. "github.com/gorilla/websocket"
  22. )
  23. const TESTPORT = ":9090"
  24. func TestWebsocketHandling(t *testing.T) {
  25. sockUpgrader := websocket.Upgrader{
  26. Subprotocols: []string{"ecal-sock"},
  27. ReadBufferSize: 1024,
  28. WriteBufferSize: 1024,
  29. }
  30. si := NewScriptingInterpreter("", nil)
  31. http.HandleFunc("/httpserver_test", func(w http.ResponseWriter, r *http.Request) {
  32. conn, err := sockUpgrader.Upgrade(w, r, nil)
  33. errorutil.AssertOk(err)
  34. wsconn := NewWebsocketConnection("123", conn)
  35. si.RegisterECALSock(wsconn)
  36. defer func() {
  37. si.DeregisterECALSock(wsconn)
  38. }()
  39. wc := NewWebsocketConnection("123", conn)
  40. wc.Init()
  41. data, _, err := wc.ReadData()
  42. errorutil.AssertOk(err)
  43. errorutil.AssertTrue(fmt.Sprint(data) == "map[foo:bar]", fmt.Sprint("data is:", data))
  44. // Simulate that an event is injectd and writes to the websocket
  45. event := engine.NewEvent(fmt.Sprintf("WebSocketRequest"), []string{"db", "web", "sock", "msg"},
  46. map[interface{}]interface{}{
  47. "commID": "123",
  48. "payload": "bla",
  49. "close": true,
  50. })
  51. si.HandleECALSockEvent(nil, nil, event, 0)
  52. })
  53. hs := &httputil.HTTPServer{}
  54. var wg sync.WaitGroup
  55. wg.Add(1)
  56. go hs.RunHTTPServer(TESTPORT, &wg)
  57. wg.Wait()
  58. // Server is started
  59. if hs.LastError != nil {
  60. t.Error(hs.LastError)
  61. return
  62. }
  63. queryURL := "ws://localhost" + TESTPORT + "/httpserver_test"
  64. c, _, err := websocket.DefaultDialer.Dial(queryURL, nil)
  65. if err != nil {
  66. t.Error("Could not open websocket:", err)
  67. return
  68. }
  69. _, message, err := c.ReadMessage()
  70. if msg := formatJSONString(string(message)); err != nil || msg != `{
  71. "type": "init_success",
  72. "payload": {}
  73. }` {
  74. t.Error("Unexpected response:", msg, err)
  75. return
  76. }
  77. err = c.WriteMessage(websocket.TextMessage, []byte(`{"foo":"bar"}`))
  78. if err != nil {
  79. t.Error("Could not send message:", err)
  80. return
  81. }
  82. _, message, err = c.ReadMessage()
  83. if msg := formatJSONString(string(message)); err != nil || msg != `{
  84. "commID": "123",
  85. "payload": {
  86. "close": true,
  87. "commID": "123",
  88. "payload": "bla"
  89. },
  90. "type": "data"
  91. }` {
  92. t.Error("Unexpected response:", msg, err)
  93. return
  94. }
  95. }
  96. /*
  97. formatJSONString formats a given JSON string.
  98. */
  99. func formatJSONString(str string) string {
  100. out := bytes.Buffer{}
  101. errorutil.AssertOk(json.Indent(&out, []byte(str), "", " "))
  102. return out.String()
  103. }