...

Source file src/github.com/gin-contrib/sse/sse_test.go

Documentation: github.com/gin-contrib/sse

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

View as plain text