sse_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // Copyright 2014 Manu Martinez-Almeida. All rights reserved.
  2. // Use of this source code is governed by a MIT style
  3. // license that can be found in the LICENSE file.
  4. package sse
  5. import (
  6. "bytes"
  7. "net/http/httptest"
  8. "testing"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func TestEncodeOnlyData(t *testing.T) {
  12. w := new(bytes.Buffer)
  13. event := Event{
  14. Data: "junk\n\njk\nid:fake",
  15. }
  16. err := Encode(w, event)
  17. assert.NoError(t, err)
  18. assert.Equal(t, w.String(),
  19. `data:junk
  20. data:
  21. data:jk
  22. data:id:fake
  23. `)
  24. decoded, _ := Decode(w)
  25. assert.Equal(t, "message", decoded[0].Event)
  26. assert.Equal(t, decoded[0].Data, []Event{event}[0].Data)
  27. }
  28. func TestEncodeWithEvent(t *testing.T) {
  29. w := new(bytes.Buffer)
  30. event := Event{
  31. Event: "t\n:<>\r\test",
  32. Data: "junk\n\njk\nid:fake",
  33. }
  34. err := Encode(w, event)
  35. assert.NoError(t, err)
  36. assert.Equal(t, w.String(),
  37. `event:t\n:<>\r est
  38. data:junk
  39. data:
  40. data:jk
  41. data:id:fake
  42. `)
  43. decoded, _ := Decode(w)
  44. assert.Equal(t, "t\\n:<>\\r\test", decoded[0].Event)
  45. assert.Equal(t, decoded[0].Data, []Event{event}[0].Data)
  46. }
  47. func TestEncodeWithId(t *testing.T) {
  48. w := new(bytes.Buffer)
  49. err := Encode(w, Event{
  50. Id: "t\n:<>\r\test",
  51. Data: "junk\n\njk\nid:fa\rke",
  52. })
  53. assert.NoError(t, err)
  54. assert.Equal(t, w.String(),
  55. `id:t\n:<>\r est
  56. data:junk
  57. data:
  58. data:jk
  59. data:id:fa\rke
  60. `)
  61. }
  62. func TestEncodeWithRetry(t *testing.T) {
  63. w := new(bytes.Buffer)
  64. err := Encode(w, Event{
  65. Retry: 11,
  66. Data: "junk\n\njk\nid:fake\n",
  67. })
  68. assert.NoError(t, err)
  69. assert.Equal(t, w.String(),
  70. `retry:11
  71. data:junk
  72. data:
  73. data:jk
  74. data:id:fake
  75. data:
  76. `)
  77. }
  78. func TestEncodeWithEverything(t *testing.T) {
  79. w := new(bytes.Buffer)
  80. err := Encode(w, Event{
  81. Event: "abc",
  82. Id: "12345",
  83. Retry: 10,
  84. Data: "some data",
  85. })
  86. assert.NoError(t, err)
  87. assert.Equal(t, w.String(), "id:12345\nevent:abc\nretry:10\ndata:some data\n\n")
  88. }
  89. func TestEncodeMap(t *testing.T) {
  90. w := new(bytes.Buffer)
  91. err := Encode(w, Event{
  92. Event: "a map",
  93. Data: map[string]interface{}{
  94. "foo": "b\n\rar",
  95. "bar": "id: 2",
  96. },
  97. })
  98. assert.NoError(t, err)
  99. assert.Equal(t, w.String(), "event:a map\ndata:{\"bar\":\"id: 2\",\"foo\":\"b\\n\\rar\"}\n\n")
  100. }
  101. func TestEncodeSlice(t *testing.T) {
  102. w := new(bytes.Buffer)
  103. err := Encode(w, Event{
  104. Event: "a slice",
  105. Data: []interface{}{1, "text", map[string]interface{}{"foo": "bar"}},
  106. })
  107. assert.NoError(t, err)
  108. assert.Equal(t, w.String(), "event:a slice\ndata:[1,\"text\",{\"foo\":\"bar\"}]\n\n")
  109. }
  110. func TestEncodeStruct(t *testing.T) {
  111. myStruct := struct {
  112. A int
  113. B string `json:"value"`
  114. }{1, "number"}
  115. w := new(bytes.Buffer)
  116. err := Encode(w, Event{
  117. Event: "a struct",
  118. Data: myStruct,
  119. })
  120. assert.NoError(t, err)
  121. assert.Equal(t, w.String(), "event:a struct\ndata:{\"A\":1,\"value\":\"number\"}\n\n")
  122. w.Reset()
  123. err = Encode(w, Event{
  124. Event: "a struct",
  125. Data: &myStruct,
  126. })
  127. assert.NoError(t, err)
  128. assert.Equal(t, w.String(), "event:a struct\ndata:{\"A\":1,\"value\":\"number\"}\n\n")
  129. }
  130. func TestEncodeInteger(t *testing.T) {
  131. w := new(bytes.Buffer)
  132. err := Encode(w, Event{
  133. Event: "an integer",
  134. Data: 1,
  135. })
  136. assert.NoError(t, err)
  137. assert.Equal(t, w.String(), "event:an integer\ndata:1\n\n")
  138. }
  139. func TestEncodeFloat(t *testing.T) {
  140. w := new(bytes.Buffer)
  141. err := Encode(w, Event{
  142. Event: "Float",
  143. Data: 1.5,
  144. })
  145. assert.NoError(t, err)
  146. assert.Equal(t, w.String(), "event:Float\ndata:1.5\n\n")
  147. }
  148. func TestEncodeStream(t *testing.T) {
  149. w := new(bytes.Buffer)
  150. Encode(w, Event{
  151. Event: "float",
  152. Data: 1.5,
  153. })
  154. Encode(w, Event{
  155. Id: "123",
  156. Data: map[string]interface{}{"foo": "bar", "bar": "foo"},
  157. })
  158. Encode(w, Event{
  159. Id: "124",
  160. Event: "chat",
  161. Data: "hi! dude",
  162. })
  163. assert.Equal(t, w.String(), "event:float\ndata:1.5\n\nid:123\ndata:{\"bar\":\"foo\",\"foo\":\"bar\"}\n\nid:124\nevent:chat\ndata:hi! dude\n\n")
  164. }
  165. func TestRenderSSE(t *testing.T) {
  166. w := httptest.NewRecorder()
  167. err := (Event{
  168. Event: "msg",
  169. Data: "hi! how are you?",
  170. }).Render(w)
  171. assert.NoError(t, err)
  172. assert.Equal(t, w.Body.String(), "event:msg\ndata:hi! how are you?\n\n")
  173. assert.Equal(t, w.Header().Get("Content-Type"), "text/event-stream")
  174. assert.Equal(t, w.Header().Get("Cache-Control"), "no-cache")
  175. }
  176. func BenchmarkResponseWriter(b *testing.B) {
  177. w := httptest.NewRecorder()
  178. b.ResetTimer()
  179. b.ReportAllocs()
  180. for i := 0; i < b.N; i++ {
  181. (Event{
  182. Event: "new_message",
  183. Data: "hi! how are you? I am fine. this is a long stupid message!!!",
  184. }).Render(w)
  185. }
  186. }
  187. func BenchmarkFullSSE(b *testing.B) {
  188. buf := new(bytes.Buffer)
  189. b.ResetTimer()
  190. b.ReportAllocs()
  191. for i := 0; i < b.N; i++ {
  192. Encode(buf, Event{
  193. Event: "new_message",
  194. Id: "13435",
  195. Retry: 10,
  196. Data: "hi! how are you? I am fine. this is a long stupid message!!!",
  197. })
  198. buf.Reset()
  199. }
  200. }
  201. func BenchmarkNoRetrySSE(b *testing.B) {
  202. buf := new(bytes.Buffer)
  203. b.ResetTimer()
  204. b.ReportAllocs()
  205. for i := 0; i < b.N; i++ {
  206. Encode(buf, Event{
  207. Event: "new_message",
  208. Id: "13435",
  209. Data: "hi! how are you? I am fine. this is a long stupid message!!!",
  210. })
  211. buf.Reset()
  212. }
  213. }
  214. func BenchmarkSimpleSSE(b *testing.B) {
  215. buf := new(bytes.Buffer)
  216. b.ResetTimer()
  217. b.ReportAllocs()
  218. for i := 0; i < b.N; i++ {
  219. Encode(buf, Event{
  220. Event: "new_message",
  221. Data: "hi! how are you? I am fine. this is a long stupid message!!!",
  222. })
  223. buf.Reset()
  224. }
  225. }