github.com/awesome-flow/flow@v0.0.3-0.20190918184116-508d75d68a2c/pkg/corev1alpha1/x/meta_parser_test.go (about)

     1  package x
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	core "github.com/awesome-flow/flow/pkg/corev1alpha1"
     8  	coretest "github.com/awesome-flow/flow/pkg/corev1alpha1/test"
     9  )
    10  
    11  func TestMetaParserParseMeta(t *testing.T) {
    12  	tests := []struct {
    13  		name    string
    14  		body    []byte
    15  		expmeta map[string]interface{}
    16  		expbody []byte
    17  		experr  error
    18  	}{
    19  		{
    20  			name:    "plain message",
    21  			body:    []byte("foo"),
    22  			expmeta: map[string]interface{}{},
    23  			expbody: []byte("foo"),
    24  		},
    25  		{
    26  			name: "message with correct meta",
    27  			body: []byte("foo=bar&boo=baz hello-world"),
    28  			expmeta: map[string]interface{}{
    29  				"foo": "bar",
    30  				"boo": "baz",
    31  			},
    32  			expbody: []byte("hello-world"),
    33  		},
    34  		{
    35  			name: "message with dummy meta",
    36  			body: []byte("foo bar"),
    37  			expmeta: map[string]interface{}{
    38  				"foo": "",
    39  			},
    40  			expbody: []byte("bar"),
    41  		},
    42  		{
    43  			name: "message with multiple space runes",
    44  			body: []byte("foo bar baz"),
    45  			expmeta: map[string]interface{}{
    46  				"foo": "",
    47  			},
    48  			expbody: []byte("bar baz"),
    49  		},
    50  		{
    51  			name: "message and meta separated with double space rune",
    52  			body: []byte("foo  bar"),
    53  			expmeta: map[string]interface{}{
    54  				"foo": "",
    55  			},
    56  			expbody: []byte(" bar"),
    57  		},
    58  		{
    59  			name:    "empty message",
    60  			body:    []byte{},
    61  			expmeta: map[string]interface{}{},
    62  			expbody: []byte{},
    63  		},
    64  	}
    65  
    66  	for _, testCase := range tests {
    67  		t.Run(testCase.name, func(t *testing.T) {
    68  			msg := core.NewMessage(testCase.body)
    69  			parsed, err := parseMsgMeta(msg)
    70  			if !coretest.EqErr(err, testCase.experr) {
    71  				t.Fatalf("unexpected parse error: got: %s, want: %s", err, testCase.experr)
    72  			}
    73  			if testCase.experr != nil {
    74  				return
    75  			}
    76  			if !reflect.DeepEqual(parsed.Body(), testCase.expbody) {
    77  				t.Fatalf("unexpected message body: got: %q, want: %q", parsed.Body(), testCase.expbody)
    78  			}
    79  			for _, k := range parsed.MetaKeys() {
    80  				v, _ := parsed.Meta(k)
    81  				if v.(string) != testCase.expmeta[k.(string)] {
    82  					t.Fatalf("unexpected message meta for key %q: got: %q, want: %q", k, v.(string), testCase.expmeta[k.(string)])
    83  				}
    84  				delete(testCase.expmeta, k.(string))
    85  			}
    86  			if len(testCase.expmeta) > 0 {
    87  				t.Fatalf("missing meta keys: %+v", testCase.expmeta)
    88  			}
    89  		})
    90  	}
    91  }