Skip to content

Commit

Permalink
fixed nil check
Browse files Browse the repository at this point in the history
  • Loading branch information
Dai.Otsuka authored and Admiral-Piett committed Oct 22, 2024
1 parent 1a058d0 commit 4e06d3d
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/models/sns.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ func (r *PublishRequest) SetAttributesFromForm(values url.Values) {
stringValue := values.Get(fmt.Sprintf("MessageAttributes.entry.%d.Value.StringValue", i))
binaryValue := values.Get(fmt.Sprintf("MessageAttributes.entry.%d.Value.BinaryValue", i))

if r.MessageAttributes == nil {
r.MessageAttributes = make(map[string]MessageAttributeValue)
}

r.MessageAttributes[name] = MessageAttributeValue{
DataType: dataType,
StringValue: stringValue,
Expand Down
72 changes: 72 additions & 0 deletions smoke_tests/sns_publish_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package smoke_tests

import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
Expand All @@ -11,13 +12,16 @@ import (
"github.com/aws/aws-sdk-go-v2/config"

"github.com/Admiral-Piett/goaws/app/conf"
af "github.com/Admiral-Piett/goaws/app/fixtures"
"github.com/Admiral-Piett/goaws/app/test"

"github.com/gavv/httpexpect/v2"

"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 @@ -52,6 +56,74 @@ func Test_Publish_sqs_json_raw(t *testing.T) {
assert.Equal(t, message, string(messages[0].MessageBody))
}

func Test_Publish_Sqs_With_Message_Attributes(t *testing.T) {
server := generateServer()
defer func() {
server.Close()
test.ResetResources()
}()

sdkConfig, _ := config.LoadDefaultConfig(context.TODO())
sdkConfig.BaseEndpoint = aws.String(server.URL)
sqsClient := sqs.NewFromConfig(sdkConfig)
snsClient := sns.NewFromConfig(sdkConfig)

createQueueResult, _ := sqsClient.CreateQueue(context.TODO(), &sqs.CreateQueueInput{
QueueName: &af.QueueName,
})

topicName := aws.String("unit-topic2")

createTopicResult, _ := snsClient.CreateTopic(context.TODO(), &sns.CreateTopicInput{
Name: topicName,
})

snsClient.Subscribe(context.TODO(), &sns.SubscribeInput{
Protocol: aws.String("sqs"),
TopicArn: createTopicResult.TopicArn,
Attributes: map[string]string{},
Endpoint: createQueueResult.QueueUrl,
ReturnSubscriptionArn: true,
})
message := "{\"IAm\": \"aMessage\"}"
subject := "I am a subject"
attributes := map[string]types.MessageAttributeValue{
"someKey": {
BinaryValue: []byte(message),
DataType: aws.String("Binary"),
},
}

publishResponse, publishErr := snsClient.Publish(context.TODO(), &sns.PublishInput{
TopicArn: createTopicResult.TopicArn,
Message: &message,
Subject: &subject,
MessageAttributes: attributes,
})

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

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)
}

func Test_Publish_sqs_json_not_raw(t *testing.T) {
server := generateServer()
defaultEnv := app.CurrentEnvironment
Expand Down

0 comments on commit 4e06d3d

Please sign in to comment.