Skip to content

Commit

Permalink
added base64 decode on json mashaller
Browse files Browse the repository at this point in the history
  • Loading branch information
Dai.Otsuka authored and Dai.Otsuka committed Oct 29, 2024
1 parent 3e612da commit 8eae00f
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 49 deletions.
8 changes: 5 additions & 3 deletions app/gosqs/message_attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,18 @@ func extractMessageAttributes(req *http.Request, prefix string) map[string]app.M
return attributes
}

func getMessageAttributeResult(a *app.MessageAttributeValue) *models.ResultMessageAttribute {
func getMessageAttributeResult(a app.MessageAttributeValue) *models.ResultMessageAttribute {
v := &models.ResultMessageAttributeValue{
DataType: a.DataType,
}

switch a.DataType {
case "Binary":
v.BinaryValue = a.Value
default:
v.StringValue = a.Value
case "String":
v.StringValue = &a.Value
case "Number":
v.StringValue = &a.Value
}

return &models.ResultMessageAttribute{
Expand Down
2 changes: 1 addition & 1 deletion app/gosqs/receive_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func ReceiveMessageV1(req *http.Request) (int, interfaces.AbstractResponseBody)
func getMessageResult(m *app.Message) *models.ResultMessage {
msgMttrs := []*models.ResultMessageAttribute{}
for _, attr := range m.MessageAttributes {
msgMttrs = append(msgMttrs, getMessageAttributeResult(&attr))
msgMttrs = append(msgMttrs, getMessageAttributeResult(attr))
}

attrsMap := map[string]string{
Expand Down
2 changes: 1 addition & 1 deletion app/gosqs/receive_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,5 +212,5 @@ func TestReceiveMessageAttributesV1(t *testing.T) {
assert.Equal(t, 1, len(result.Messages[0].MessageAttributes))
assert.Equal(t, "TestMessageAttrName", result.Messages[0].MessageAttributes[0].Name)
assert.Equal(t, "String", result.Messages[0].MessageAttributes[0].Value.DataType)
assert.Equal(t, "TestMessageAttrValue", result.Messages[0].MessageAttributes[0].Value.StringValue)
assert.Equal(t, "TestMessageAttrValue", *result.Messages[0].MessageAttributes[0].Value.StringValue)
}
2 changes: 1 addition & 1 deletion app/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var AVAILABLE_QUEUE_ATTRIBUTES = map[string]bool{
// TODO - reconcile this with app.MessageAttributeValue - deal with ConvertToOldMessageAttributeValueStructure
type MessageAttributeValue struct {
BinaryListValues []string `json:"BinaryListValues,omitempty"` // currently unsupported by AWS
BinaryValue string `json:"BinaryValue,omitempty"`
BinaryValue string `json:"BinaryValue,omitempty"` // base64 encoded string
DataType string `json:"DataType,omitempty"`
StringListValues []string `json:"StringListValues,omitempty"` // currently unsupported by AWS
StringValue string `json:"StringValue,omitempty"`
Expand Down
13 changes: 8 additions & 5 deletions app/models/responses.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package models

import (
"encoding/base64"
"encoding/json"

"github.com/Admiral-Piett/goaws/app"
Expand Down Expand Up @@ -80,20 +81,22 @@ func (r *ResultMessage) MarshalJSON() ([]byte, error) {
}

for _, attr := range r.MessageAttributes {
// When encoding json, BinaryValue([]byte) are automatically converted to base64.
binary, _ := base64.StdEncoding.DecodeString(attr.Value.BinaryValue)
m.MessageAttributes[attr.Name] = sqstypes.MessageAttributeValue{
DataType: &attr.Value.DataType,
StringValue: &attr.Value.StringValue,
BinaryValue: []byte(attr.Value.BinaryValue),
StringValue: attr.Value.StringValue,
BinaryValue: binary,
}
}

return json.Marshal(m)
}

type ResultMessageAttributeValue struct {
DataType string `xml:"DataType,omitempty"`
StringValue string `xml:"StringValue,omitempty"`
BinaryValue string `xml:"BinaryValue,omitempty"`
DataType string `xml:"DataType,omitempty"`
StringValue *string `xml:"StringValue,omitempty"`
BinaryValue string `xml:"BinaryValue,omitempty"`
}

type ResultMessageAttribute struct {
Expand Down
56 changes: 40 additions & 16 deletions smoke_tests/sns_publish_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package smoke_tests

import (
"context"
"encoding/json"
"crypto/md5"
"encoding/binary"
"encoding/hex"
"hash"
"io"
"net/http"
"net/http/httptest"
Expand All @@ -24,8 +27,6 @@ import (
"github.com/Admiral-Piett/goaws/app"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/sns"
"github.com/aws/aws-sdk-go-v2/service/sns/types"
"github.com/aws/aws-sdk-go-v2/service/sqs"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -73,6 +74,12 @@ func Test_Publish_Sqs_With_Message_Attributes(t *testing.T) {
server.Close()
test.ResetResources()
}()
addBytesToHash := func(hasher hash.Hash, arr []byte) {
bs := make([]byte, 4)
binary.BigEndian.PutUint32(bs, uint32(len(arr)))
hasher.Write(bs)
hasher.Write(arr)
}

sdkConfig, _ := config.LoadDefaultConfig(context.TODO())
sdkConfig.BaseEndpoint = aws.String(server.URL)
Expand All @@ -89,18 +96,28 @@ func Test_Publish_Sqs_With_Message_Attributes(t *testing.T) {
Name: topicName,
})

snsClient.Subscribe(context.TODO(), &sns.SubscribeInput{
subscribeResult, _ := snsClient.Subscribe(context.TODO(), &sns.SubscribeInput{
Protocol: aws.String("sqs"),
TopicArn: createTopicResult.TopicArn,
Attributes: map[string]string{},
Endpoint: createQueueResult.QueueUrl,
ReturnSubscriptionArn: true,
})

snsClient.SetSubscriptionAttributes(context.TODO(), &sns.SetSubscriptionAttributesInput{
SubscriptionArn: subscribeResult.SubscriptionArn,
AttributeName: aws.String("RawMessageDelivery"),
AttributeValue: aws.String("true"),
})

message := "{\"IAm\": \"aMessage\"}"
binaryValueString := "binary-value"
binaryValue := []byte(binaryValueString)
subject := "I am a subject"

attributes := map[string]types.MessageAttributeValue{
"someKey": {
BinaryValue: []byte(message),
BinaryValue: binaryValue,
DataType: aws.String("Binary"),
},
}
Expand All @@ -113,26 +130,33 @@ func Test_Publish_Sqs_With_Message_Attributes(t *testing.T) {
})

receiveMessageResponse, receiveErr := sqsClient.ReceiveMessage(context.TODO(), &sqs.ReceiveMessageInput{
QueueUrl: createQueueResult.QueueUrl,
QueueUrl: createQueueResult.QueueUrl,
MaxNumberOfMessages: 10,
})

type Message struct {
Message string `json:"Message"`
Subject string `json:"Subject"`
}

var receiveMessage Message

assert.Nil(t, publishErr)
assert.NotNil(t, publishResponse)

assert.Nil(t, receiveErr)
assert.NotNil(t, receiveMessageResponse)

body := *receiveMessageResponse.Messages[0].Body
json.Unmarshal([]byte(body), &receiveMessage)
assert.Equal(t, message, receiveMessage.Message)
assert.Equal(t, subject, receiveMessage.Subject)
assert.Equal(t, message, body)

messageAttribute := receiveMessageResponse.Messages[0].MessageAttributes["someKey"]
assert.Equal(t, "Binary", *messageAttribute.DataType)
assert.Equal(t, binaryValue, messageAttribute.BinaryValue)

// check client md5hash and received md5hash
hasher := md5.New()
addBytesToHash(hasher, []byte("someKey"))
addBytesToHash(hasher, []byte(*messageAttribute.DataType))
hasher.Write([]byte{2})
addBytesToHash(hasher, messageAttribute.BinaryValue)
clientHash := hex.EncodeToString(hasher.Sum(nil))

assert.Equal(t, clientHash, *receiveMessageResponse.Messages[0].MD5OfMessageAttributes)

}

func Test_Publish_sqs_json_not_raw(t *testing.T) {
Expand Down
32 changes: 18 additions & 14 deletions smoke_tests/sqs_send_message_batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package smoke_tests

import (
"context"
"encoding/base64"
"encoding/xml"
"fmt"
"net/http"
Expand Down Expand Up @@ -253,7 +254,8 @@ func TestSendMessageBatchV1_Json_Success(t *testing.T) {
stringType := "String"
numberType := "Number"

binaryValue := "base64-encoded-value"
binaryValueString := "binary-value"
binaryValue := []byte(binaryValueString)
stringValue := "hogeValue"
numberValue := "100"

Expand All @@ -268,7 +270,7 @@ func TestSendMessageBatchV1_Json_Success(t *testing.T) {
MessageBody: &messageBody2,
MessageAttributes: map[string]types.MessageAttributeValue{
binaryAttribute: {
BinaryValue: []byte(binaryValue),
BinaryValue: binaryValue,
DataType: &binaryType,
},
stringAttribute: {
Expand Down Expand Up @@ -318,27 +320,27 @@ func TestSendMessageBatchV1_Json_Success(t *testing.T) {
attr2.Name = k
attr2.Value = &models.ResultMessageAttributeValue{
DataType: *attr.DataType,
StringValue: *attr.StringValue,
StringValue: attr.StringValue,
}
} else if k == numberAttribute {
attr3.Name = k
attr3.Value = &models.ResultMessageAttributeValue{
DataType: *attr.DataType,
StringValue: *attr.StringValue,
StringValue: attr.StringValue,
}
}
}
assert.Equal(t, binaryAttribute, attr1.Name)
assert.Equal(t, binaryType, attr1.Value.DataType)
assert.Equal(t, "YmFzZTY0LWVuY29kZWQtdmFsdWU=", attr1.Value.BinaryValue) // base64 encoded value
assert.Equal(t, binaryValueString, attr1.Value.BinaryValue)

assert.Equal(t, stringAttribute, attr2.Name)
assert.Equal(t, stringType, attr2.Value.DataType)
assert.Equal(t, stringValue, attr2.Value.StringValue)
assert.Equal(t, stringValue, *attr2.Value.StringValue)

assert.Equal(t, numberAttribute, attr3.Name)
assert.Equal(t, numberType, attr3.Value.DataType)
assert.Equal(t, numberValue, attr3.Value.StringValue)
assert.Equal(t, numberValue, *attr3.Value.StringValue)
}

func TestSendMessageBatchV1_Xml_Success(t *testing.T) {
Expand Down Expand Up @@ -371,7 +373,9 @@ func TestSendMessageBatchV1_Xml_Success(t *testing.T) {
stringType := "String"
numberType := "Number"

binaryValue := "YmFzZTY0LWVuY29kZWQtdmFsdWU="
binaryValueString := "binary-value"
binaryValue := []byte(binaryValueString)
base64EncodedString := base64.StdEncoding.EncodeToString(binaryValue)
stringValue := "hogeValue"
numberValue := "100"

Expand All @@ -393,7 +397,7 @@ func TestSendMessageBatchV1_Xml_Success(t *testing.T) {
WithFormField("Entries.1.MessageBody", messageBody2).
WithFormField("Entries.1.MessageAttributes.1.Name", binaryAttribute).
WithFormField("Entries.1.MessageAttributes.1.Value.DataType", binaryType).
WithFormField("Entries.1.MessageAttributes.1.Value.BinaryValue", binaryValue).
WithFormField("Entries.1.MessageAttributes.1.Value.BinaryValue", base64EncodedString).
WithFormField("Entries.1.MessageAttributes.2.Name", stringAttribute).
WithFormField("Entries.1.MessageAttributes.2.Value.DataType", stringType).
WithFormField("Entries.1.MessageAttributes.2.Value.StringValue", stringValue).
Expand Down Expand Up @@ -441,26 +445,26 @@ func TestSendMessageBatchV1_Xml_Success(t *testing.T) {
attr2.Name = k
attr2.Value = &models.ResultMessageAttributeValue{
DataType: *attr.DataType,
StringValue: *attr.StringValue,
StringValue: attr.StringValue,
}
} else if k == numberAttribute {
attr3.Name = k
attr3.Value = &models.ResultMessageAttributeValue{
DataType: *attr.DataType,
StringValue: *attr.StringValue,
StringValue: attr.StringValue,
}
}
}
assert.Equal(t, binaryAttribute, attr1.Name)
assert.Equal(t, binaryType, attr1.Value.DataType)
assert.Equal(t, "YmFzZTY0LWVuY29kZWQtdmFsdWU=", attr1.Value.BinaryValue) // base64 encoded value
assert.Equal(t, binaryValueString, attr1.Value.BinaryValue)

assert.Equal(t, stringAttribute, attr2.Name)
assert.Equal(t, stringType, attr2.Value.DataType)
assert.Equal(t, stringValue, attr2.Value.StringValue)
assert.Equal(t, stringValue, *attr2.Value.StringValue)

assert.Equal(t, numberAttribute, attr3.Name)
assert.Equal(t, numberType, attr3.Value.DataType)
assert.Equal(t, numberValue, attr3.Value.StringValue)
assert.Equal(t, numberValue, *attr3.Value.StringValue)

}
16 changes: 8 additions & 8 deletions smoke_tests/sqs_send_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,34 +129,34 @@ func Test_SendMessageV1_json_with_attributes(t *testing.T) {
attr1.Name = k
attr1.Value = &models.ResultMessageAttributeValue{
DataType: *attr.DataType,
StringValue: *attr.StringValue,
StringValue: attr.StringValue,
BinaryValue: string(attr.BinaryValue),
}
} else if k == "attr2" {
attr2.Name = k
attr2.Value = &models.ResultMessageAttributeValue{
DataType: *attr.DataType,
StringValue: *attr.StringValue,
StringValue: attr.StringValue,
BinaryValue: string(attr.BinaryValue),
}
} else if k == "attr3" {
attr3.Name = k
attr3.Value = &models.ResultMessageAttributeValue{
DataType: *attr.DataType,
StringValue: *attr.StringValue,
StringValue: attr.StringValue,
BinaryValue: string(attr.BinaryValue),
}
}
}
assert.Equal(t, "attr1", attr1.Name)
assert.Equal(t, "String", attr1.Value.DataType)
assert.Equal(t, "attr1_value", attr1.Value.StringValue)
assert.Equal(t, "attr1_value", *attr1.Value.StringValue)
assert.Equal(t, "attr2", attr2.Name)
assert.Equal(t, "Number", attr2.Value.DataType)
assert.Equal(t, "2", attr2.Value.StringValue)
assert.Equal(t, "2", *attr2.Value.StringValue)
assert.Equal(t, "attr3", attr3.Name)
assert.Equal(t, "Binary", attr3.Value.DataType)
assert.Equal(t, "YXR0cjNfdmFsdWU=", attr3.Value.BinaryValue) // base64 encoded "attr3_value"
assert.Equal(t, "attr3_value", attr3.Value.BinaryValue)
}

func Test_SendMessageV1_json_MaximumMessageSize_TooBig(t *testing.T) {
Expand Down Expand Up @@ -350,10 +350,10 @@ func Test_SendMessageV1_xml_with_attributes(t *testing.T) {
}
assert.Equal(t, "attr1", attr1.Name)
assert.Equal(t, "String", attr1.Value.DataType)
assert.Equal(t, "attr1_value", attr1.Value.StringValue)
assert.Equal(t, "attr1_value", *attr1.Value.StringValue)
assert.Equal(t, "attr2", attr2.Name)
assert.Equal(t, "Number", attr2.Value.DataType)
assert.Equal(t, "2", attr2.Value.StringValue)
assert.Equal(t, "2", *attr2.Value.StringValue)
assert.Equal(t, "attr3", attr3.Name)
assert.Equal(t, "Binary", attr3.Value.DataType)
assert.Equal(t, "YXR0cjNfdmFsdWU=", attr3.Value.BinaryValue) // base64 encoded "attr3_value"
Expand Down

0 comments on commit 8eae00f

Please sign in to comment.