github.com/anycable/anycable-go@v1.5.1/encoders/json_test.go (about)

     1  package encoders
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/anycable/anycable-go/common"
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestJSONEncoder(t *testing.T) {
    11  	coder := JSON{}
    12  
    13  	t.Run(".Encode", func(t *testing.T) {
    14  		msg := &common.Reply{Type: "test", Identifier: "test_channel", Message: "hello"}
    15  
    16  		expected := []byte("{\"type\":\"test\",\"identifier\":\"test_channel\",\"message\":\"hello\"}")
    17  
    18  		actual, err := coder.Encode(msg)
    19  
    20  		assert.NoError(t, err)
    21  		assert.Equal(t, expected, actual.Payload)
    22  	})
    23  
    24  	t.Run(".EncodeTransmission", func(t *testing.T) {
    25  		msg := "{\"type\":\"test\",\"identifier\":\"test_channel\",\"message\":\"hello\"}"
    26  		expected := []byte(msg)
    27  
    28  		actual, err := coder.EncodeTransmission(msg)
    29  
    30  		assert.NoError(t, err)
    31  		assert.Equal(t, expected, actual.Payload)
    32  	})
    33  
    34  	t.Run(".Decode", func(t *testing.T) {
    35  		msg := []byte("{\"command\":\"test\",\"identifier\":\"test_channel\",\"data\":\"hello\"}")
    36  
    37  		actual, err := coder.Decode(msg)
    38  
    39  		assert.NoError(t, err)
    40  		assert.Equal(t, actual.Command, "test")
    41  		assert.Equal(t, actual.Identifier, "test_channel")
    42  		assert.Equal(t, actual.Data, "hello")
    43  	})
    44  }