Skip to content

Commit

Permalink
Add xml smoke test
Browse files Browse the repository at this point in the history
  • Loading branch information
kojisaikiAtSony committed May 15, 2024
1 parent fe667e4 commit 8464892
Showing 1 changed file with 171 additions and 0 deletions.
171 changes: 171 additions & 0 deletions smoke_tests/sqs_send_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/Admiral-Piett/goaws/app"
af "github.com/Admiral-Piett/goaws/app/fixtures"
"github.com/Admiral-Piett/goaws/app/models"
"github.com/Admiral-Piett/goaws/app/utils"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
Expand Down Expand Up @@ -218,3 +219,173 @@ func Test_SendMessageV1_json_QueueNotExistant(t *testing.T) {
assert.Contains(t, err.Error(), "AWS.SimpleQueueService.NonExistentQueue")
assert.Nil(t, sendMessageOutput)
}

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

sdkConfig, _ := config.LoadDefaultConfig(context.TODO())
sdkConfig.BaseEndpoint = aws.String(server.URL)
sqsClient := sqs.NewFromConfig(sdkConfig)
sdkResponse, _ := sqsClient.CreateQueue(context.TODO(), &sqs.CreateQueueInput{
QueueName: &af.QueueName,
})
targetQueueUrl := sdkResponse.QueueUrl

// Assert no messages in the queue before sending message
getQueueAttributesBodyXML := struct {
Action string `xml:"Action"`
Version string `xml:"Version"`
Attribute1 string `xml:"AttributeName.1"`
QueueUrl string `xml:"QueueUrl"`
}{
Action: "GetQueueAttributes",
Version: "2012-11-05",
Attribute1: "All",
QueueUrl: *targetQueueUrl,
}
e := httpexpect.Default(t, server.URL)
r := e.POST("/").
WithForm(getQueueAttributesBodyXML).
Expect().
Status(http.StatusOK).
Body().Raw()
r2 := app.GetQueueAttributesResponse{}
xml.Unmarshal([]byte(r), &r2)
for _, attr := range r2.Result.Attrs {
if attr.Name == "ApproximateNumberOfMessages" {
assert.Equal(t, "0", attr.Value)
}
}

// Target test: send a message
sendMessageXML := struct {
Action string `xml:"Action"`
Version string `xml:"Version"`
QueueUrl string `xml:"QueueUrl"`
MessageBody string `xml:"MessageBody"`
}{
Action: "SendMessage",
Version: "2012-11-05",
QueueUrl: *targetQueueUrl,
MessageBody: "Test Message",
}
e = httpexpect.Default(t, server.URL)
r = e.POST("/").
WithForm(sendMessageXML).
Expect().
Status(http.StatusOK).
Body().Raw()
r3 := models.SendMessageResult{}
xml.Unmarshal([]byte(r), &r3)
assert.NotNil(t, r3.MessageId)

// Assert 1 message in the queue
r = e.POST("/").
WithForm(getQueueAttributesBodyXML).
Expect().
Status(http.StatusOK).
Body().Raw()
r2 = app.GetQueueAttributesResponse{}
xml.Unmarshal([]byte(r), &r2)
for _, attr := range r2.Result.Attrs {
if attr.Name == "ApproximateNumberOfMessages" {
assert.Equal(t, "1", attr.Value)
}
}
}

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

sdkConfig, _ := config.LoadDefaultConfig(context.TODO())
sdkConfig.BaseEndpoint = aws.String(server.URL)
sqsClient := sqs.NewFromConfig(sdkConfig)
sdkResponse, _ := sqsClient.CreateQueue(context.TODO(), &sqs.CreateQueueInput{
QueueName: &af.QueueName,
})
targetQueueUrl := sdkResponse.QueueUrl

// Assert no messages in the queue before sending message
getQueueAttributesBodyXML := struct {
Action string `xml:"Action"`
Version string `xml:"Version"`
Attribute1 string `xml:"AttributeName.1"`
QueueUrl string `xml:"QueueUrl"`
}{
Action: "GetQueueAttributes",
Version: "2012-11-05",
Attribute1: "All",
QueueUrl: *targetQueueUrl,
}
e := httpexpect.Default(t, server.URL)
r := e.POST("/").
WithForm(getQueueAttributesBodyXML).
Expect().
Status(http.StatusOK).
Body().Raw()
r2 := app.GetQueueAttributesResponse{}
xml.Unmarshal([]byte(r), &r2)
for _, attr := range r2.Result.Attrs {
if attr.Name == "ApproximateNumberOfMessages" {
assert.Equal(t, "0", attr.Value)
}
}

// Target test: send a message
sendMessageXML := struct {
Action string `xml:"Action"`
Version string `xml:"Version"`
QueueUrl string `xml:"QueueUrl"`
MessageBody string `xml:"MessageBody"`
}{
Action: "SendMessage",
Version: "2012-11-05",
QueueUrl: *targetQueueUrl,
MessageBody: "Test Message",
}
e = httpexpect.Default(t, server.URL)
r = e.POST("/").
WithForm(sendMessageXML).
WithFormField("MessageAttribute.1.Name", "Attr1").
WithFormField("MessageAttribute.1.Value.DataType", "String").
WithFormField("MessageAttribute.1.Value.StringValue", "Value1").
WithFormField("MessageAttribute.2.Name", "Attr2").
WithFormField("MessageAttribute.2.Value.DataType", "Number").
WithFormField("MessageAttribute.2.Value.StringValue", "2").
Expect().
Status(http.StatusOK).
Body().Raw()
r3 := models.SendMessageResult{}
xml.Unmarshal([]byte(r), &r3)
assert.NotNil(t, r3.MessageId)

// Receive message and check attribute
receiveMessageBodyXML := struct {
Action string `xml:"Action"`
Version string `xml:"Version"`
Attribute1 string `xml:"AttributeName.1"`
QueueUrl string `xml:"QueueUrl"`
}{
Action: "ReceiveMessage",
Version: "2012-11-05",
QueueUrl: *targetQueueUrl,
}
r = e.POST("/").
WithForm(receiveMessageBodyXML).
Expect().
Status(http.StatusOK).
Body().Raw()
r4 := app.ReceiveMessageResponse{}
xml.Unmarshal([]byte(r), &r4)
message := r4.Result.Message[0]
assert.Equal(t, "Test Message", string(message.Body))
assert.Equal(t, 2, len(message.MessageAttributes))
}

0 comments on commit 8464892

Please sign in to comment.