Skip to content

Commit

Permalink
fix: check on missing topic & test cases for payload contents
Browse files Browse the repository at this point in the history
  • Loading branch information
TechSolomon committed Jul 16, 2024
1 parent e39a7d7 commit fa89727
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
5 changes: 5 additions & 0 deletions utility/republisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ func OutboundMessageFlow(server string, port int, prefix string, destination str

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

if msg.Topic == "" {
fmt.Println(">> [!] Error extracting the message topic.")
continue
}

prepend := prefix + "/" + msg.Topic
repackaged := RepackageContents(message, prepend)
PublishPayload(server, port, prepend, repackaged)
Expand Down
34 changes: 34 additions & 0 deletions utility/republisher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,37 @@ func TestDetectContents(t *testing.T) {
}
}
}

func TestRepackageContents(t *testing.T) {
testcases := []struct {
input string
topic string
output string
}{
{"{\"time\": 1234567890123456, \"topic\": \"test-topic\", \"b64payload\": \"ZGF0YS1kaW9kZQ==\"}", "test-topic", "data-diode"},
}

for _, test := range testcases {
actual := RepackageContents(test.input, test.topic)
expected := test.output

if actual != expected {
t.Errorf("Expected %s but got %s", expected, actual)
}
}
}

func TestPublishPayload(t *testing.T) {
testcases := []struct {
server string
port int
topic string
message string
}{
{"localhost", 1883, "test-topic", "data-diode"},
}

for _, test := range testcases {
PublishPayload(test.server, test.port, test.topic, test.message)
}
}

0 comments on commit fa89727

Please sign in to comment.