github.com/blend/go-sdk@v1.20220411.3/webutil/event_source_test.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package webutil 9 10 import ( 11 "bytes" 12 "net/http" 13 "testing" 14 15 "github.com/blend/go-sdk/assert" 16 ) 17 18 func TestEventSourceStartSession(t *testing.T) { 19 assert := assert.New(t) 20 21 buffer := new(bytes.Buffer) 22 rw := NewMockResponse(buffer) 23 es := NewEventSource(rw) 24 err := es.StartSession() 25 assert.Nil(err) 26 27 assert.Equal(http.StatusOK, rw.StatusCode()) 28 assert.Equal("text/event-stream", rw.Header().Get(HeaderContentType)) 29 assert.Equal("Content-Type", rw.Header().Get(HeaderVary)) 30 assert.Equal("event: ping\n\n", buffer.String()) 31 } 32 33 func TestEventSourcePing(t *testing.T) { 34 assert := assert.New(t) 35 36 buffer := new(bytes.Buffer) 37 rw := NewMockResponse(buffer) 38 es := NewEventSource(rw) 39 assert.Nil(es.Ping()) 40 assert.Equal("event: ping\n\n", buffer.String()) 41 } 42 43 func TestEventSourceEvent(t *testing.T) { 44 assert := assert.New(t) 45 46 buffer := new(bytes.Buffer) 47 rw := NewMockResponse(buffer) 48 es := NewEventSource(rw) 49 assert.Nil(es.Event("test event")) 50 assert.Equal("event: test event\n\n", buffer.String()) 51 } 52 53 func TestEventSourceData(t *testing.T) { 54 assert := assert.New(t) 55 56 buffer := new(bytes.Buffer) 57 rw := NewMockResponse(buffer) 58 es := NewEventSource(rw) 59 assert.Nil(es.Data("test event data")) 60 assert.Equal("data: test event data\n\n", buffer.String()) 61 } 62 63 func TestEventSourceDataLines(t *testing.T) { 64 assert := assert.New(t) 65 66 buffer := new(bytes.Buffer) 67 rw := NewMockResponse(buffer) 68 es := NewEventSource(rw) 69 assert.Nil(es.Data("test event data one\ntest event data two\n")) 70 assert.Equal("data: test event data one\ndata: test event data two\n\n", buffer.String()) 71 } 72 73 func TestEventSourceEventData(t *testing.T) { 74 assert := assert.New(t) 75 76 buffer := new(bytes.Buffer) 77 rw := NewMockResponse(buffer) 78 es := NewEventSource(rw) 79 assert.Nil(es.EventData("test event", "test event data")) 80 assert.Equal("event: test event\ndata: test event data\n\n", buffer.String()) 81 } 82 83 func TestEventSourceEventDataLines(t *testing.T) { 84 assert := assert.New(t) 85 86 buffer := new(bytes.Buffer) 87 rw := NewMockResponse(buffer) 88 es := NewEventSource(rw) 89 assert.Nil(es.EventData("test event", "test event data one\ntest event data two\n")) 90 assert.Equal("event: test event\ndata: test event data one\ndata: test event data two\n\n", buffer.String()) 91 }