github.com/Jeffail/benthos/v3@v3.65.0/lib/message/io/json_test.go (about)

     1  package io
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/Jeffail/benthos/v3/lib/message"
     7  )
     8  
     9  //------------------------------------------------------------------------------
    10  
    11  func TestJSONSerialisation(t *testing.T) {
    12  	msg := message.New([][]byte{
    13  		[]byte("foo"),
    14  		[]byte(`{"bar":"baz"}`),
    15  		[]byte("qux"),
    16  	})
    17  
    18  	msg.Get(1).Metadata().Set("meta1", "val1")
    19  	msg.Get(1).Metadata().Set("meta2", "val2")
    20  	msg.Get(2).Metadata().Set("meta3", "val3")
    21  
    22  	bytes, err := MessageToJSON(msg)
    23  	if err != nil {
    24  		t.Fatal(err)
    25  	}
    26  
    27  	exp := `[{"metadata":{},"value":"foo"},{"metadata":{"meta1":"val1","meta2":"val2"},"value":"{\"bar\":\"baz\"}"},{"metadata":{"meta3":"val3"},"value":"qux"}]`
    28  
    29  	if act := string(bytes); exp != act {
    30  		t.Errorf("Wrong serialisation result: %v != %v", act, exp)
    31  	}
    32  
    33  	msg = message.New(nil)
    34  	bytes, err = MessageToJSON(msg)
    35  	if err != nil {
    36  		t.Fatal(err)
    37  	}
    38  	exp = `[]`
    39  	if act := string(bytes); exp != act {
    40  		t.Errorf("Wrong serialisation result: %v != %v", act, exp)
    41  	}
    42  
    43  	msg.Append(message.NewPart(nil))
    44  	bytes, err = MessageToJSON(msg)
    45  	if err != nil {
    46  		t.Fatal(err)
    47  	}
    48  	exp = `[{"metadata":{},"value":""}]`
    49  	if act := string(bytes); exp != act {
    50  		t.Errorf("Wrong serialisation result: %v != %v", act, exp)
    51  	}
    52  }
    53  
    54  func TestJSONBadDeserialisation(t *testing.T) {
    55  	input := `not #%@%$# valuid "" json`
    56  	if _, err := MessageFromJSON([]byte(input)); err == nil {
    57  		t.Error("Expected error")
    58  	}
    59  
    60  	input = `{"foo":"bar}`
    61  	if _, err := MessageFromJSON([]byte(input)); err == nil {
    62  		t.Error("Expected error")
    63  	}
    64  
    65  	input = `[[]]`
    66  	if _, err := MessageFromJSON([]byte(input)); err == nil {
    67  		t.Error("Expected error")
    68  	}
    69  
    70  	input = `[{"value":5}]`
    71  	if _, err := MessageFromJSON([]byte(input)); err == nil {
    72  		t.Error("Expected error")
    73  	}
    74  
    75  	input = `[{"value":"5","metadata":[]}]`
    76  	if _, err := MessageFromJSON([]byte(input)); err == nil {
    77  		t.Error("Expected error")
    78  	}
    79  }
    80  
    81  func TestJSONDeserialisation(t *testing.T) {
    82  	input := `[]`
    83  	msg, err := MessageFromJSON([]byte(input))
    84  	if err != nil {
    85  		t.Fatal(err)
    86  	}
    87  	if exp, act := 0, msg.Len(); exp != act {
    88  		t.Errorf("Wrong count of message parts: %v != %v", act, exp)
    89  	}
    90  
    91  	input = `[{"metadata":{},"value":""}]`
    92  	msg, err = MessageFromJSON([]byte(input))
    93  	if err != nil {
    94  		t.Fatal(err)
    95  	}
    96  	if exp, act := 1, msg.Len(); exp != act {
    97  		t.Errorf("Wrong count of message parts: %v != %v", act, exp)
    98  	}
    99  	if exp, act := 0, len(msg.Get(0).Get()); exp != act {
   100  		t.Errorf("Wrong size of message part: %v != %v", act, exp)
   101  	}
   102  
   103  	input = `[{"metadata":{"meta1":"value1"},"value":"foo"},{"metadata":{"meta2":"value2","meta3":"value3"},"value":"bar"}]`
   104  	msg, err = MessageFromJSON([]byte(input))
   105  	if err != nil {
   106  		t.Fatal(err)
   107  	}
   108  	if exp, act := 2, msg.Len(); exp != act {
   109  		t.Errorf("Wrong count of message parts: %v != %v", act, exp)
   110  	}
   111  	if exp, act := "foo", string(msg.Get(0).Get()); exp != act {
   112  		t.Errorf("Wrong message part value: %v != %v", act, exp)
   113  	}
   114  	if exp, act := "bar", string(msg.Get(1).Get()); exp != act {
   115  		t.Errorf("Wrong message part value: %v != %v", act, exp)
   116  	}
   117  	if exp, act := "value1", msg.Get(0).Metadata().Get("meta1"); exp != act {
   118  		t.Errorf("Wrong message part metadata value: %v != %v", act, exp)
   119  	}
   120  	if exp, act := "value2", msg.Get(1).Metadata().Get("meta2"); exp != act {
   121  		t.Errorf("Wrong message part metadata value: %v != %v", act, exp)
   122  	}
   123  	if exp, act := "value3", msg.Get(1).Metadata().Get("meta3"); exp != act {
   124  		t.Errorf("Wrong message part metadata value: %v != %v", act, exp)
   125  	}
   126  }
   127  
   128  //------------------------------------------------------------------------------