Skip to content

Commit

Permalink
Merge pull request #145 from xmidt-org/messageType-Update
Browse files Browse the repository at this point in the history
Message type update
  • Loading branch information
denopink authored Nov 15, 2023
2 parents 3460c43 + f80095b commit c62c675
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 21 deletions.
7 changes: 3 additions & 4 deletions messagetype.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package wrp

import (
"fmt"
"strconv"
)

Expand Down Expand Up @@ -97,11 +96,11 @@ func init() {
// If the value equals the friendly name of a type, e.g. "Auth" for AuthMessageType,
// that type is returned. Otherwise, the value is converted to an integer and looked up,
// with an error being returned in the event the integer value is not valid.
func StringToMessageType(value string) (MessageType, error) {
func StringToMessageType(value string) MessageType {
mt, ok := stringToMessageType[value]
if !ok {
return MessageType(-1), fmt.Errorf("invalid message type: %s", value)
return UnknownMessageType
}

return mt, nil
return mt
}
14 changes: 5 additions & 9 deletions messagetype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,25 +101,21 @@ func testStringToMessageTypeValid(t *testing.T, expected MessageType) {
expectedString = expected.String()
)

actual, err := StringToMessageType(expectedString)
actual := StringToMessageType(expectedString)
assert.Equal(expected, actual)
assert.NoError(err)

actual, err = StringToMessageType(expectedString[0 : len(expectedString)-len("MessageType")])
actual = StringToMessageType(expectedString[0 : len(expectedString)-len("MessageType")])
assert.Equal(expected, actual)
assert.NoError(err)

actual, err = StringToMessageType(strconv.Itoa(int(expected)))
actual = StringToMessageType(strconv.Itoa(int(expected)))
assert.Equal(expected, actual)
assert.NoError(err)
}

func testStringToMessageTypeInvalid(t *testing.T, invalid string) {
assert := assert.New(t)

actual, err := StringToMessageType(invalid)
assert.Equal(MessageType(-1), actual)
assert.Error(err)
actual := StringToMessageType(invalid)
assert.Equal(UnknownMessageType, actual)
}

func TestStringToMessageType(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions wrphttp/decoders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ func testDecodeRequestHeadersInvalid(t *testing.T) {

request.Header.Set(MessageTypeHeader, "askdjfa;skdjfasdf")
entity, err := DecodeRequestHeaders(context.Background(), request)
assert.Nil(entity)
assert.Error(err)
assert.Equal(wrp.UnknownMessageType, entity.Message.Type)
assert.NoError(err)
}

func TestDecodeRequestHeaders(t *testing.T) {
Expand Down
5 changes: 1 addition & 4 deletions wrphttp/headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,7 @@ func getMessageType(h http.Header) wrp.MessageType {
}
}

messageType, err := wrp.StringToMessageType(value)
if err != nil {
panic(err)
}
messageType := wrp.StringToMessageType(value)
return messageType
}

Expand Down
4 changes: 2 additions & 2 deletions wrphttp/headers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@ func testNewMessageFromHeadersBadMessageType(t *testing.T) {
assert.Error(err)

message, err = NewMessageFromHeaders(http.Header{MessageTypeHeader: []string{"this could not possibly be a valid message type"}}, nil)
assert.Nil(message)
assert.Error(err)
assert.Equal(wrp.UnknownMessageType, message.MessageType())
assert.NoError(err)
}

func testNewMessageFromHeadersBadIntHeader(t *testing.T, headerName string) {
Expand Down

0 comments on commit c62c675

Please sign in to comment.