github.com/OpsMx/go-app-base@v0.0.24/sse/sse_test.go (about)

     1  // Copyright 2023 OpsMx, Inc
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package sse
    16  
    17  import (
    18  	"bytes"
    19  	"reflect"
    20  	"strings"
    21  	"testing"
    22  )
    23  
    24  func TestSSE_Read(t *testing.T) {
    25  	tests := []struct {
    26  		name    string
    27  		input   string
    28  		want    Event
    29  		wantEOF bool
    30  	}{
    31  		{
    32  			"Nothing but colons",
    33  			":\n",
    34  			Event{},
    35  			false,
    36  		},
    37  		{
    38  			"EOF",
    39  			"",
    40  			Event{},
    41  			true,
    42  		},
    43  		{
    44  			"data with data",
    45  			"data: foo\n\n",
    46  			Event{"data": "foo"},
    47  			false,
    48  		},
    49  		{
    50  			"data with data and colons",
    51  			"data: foo\n:\n:\n\n",
    52  			Event{"data": "foo"},
    53  			false,
    54  		},
    55  		{
    56  			"multi-line data",
    57  			"data: foo\ndata: bar\n\n",
    58  			Event{"data": "foo\nbar"},
    59  			false,
    60  		},
    61  	}
    62  	for _, tt := range tests {
    63  		t.Run(tt.name, func(t *testing.T) {
    64  			sse := NewSSE(strings.NewReader(tt.input))
    65  			got, gotEOF := sse.Read()
    66  			if !reflect.DeepEqual(got, tt.want) {
    67  				t.Errorf("SSE.Read() = %v, want %v", got, tt.want)
    68  			}
    69  			if gotEOF != tt.wantEOF {
    70  				t.Errorf("SSE.Read() EOF == %v, want %v", gotEOF, tt.wantEOF)
    71  			}
    72  		})
    73  	}
    74  }
    75  
    76  func TestSSE_KeepAlive(t *testing.T) {
    77  	tests := []struct {
    78  		name    string
    79  		wantW   string
    80  		wantErr bool
    81  	}{
    82  		{
    83  			"works",
    84  			":\n",
    85  			false,
    86  		},
    87  	}
    88  	for _, tt := range tests {
    89  		t.Run(tt.name, func(t *testing.T) {
    90  			sse := NewSSE(strings.NewReader(""))
    91  			w := &bytes.Buffer{}
    92  			if err := sse.KeepAlive(w); (err != nil) != tt.wantErr {
    93  				t.Errorf("SSE.KeepAlive() error = %v, wantErr %v", err, tt.wantErr)
    94  				return
    95  			}
    96  			if gotW := w.String(); gotW != tt.wantW {
    97  				t.Errorf("SSE.KeepAlive() = %v, want %v", gotW, tt.wantW)
    98  			}
    99  		})
   100  	}
   101  }
   102  
   103  func TestSSE_Write(t *testing.T) {
   104  	tests := []struct {
   105  		name    string
   106  		event   Event
   107  		wantW   string
   108  		wantErr bool
   109  	}{
   110  		{
   111  			"empty event",
   112  			Event{},
   113  			"",
   114  			false,
   115  		},
   116  		{
   117  			"data only",
   118  			Event{
   119  				"data": `{"foo":"bar"}`,
   120  			},
   121  			"data: {\"foo\":\"bar\"}\n\n",
   122  			false,
   123  		},
   124  		{
   125  			"multi-line data",
   126  			Event{
   127  				"data": "foo\nbar",
   128  			},
   129  			"data: foo\ndata: bar\n\n",
   130  			false,
   131  		},
   132  	}
   133  	for _, tt := range tests {
   134  		t.Run(tt.name, func(t *testing.T) {
   135  			sse := NewSSE(strings.NewReader(""))
   136  			w := &bytes.Buffer{}
   137  			if err := sse.Write(w, tt.event); (err != nil) != tt.wantErr {
   138  				t.Errorf("SSE.Write() error = %v, wantErr %v", err, tt.wantErr)
   139  				return
   140  			}
   141  			if gotW := w.String(); gotW != tt.wantW {
   142  				t.Errorf("SSE.Write() = %v, want %v", gotW, tt.wantW)
   143  			}
   144  		})
   145  	}
   146  }