github.com/Jeffail/benthos/v3@v3.65.0/lib/processor/try_test.go (about) 1 package processor 2 3 import ( 4 "reflect" 5 "testing" 6 7 "github.com/Jeffail/benthos/v3/lib/condition" 8 "github.com/Jeffail/benthos/v3/lib/log" 9 "github.com/Jeffail/benthos/v3/lib/message" 10 "github.com/Jeffail/benthos/v3/lib/metrics" 11 ) 12 13 //------------------------------------------------------------------------------ 14 15 func TestTryEmpty(t *testing.T) { 16 conf := NewConfig() 17 conf.Type = TypeTry 18 19 proc, err := New(conf, nil, log.Noop(), metrics.Noop()) 20 if err != nil { 21 t.Fatal(err) 22 } 23 24 exp := [][]byte{ 25 []byte("foo bar baz"), 26 } 27 msgs, res := proc.ProcessMessage(message.New(exp)) 28 if res != nil { 29 t.Fatal(res.Error()) 30 } 31 32 if len(msgs) != 1 { 33 t.Errorf("Wrong count of result msgs: %v", len(msgs)) 34 } 35 if act := message.GetAllBytes(msgs[0]); !reflect.DeepEqual(exp, act) { 36 t.Errorf("Wrong results: %s != %s", act, exp) 37 } 38 } 39 40 func TestTryBasic(t *testing.T) { 41 encodeConf := NewConfig() 42 encodeConf.Type = "encode" 43 encodeConf.Encode.Parts = []int{0} 44 45 conf := NewConfig() 46 conf.Type = TypeTry 47 conf.Try = append(conf.Try, encodeConf) 48 49 proc, err := New(conf, nil, log.Noop(), metrics.Noop()) 50 if err != nil { 51 t.Fatal(err) 52 } 53 54 parts := [][]byte{ 55 []byte("foo bar baz"), 56 []byte("1 2 3 4"), 57 []byte("hello foo world"), 58 } 59 exp := [][]byte{ 60 []byte("Zm9vIGJhciBiYXo="), 61 []byte("MSAyIDMgNA=="), 62 []byte("aGVsbG8gZm9vIHdvcmxk"), 63 } 64 msgs, res := proc.ProcessMessage(message.New(parts)) 65 if res != nil { 66 t.Fatal(res.Error()) 67 } 68 69 if len(msgs) != 1 { 70 t.Errorf("Wrong count of result msgs: %v", len(msgs)) 71 } 72 if act := message.GetAllBytes(msgs[0]); !reflect.DeepEqual(exp, act) { 73 t.Errorf("Wrong results: %s != %s", act, exp) 74 } 75 } 76 77 func TestTryFilterSome(t *testing.T) { 78 cond := condition.NewConfig() 79 cond.Type = "text" 80 cond.Text.Arg = "foo" 81 cond.Text.Operator = "contains" 82 83 filterConf := NewConfig() 84 filterConf.Type = "filter" 85 filterConf.Filter.Config = cond 86 87 conf := NewConfig() 88 conf.Type = TypeTry 89 conf.Try = append(conf.Try, filterConf) 90 91 proc, err := New(conf, nil, log.Noop(), metrics.Noop()) 92 if err != nil { 93 t.Fatal(err) 94 } 95 96 parts := [][]byte{ 97 []byte("foo bar baz"), 98 []byte("1 2 3 4"), 99 []byte("hello foo world"), 100 } 101 exp := [][]byte{ 102 []byte("foo bar baz"), 103 []byte("hello foo world"), 104 } 105 msgs, res := proc.ProcessMessage(message.New(parts)) 106 if res != nil { 107 t.Fatal(res.Error()) 108 } 109 110 if len(msgs) != 1 { 111 t.Errorf("Wrong count of result msgs: %v", len(msgs)) 112 } 113 if act := message.GetAllBytes(msgs[0]); !reflect.DeepEqual(exp, act) { 114 t.Errorf("Wrong results: %s != %s", act, exp) 115 } 116 } 117 118 func TestTryMultiProcs(t *testing.T) { 119 encodeConf := NewConfig() 120 encodeConf.Type = "encode" 121 encodeConf.Encode.Parts = []int{0} 122 123 cond := condition.NewConfig() 124 cond.Type = "text" 125 cond.Text.Arg = "foo" 126 cond.Text.Operator = "contains" 127 128 filterConf := NewConfig() 129 filterConf.Type = "filter" 130 filterConf.Filter.Config = cond 131 132 conf := NewConfig() 133 conf.Type = TypeTry 134 conf.Try = append(conf.Try, filterConf, encodeConf) 135 136 proc, err := New(conf, nil, log.Noop(), metrics.Noop()) 137 if err != nil { 138 t.Fatal(err) 139 } 140 141 parts := [][]byte{ 142 []byte("foo bar baz"), 143 []byte("1 2 3 4"), 144 []byte("hello foo world"), 145 } 146 exp := [][]byte{ 147 []byte("Zm9vIGJhciBiYXo="), 148 []byte("aGVsbG8gZm9vIHdvcmxk"), 149 } 150 msgs, res := proc.ProcessMessage(message.New(parts)) 151 if res != nil { 152 t.Fatal(res.Error()) 153 } 154 155 if len(msgs) != 1 { 156 t.Errorf("Wrong count of result msgs: %v", len(msgs)) 157 } 158 if act := message.GetAllBytes(msgs[0]); !reflect.DeepEqual(exp, act) { 159 t.Errorf("Wrong results: %s != %s", act, exp) 160 } 161 } 162 163 func TestTryFailJSON(t *testing.T) { 164 encodeConf := NewConfig() 165 encodeConf.Type = "encode" 166 encodeConf.Encode.Parts = []int{0} 167 168 jmespathConf := NewConfig() 169 jmespathConf.Type = TypeJMESPath 170 jmespathConf.JMESPath.Query = "foo" 171 172 conf := NewConfig() 173 conf.Type = TypeTry 174 conf.Try = append(conf.Try, jmespathConf, encodeConf) 175 176 proc, err := New(conf, nil, log.Noop(), metrics.Noop()) 177 if err != nil { 178 t.Fatal(err) 179 } 180 181 parts := [][]byte{ 182 []byte(`{"foo":{"bar":"baz"}}`), 183 []byte("NOT VALID JSON"), 184 []byte(`{"foo":{"bar":"baz2"}}`), 185 } 186 exp := [][]byte{ 187 []byte("eyJiYXIiOiJiYXoifQ=="), 188 []byte("NOT VALID JSON"), 189 []byte("eyJiYXIiOiJiYXoyIn0="), 190 } 191 msgs, res := proc.ProcessMessage(message.New(parts)) 192 if res != nil { 193 t.Fatal(res.Error()) 194 } 195 196 if len(msgs) != 1 { 197 t.Errorf("Wrong count of result msgs: %v", len(msgs)) 198 } 199 if act := message.GetAllBytes(msgs[0]); !reflect.DeepEqual(exp, act) { 200 t.Errorf("Wrong results: %s != %s", act, exp) 201 } 202 if HasFailed(msgs[0].Get(0)) { 203 t.Error("Unexpected part 0 failed flag") 204 } 205 if !HasFailed(msgs[0].Get(1)) { 206 t.Error("Unexpected part 1 failed flag") 207 } 208 if HasFailed(msgs[0].Get(2)) { 209 t.Error("Unexpected part 2 failed flag") 210 } 211 } 212 213 func TestTryFilterAll(t *testing.T) { 214 cond := condition.NewConfig() 215 cond.Type = "text" 216 cond.Text.Arg = "foo" 217 cond.Text.Operator = "contains" 218 219 filterConf := NewConfig() 220 filterConf.Type = "filter" 221 filterConf.Filter.Config = cond 222 223 conf := NewConfig() 224 conf.Type = TypeTry 225 conf.Try = append(conf.Try, filterConf) 226 227 proc, err := New(conf, nil, log.Noop(), metrics.Noop()) 228 if err != nil { 229 t.Fatal(err) 230 } 231 232 parts := [][]byte{ 233 []byte("bar baz"), 234 []byte("1 2 3 4"), 235 []byte("hello world"), 236 } 237 msgs, res := proc.ProcessMessage(message.New(parts)) 238 if res == nil { 239 t.Fatal("expected empty response") 240 } 241 if err = res.Error(); err != nil { 242 t.Error(err) 243 } 244 if len(msgs) != 0 { 245 t.Errorf("Wrong count of result msgs: %v", len(msgs)) 246 } 247 } 248 249 //------------------------------------------------------------------------------