Skip to content

Commit

Permalink
Add raw output option
Browse files Browse the repository at this point in the history
  • Loading branch information
muzzammilshahid committed Jul 1, 2024
1 parent 73ee63e commit 81967d0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
13 changes: 9 additions & 4 deletions cmd/wampproto/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ func parseCmd(args []string) (*cmd, error) {
unSubscribeCommand := messageCommand.Command("unsubscribe", "Unsubscribe message.")
unSubscribedCommand := messageCommand.Command("unsubscribed", "Unsubscribed message.")
c := &cmd{
output: app.Flag("output", "Format of the output.").Enum(wampprotocli.HexFormat, wampprotocli.Base64Format),
output: app.Flag("output", "Format of the output.").Default(wampprotocli.RawFormat).
Enum(wampprotocli.RawFormat, wampprotocli.HexFormat, wampprotocli.Base64Format),

auth: authCommand,

Expand Down Expand Up @@ -454,7 +455,7 @@ func Run(args []string) (string, error) {
craChallenge = string(craChallengeBytes)
}

if *c.output == "" {
if *c.output == wampprotocli.RawFormat {
return auth.SignCRAChallenge(craChallenge, craKey), nil
}

Expand Down Expand Up @@ -693,8 +694,12 @@ func serializeMessageAndOutput(serializerStr string, message messages.Message, o
return "", err
}

if outputFormat == "" && serializerStr != wampprotocli.JsonSerializer {
outputFormat = wampprotocli.HexFormat
if outputFormat == wampprotocli.RawFormat {
if serializerStr == wampprotocli.JsonSerializer {
return string(serializedMessage), nil
}

return fmt.Sprintf("%q", serializedMessage), nil
}

return wampprotocli.FormatOutputBytes(outputFormat, serializedMessage)
Expand Down
21 changes: 18 additions & 3 deletions cmd/wampproto/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"strconv"
"strings"
"testing"

"github.com/stretchr/testify/require"

wampprotocli "github.com/xconnio/wampproto-cli"
main "github.com/xconnio/wampproto-cli/cmd/wampproto"
"github.com/xconnio/wampproto-go/serializers"
wampprotobuf "github.com/xconnio/wampproto-protobuf/go"
)

func TestRunGenerateChallenge(t *testing.T) {
Expand Down Expand Up @@ -356,23 +359,35 @@ func testMessageCommand(t *testing.T, command string) {
output, err := main.Run(strings.Split(command+" --serializer cbor", " "))
require.NoError(t, err)

_, err = hex.DecodeString(output)
unQuotedStr, err := strconv.Unquote(output)
require.NoError(t, err)

var cborSerializer = serializers.CBORSerializer{}
_, err = cborSerializer.Deserialize([]byte(unQuotedStr))
require.NoError(t, err)
})

t.Run("MsgPackSerializer", func(t *testing.T) {
output, err := main.Run(strings.Split(command+" --serializer msgpack", " "))
require.NoError(t, err)

_, err = hex.DecodeString(output)
unQuotedStr, err := strconv.Unquote(output)
require.NoError(t, err)

var msgpackSerializer = serializers.MsgPackSerializer{}
_, err = msgpackSerializer.Deserialize([]byte(unQuotedStr))
require.NoError(t, err)
})

t.Run("ProtobufSerailizer", func(t *testing.T) {
output, err := main.Run(strings.Split(command+" --serializer protobuf", " "))
require.NoError(t, err)

_, err = hex.DecodeString(output)
unQuotedStr, err := strconv.Unquote(output)
require.NoError(t, err)

var protobufSerializer = wampprotobuf.ProtobufSerializer{}
_, err = protobufSerializer.Deserialize([]byte(unQuotedStr))
require.NoError(t, err)
})
}
Expand Down
1 change: 1 addition & 0 deletions constants.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package wampprotocli

const (
RawFormat = "raw"
HexFormat = "hex"
Base64Format = "base64"

Expand Down
2 changes: 1 addition & 1 deletion helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func FormatOutput(outputFormat, outputString string) (string, error) {

func FormatOutputBytes(outputFormat string, outputBytes []byte) (string, error) {
switch outputFormat {
case "":
case RawFormat:
return string(outputBytes), nil
case HexFormat:
return hex.EncodeToString(outputBytes), nil
Expand Down

0 comments on commit 81967d0

Please sign in to comment.