sse-decoder_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestDecodeSingle1(t *testing.T) {
  11. events, err := Decode(bytes.NewBufferString(
  12. `data: this is a text
  13. event: message
  14. fake:
  15. id: 123456789010
  16. : we can append data
  17. : and multiple comments should not break it
  18. data: a very nice one`))
  19. assert.NoError(t, err)
  20. assert.Len(t, events, 1)
  21. assert.Equal(t, events[0].Event, "message")
  22. assert.Equal(t, events[0].Id, "123456789010")
  23. }
  24. func TestDecodeSingle2(t *testing.T) {
  25. events, err := Decode(bytes.NewBufferString(
  26. `: starting with a comment
  27. fake:
  28. data:this is a \ntext
  29. event:a message\n\n
  30. fake
  31. :and multiple comments\n should not break it\n\n
  32. id:1234567890\n10
  33. :we can append data
  34. data:a very nice one\n!
  35. `))
  36. assert.NoError(t, err)
  37. assert.Len(t, events, 1)
  38. assert.Equal(t, events[0].Event, "a message\\n\\n")
  39. assert.Equal(t, events[0].Id, "1234567890\\n10")
  40. }
  41. func TestDecodeSingle3(t *testing.T) {
  42. events, err := Decode(bytes.NewBufferString(
  43. `
  44. id:123456ABCabc789010
  45. event: message123
  46. : we can append data
  47. data:this is a text
  48. data: a very nice one
  49. data:
  50. data
  51. : ending with a comment`))
  52. assert.NoError(t, err)
  53. assert.Len(t, events, 1)
  54. assert.Equal(t, events[0].Event, "message123")
  55. assert.Equal(t, events[0].Id, "123456ABCabc789010")
  56. }
  57. func TestDecodeMulti1(t *testing.T) {
  58. events, err := Decode(bytes.NewBufferString(
  59. `
  60. id:
  61. event: weird event
  62. data:this is a text
  63. :data: this should NOT APER
  64. data: second line
  65. : a comment
  66. event: message
  67. id:123
  68. data:this is a text
  69. :data: this should NOT APER
  70. data: second line
  71. : a comment
  72. event: message
  73. id:123
  74. data:this is a text
  75. data: second line
  76. :hola
  77. data
  78. event:
  79. id`))
  80. assert.NoError(t, err)
  81. assert.Len(t, events, 3)
  82. assert.Equal(t, events[0].Event, "weird event")
  83. assert.Equal(t, events[0].Id, "")
  84. }
  85. func TestDecodeW3C(t *testing.T) {
  86. events, err := Decode(bytes.NewBufferString(
  87. `data
  88. data
  89. data
  90. data:
  91. `))
  92. assert.NoError(t, err)
  93. assert.Len(t, events, 1)
  94. }