Debian Codegraph @

amqptest/server/utils_test.go

raw
1 package server
2
3 import (
4 "testing"
5
6 "github.com/NeowayLabs/wabbit"
7 )
8
9 func matchSuccess(t *testing.T, b, r string, expected bool) {
10 res := topicMatch(b, r)
11
12 if res != expected {
13 t.Errorf("route '%s' and bind '%s' returns %v", b, r, res)
14 }
15 }
16
17 func TestTopicMatch(t *testing.T) {
18 for _, pair := range []struct {
19 b, r string
20 ok bool
21 }{
22 // OK
23 {"a", "a", true},
24 {"ab", "ab", true},
25 {"#", "a", true},
26 {"#", "aa", true},
27 {"#", "aaaaaaaaaaaaaaaaaaaaaaaaa", true},
28 {"#.a", "bbbb.a", true},
29 {"teste#", "testeb", true},
30 {"teste#", "testebbbbbbbbbbb", true},
31 {"*.*", "a.b", true},
32 {"*a", "aa", true},
33 {"*aa", "baa", true},
34 {"a*.b*", "ab.ba", true},
35 {"maps.layer.stored", "maps.layer.stored", true},
36 {"maps.layer.#", "maps.layer.bleh", true},
37
38 // FAIL
39 {"", "a", false},
40 {"a", "b", false},
41 {"*", "aa", false},
42 {"a*", "aaa", false},
43 {"maps.layer.*", "maps.layer.stored", false},
44 } {
45 matchSuccess(t, pair.b, pair.r, pair.ok)
46 }
47 }
48
49 func TestHeadersMatch(t *testing.T) {
50 q := NewQueue("test")
51 for _, tt := range []struct {
52 name string
53 b BindingsMap
54 d Delivery
55 expected, ok bool
56 }{
57 // OK
58 {"no headers exchange nor message", BindingsMap{queue: q, headers: map[string]string{}}, Delivery{headers: wabbit.Option{}}, true, true},
59 {"no headers exchange headers in message", BindingsMap{queue: q, headers: map[string]string{}}, Delivery{headers: wabbit.Option{"test": "test"}}, true, true},
60 {"just x-match header set to all in exchange no headers in message", BindingsMap{queue: q, headers: map[string]string{"x-match": "all"}}, Delivery{headers: wabbit.Option{}}, true, true},
61 {"all headers in exchange and message", BindingsMap{queue: q, headers: map[string]string{"x-match": "all", "test": "test"}}, Delivery{headers: wabbit.Option{"test": "test"}}, true, true},
62 {"ignoring all \"x-\" prefixed headers", BindingsMap{queue: q, headers: map[string]string{"x-match": "all", "x-test": "test"}}, Delivery{headers: wabbit.Option{"x-test": "test"}}, true, true},
63 {"any headers in exchange and message", BindingsMap{queue: q, headers: map[string]string{"x-match": "any", "test": "test", "test2": "test"}}, Delivery{headers: wabbit.Option{"test": "test"}}, true, true},
64
65 // FAIL
66 {"wrong x-match header value in bindings", BindingsMap{queue: q, headers: map[string]string{"x-match": "test"}}, Delivery{headers: wabbit.Option{}}, false, false},
67 {"just x-match header set to any in exchange no headers message message", BindingsMap{queue: q, headers: map[string]string{"x-match": "any"}}, Delivery{headers: wabbit.Option{}}, false, true},
68 {"all headers in exchange no headers in message", BindingsMap{queue: q, headers: map[string]string{"x-match": "all", "test": "test"}}, Delivery{headers: wabbit.Option{}}, false, true},
69 {"all headers in exchange some headers in message", BindingsMap{queue: q, headers: map[string]string{"x-match": "all", "test": "test", "test2": "test"}}, Delivery{headers: wabbit.Option{"test": "test"}}, false, true},
70 {"any headers in exchange and no match in message", BindingsMap{queue: q, headers: map[string]string{"x-match": "any", "test": "test"}}, Delivery{headers: wabbit.Option{"test2": "test"}}, false, true},
71 {"ignoring any \"x-\" prefixed headers", BindingsMap{queue: q, headers: map[string]string{"x-match": "any", "x-test": "test"}}, Delivery{headers: wabbit.Option{"x-test": "test"}}, false, true},
72 {"any headers in exchange and no match in message", BindingsMap{queue: q, headers: map[string]string{"x-match": "any", "test": "test"}}, Delivery{headers: wabbit.Option{"test2": "test"}}, false, true},
73 } {
74 t.Run(tt.name, func(t *testing.T) {
75 tt := tt
76 t.Parallel()
77 res, err := headersMatch(&tt.b, &tt.d)
78
79 if !tt.ok && err == nil {
80 t.Errorf("expected error but got none")
81 }
82
83 if res != tt.expected {
84 t.Errorf("expected '%v' got '%v'", tt.expected, res)
85 }
86 })
87 }
88 }