Skip to content

Commit fa89727

Browse files
committed
fix: check on missing topic & test cases for payload contents
1 parent e39a7d7 commit fa89727

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

utility/republisher.go

+5
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ func OutboundMessageFlow(server string, port int, prefix string, destination str
7171

7272
// TODO: Bounds checking for extracted top-level message intent.
7373

74+
if msg.Topic == "" {
75+
fmt.Println(">> [!] Error extracting the message topic.")
76+
continue
77+
}
78+
7479
prepend := prefix + "/" + msg.Topic
7580
repackaged := RepackageContents(message, prepend)
7681
PublishPayload(server, port, prepend, repackaged)

utility/republisher_test.go

+34
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,37 @@ func TestDetectContents(t *testing.T) {
4141
}
4242
}
4343
}
44+
45+
func TestRepackageContents(t *testing.T) {
46+
testcases := []struct {
47+
input string
48+
topic string
49+
output string
50+
}{
51+
{"{\"time\": 1234567890123456, \"topic\": \"test-topic\", \"b64payload\": \"ZGF0YS1kaW9kZQ==\"}", "test-topic", "data-diode"},
52+
}
53+
54+
for _, test := range testcases {
55+
actual := RepackageContents(test.input, test.topic)
56+
expected := test.output
57+
58+
if actual != expected {
59+
t.Errorf("Expected %s but got %s", expected, actual)
60+
}
61+
}
62+
}
63+
64+
func TestPublishPayload(t *testing.T) {
65+
testcases := []struct {
66+
server string
67+
port int
68+
topic string
69+
message string
70+
}{
71+
{"localhost", 1883, "test-topic", "data-diode"},
72+
}
73+
74+
for _, test := range testcases {
75+
PublishPayload(test.server, test.port, test.topic, test.message)
76+
}
77+
}

0 commit comments

Comments
 (0)