Skip to content

Commit

Permalink
Index out of range panic upon encountering an empty string (#27)
Browse files Browse the repository at this point in the history
* Index out of range panic upon encountering an empty string

* Added unit test for the empty value case
  • Loading branch information
szaydel authored and jandelgado committed Sep 26, 2019
1 parent d417517 commit 91d87fe
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
7 changes: 5 additions & 2 deletions cmd/rabtap/json_message_formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ var (
)

// Format validates and formats a message in JSON format. The body can be a
// simple JSON object or an array of JSON objecs. If the message is not valid
// JSON, it will returned unformatted as-is.
// simple JSON object or an array of JSON objects. If the message is not valid
// JSON, it will be returned unformatted as-is.
func (s JSONMessageFormatter) Format(message rabtap.TapMessage) string {

var formatted []byte
originalMessage := strings.TrimSpace(string(message.AmqpMessage.Body))
if len(originalMessage) == 0 {
return string(message.AmqpMessage.Body)
}
if originalMessage[0] == '[' {
// try to unmarshal array to JSON objects
var arrayJSONObj []map[string]interface{}
Expand Down
10 changes: 10 additions & 0 deletions cmd/rabtap/json_message_formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,13 @@ func TestJSONFormatterValidObject(t *testing.T) {
formattedMessage := JSONMessageFormatter{}.Format(rabtap.NewTapMessage(&message, time.Now()))
assert.Equal(t, "{\n \"a\": 1\n}", formattedMessage)
}

func TestJSONFormatterEmptyValue(t *testing.T) {
// An empty buffer effectively should be returned unmodified
message := amqp.Delivery{
Body: []byte(""),
}
formattedMessage := JSONMessageFormatter{}.Format(rabtap.NewTapMessage(&message, time.Now()))
// message is expected to be returned untouched
assert.Equal(t, "", formattedMessage)
}

0 comments on commit 91d87fe

Please sign in to comment.