MCPcopy
hub / github.com/nats-io/nats.go / TestGetMetadataFields

Function TestGetMetadataFields

internal/parser/parse_test.go:103–161  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

101}
102
103func TestGetMetadataFields(t *testing.T) {
104 tests := []struct {
105 name string
106 subject string
107 expected []string
108 withError error
109 }{
110 {
111 name: "parse v2 tokens",
112 subject: "$JS.ACK.domain.hash-123.stream.cons.100.200.150.123456789.100.token",
113 expected: []string{"$JS", "ACK", "domain", "hash-123", "stream", "cons", "100", "200", "150", "123456789", "100", "token"},
114 },
115 {
116 name: "parse v2 tokens with underscore domain",
117 subject: "$JS.ACK._.hash-123.stream.cons.100.200.150.123456789.100.token",
118 expected: []string{"$JS", "ACK", "", "hash-123", "stream", "cons", "100", "200", "150", "123456789", "100", "token"},
119 },
120 {
121 name: "parse v1 tokens",
122 subject: "$JS.ACK.stream.cons.100.200.150.123456789.100",
123 expected: []string{"$JS", "ACK", "", "", "stream", "cons", "100", "200", "150", "123456789", "100"},
124 },
125 {
126 name: "invalid start of subject",
127 subject: "$ABC.123.stream.cons.100.200.150.123456789.100",
128 withError: ErrInvalidSubjectFormat,
129 },
130 {
131 name: "invalid subject length (10)",
132 subject: "$JS.ACK.stream.cons.100.200.150.123456789.100.ABC",
133 withError: ErrInvalidSubjectFormat,
134 },
135 {
136 name: "invalid subject length (5)",
137 subject: "$JS.ACK.stream.cons.100",
138 withError: ErrInvalidSubjectFormat,
139 },
140 {
141 name: "invalid subject ",
142 subject: "",
143 withError: ErrInvalidSubjectFormat,
144 },
145 }
146
147 for _, test := range tests {
148 t.Run(test.name, func(t *testing.T) {
149 res, err := GetMetadataFields(test.subject)
150 if test.withError != nil {
151 if err == nil || !errors.Is(err, test.withError) {
152 t.Fatalf("Expected error: %v; got: %v", test.withError, err)
153 }
154 return
155 }
156 if !reflect.DeepEqual(test.expected, res) {
157 t.Fatalf("Invalid result; want: %v; got: %v", test.expected, res)
158 }
159 })
160 }

Callers

nothing calls this directly

Calls 3

GetMetadataFieldsFunction · 0.85
FatalfMethod · 0.80
IsMethod · 0.45

Tested by

no test coverage detected