From fa5ea7bf3afd44037dd6999f617519ac0a5b64e6 Mon Sep 17 00:00:00 2001 From: Devin Humphreys Date: Tue, 5 Mar 2024 14:47:13 -0500 Subject: [PATCH] Finish CreateQueueV1 --- app/fixtures/fixtures.go | 3 + app/gosqs/gosqs.go | 1 + app/models/conversions.go | 1 - app/models/models.go | 18 ++++ app/models/models_test.go | 51 +++++++++++ app/utils/tests.go | 25 ++++++ go.mod | 15 +++- go.sum | 31 ++++++- smoke_tests/fixtures/fixtures.go | 3 - smoke_tests/fixtures/requests.go | 2 +- smoke_tests/fixtures/responses.go | 4 +- smoke_tests/sqs_create_queue_test.go | 123 +++++++++++++++++++++------ 12 files changed, 240 insertions(+), 37 deletions(-) diff --git a/app/fixtures/fixtures.go b/app/fixtures/fixtures.go index 252753bb..c88ce736 100644 --- a/app/fixtures/fixtures.go +++ b/app/fixtures/fixtures.go @@ -1,4 +1,7 @@ package fixtures +var BASE_URL = "http://region.host:port/accountID" +var BASE_ARN = "arn:aws:sqs:region:accountID" + var XMLNS = "http://queue.amazonaws.com/doc/2012-11-05/" var REQUEST_ID = "request-id" diff --git a/app/gosqs/gosqs.go b/app/gosqs/gosqs.go index 9ec13b9d..018e9286 100644 --- a/app/gosqs/gosqs.go +++ b/app/gosqs/gosqs.go @@ -897,6 +897,7 @@ func GetQueueAttributes(w http.ResponseWriter, req *http.Request) { attribs = append(attribs, attr) } + // TODO - why do we just return the name and NOT the actual ARN here? deadLetterTargetArn := "" if queue.DeadLetterQueue != nil { deadLetterTargetArn = queue.DeadLetterQueue.Name diff --git a/app/models/conversions.go b/app/models/conversions.go index 5bd93b7b..35c7530f 100644 --- a/app/models/conversions.go +++ b/app/models/conversions.go @@ -5,7 +5,6 @@ import ( "strconv" ) -// TODO - test me // StringToInt this is a custom type that will allow our request bodies to support either a string OR an int. // It has its own UnmarshalJSON method to handle both types automatically and it can return an `int` // from the `Int` method. diff --git a/app/models/models.go b/app/models/models.go index c8ef10b7..f8b42334 100644 --- a/app/models/models.go +++ b/app/models/models.go @@ -152,3 +152,21 @@ type RedrivePolicy struct { MaxReceiveCount StringToInt `json:"maxReceiveCount"` DeadLetterTargetArn string `json:"deadLetterTargetArn"` } + +// UnmarshalJSON this will convert a JSON string of a Redrive Policy sub-doc (escaped characters and all) or +// a regular json document into the appropriate resulting struct. +func (r *RedrivePolicy) UnmarshalJSON(data []byte) error { + type basicRequest RedrivePolicy + + err := json.Unmarshal(data, (*basicRequest)(r)) + if err == nil { + return nil + } + + tmp, _ := strconv.Unquote(string(data)) + err = json.Unmarshal([]byte(tmp), (*basicRequest)(r)) + if err != nil { + return err + } + return nil +} diff --git a/app/models/models_test.go b/app/models/models_test.go index 041222d9..1ec5323f 100644 --- a/app/models/models_test.go +++ b/app/models/models_test.go @@ -1,6 +1,8 @@ package models import ( + "encoding/json" + "fmt" "net/url" "testing" @@ -175,3 +177,52 @@ func TestCreateQueueRequest_SetAttributesFromForm_success_skips_invalid_values(t assert.Equal(t, RedrivePolicy{}, cqr.Attributes.RedrivePolicy) assert.Equal(t, map[string]interface{}(nil), cqr.Attributes.RedriveAllowPolicy) } + +func TestRedrivePolicy_UnmarshalJSON_handles_nested_json(t *testing.T) { + request := struct { + MaxReceiveCount int `json:"maxReceiveCount"` + DeadLetterTargetArn string `json:"deadLetterTargetArn"` + }{ + MaxReceiveCount: 100, + DeadLetterTargetArn: "arn:redrive-queue", + } + b, _ := json.Marshal(request) + var r = RedrivePolicy{} + err := r.UnmarshalJSON(b) + + assert.Nil(t, err) + assert.Equal(t, StringToInt(100), r.MaxReceiveCount) + assert.Equal(t, fmt.Sprintf("%s:%s", "arn", "redrive-queue"), r.DeadLetterTargetArn) +} + +func TestRedrivePolicy_UnmarshalJSON_handles_escaped_string(t *testing.T) { + request := `{"maxReceiveCount":"100","deadLetterTargetArn":"arn:redrive-queue"}` + b, _ := json.Marshal(request) + var r = RedrivePolicy{} + err := r.UnmarshalJSON(b) + + assert.Nil(t, err) + assert.Equal(t, StringToInt(100), r.MaxReceiveCount) + assert.Equal(t, fmt.Sprintf("%s:%s", "arn", "redrive-queue"), r.DeadLetterTargetArn) +} + +func TestRedrivePolicy_UnmarshalJSON_invalid_json_request_returns_error(t *testing.T) { + request := fmt.Sprintf(`{\"maxReceiveCount\":\"100\",\"deadLetterTargetArn\":\"arn:redrive-queue\"}`) + var r = RedrivePolicy{} + err := r.UnmarshalJSON([]byte(request)) + + assert.Error(t, err) + assert.Equal(t, StringToInt(0), r.MaxReceiveCount) + assert.Equal(t, "", r.DeadLetterTargetArn) +} + +func TestRedrivePolicy_UnmarshalJSON_invalid_type_returns_error(t *testing.T) { + request := `{"maxReceiveCount":true,"deadLetterTargetArn":"arn:redrive-queue"}` + b, _ := json.Marshal(request) + var r = RedrivePolicy{} + err := r.UnmarshalJSON(b) + + assert.Error(t, err) + assert.Equal(t, StringToInt(0), r.MaxReceiveCount) + assert.Equal(t, "", r.DeadLetterTargetArn) +} diff --git a/app/utils/tests.go b/app/utils/tests.go index bac67374..d62c7e92 100644 --- a/app/utils/tests.go +++ b/app/utils/tests.go @@ -2,11 +2,17 @@ package utils import ( "bytes" + "context" + "crypto/tls" "encoding/json" + "fmt" "net/http" "net/http/httptest" urlLib "net/url" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/config" + "github.com/Admiral-Piett/goaws/app" ) @@ -57,3 +63,22 @@ func GenerateRequestInfo(method, url string, body interface{}, isJson bool) (*ht rr := httptest.NewRecorder() return rr, req } + +// GenerateLocalProxyConfig use this to create AWS config that can be plugged into your sqs client, and +// force calls onto a local proxy. This is helpful for testing directly with an HTTP inspection tool +// such as Charles or Proxyman. +func GenerateLocalProxyConfig(proxyPort int) aws.Config { + tr := &http.Transport{ + TLSClientConfig: &tls.Config{ + InsecureSkipVerify: false, + }, + } + proxyURL, _ := urlLib.Parse(fmt.Sprintf("http://127.0.0.1:%d", proxyPort)) + tr.Proxy = http.ProxyURL(proxyURL) + client := &http.Client{Transport: tr} + + sdkConfig, _ := config.LoadDefaultConfig(context.TODO(), + config.WithHTTPClient(client), + ) + return sdkConfig +} diff --git a/go.mod b/go.mod index 24515f72..3feb252f 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,9 @@ go 1.18 require ( github.com/aws/aws-sdk-go v1.47.3 + github.com/aws/aws-sdk-go-v2 v1.25.2 + github.com/aws/aws-sdk-go-v2/config v1.27.4 + github.com/aws/aws-sdk-go-v2/service/sqs v1.31.1 github.com/gavv/httpexpect/v2 v2.16.0 github.com/ghodss/yaml v1.0.0 github.com/gorilla/mux v1.8.0 @@ -17,6 +20,17 @@ require ( github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2 // indirect github.com/ajg/form v1.5.1 // indirect github.com/andybalholm/brotli v1.0.4 // indirect + github.com/aws/aws-sdk-go-v2/credentials v1.17.4 // indirect + github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.15.2 // indirect + github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.2 // indirect + github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.2 // indirect + github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 // indirect + github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.1 // indirect + github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.2 // indirect + github.com/aws/aws-sdk-go-v2/service/sso v1.20.1 // indirect + github.com/aws/aws-sdk-go-v2/service/ssooidc v1.23.1 // indirect + github.com/aws/aws-sdk-go-v2/service/sts v1.28.1 // indirect + github.com/aws/smithy-go v1.20.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/fatih/color v1.15.0 // indirect github.com/fatih/structs v1.1.0 // indirect @@ -35,7 +49,6 @@ require ( github.com/pmezard/go-difflib v1.0.0 // indirect github.com/sanity-io/litter v1.5.5 // indirect github.com/sergi/go-diff v1.0.0 // indirect - github.com/stretchr/objx v0.1.0 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasthttp v1.34.0 // indirect github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect diff --git a/go.sum b/go.sum index 74268a2f..1f37d935 100644 --- a/go.sum +++ b/go.sum @@ -6,6 +6,34 @@ github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= github.com/aws/aws-sdk-go v1.47.3 h1:e0H6NFXiniCpR8Lu3lTphVdRaeRCDLAeRyTHd1tJSd8= github.com/aws/aws-sdk-go v1.47.3/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk= +github.com/aws/aws-sdk-go-v2 v1.25.2 h1:/uiG1avJRgLGiQM9X3qJM8+Qa6KRGK5rRPuXE0HUM+w= +github.com/aws/aws-sdk-go-v2 v1.25.2/go.mod h1:Evoc5AsmtveRt1komDwIsjHFyrP5tDuF1D1U+6z6pNo= +github.com/aws/aws-sdk-go-v2/config v1.27.4 h1:AhfWb5ZwimdsYTgP7Od8E9L1u4sKmDW2ZVeLcf2O42M= +github.com/aws/aws-sdk-go-v2/config v1.27.4/go.mod h1:zq2FFXK3A416kiukwpsd+rD4ny6JC7QSkp4QdN1Mp2g= +github.com/aws/aws-sdk-go-v2/credentials v1.17.4 h1:h5Vztbd8qLppiPwX+y0Q6WiwMZgpd9keKe2EAENgAuI= +github.com/aws/aws-sdk-go-v2/credentials v1.17.4/go.mod h1:+30tpwrkOgvkJL1rUZuRLoxcJwtI/OkeBLYnHxJtVe0= +github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.15.2 h1:AK0J8iYBFeUk2Ax7O8YpLtFsfhdOByh2QIkHmigpRYk= +github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.15.2/go.mod h1:iRlGzMix0SExQEviAyptRWRGdYNo3+ufW/lCzvKVTUc= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.2 h1:bNo4LagzUKbjdxE0tIcR9pMzLR2U/Tgie1Hq1HQ3iH8= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.2/go.mod h1:wRQv0nN6v9wDXuWThpovGQjqF1HFdcgWjporw14lS8k= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.2 h1:EtOU5jsPdIQNP+6Q2C5e3d65NKT1PeCiQk+9OdzO12Q= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.2/go.mod h1:tyF5sKccmDz0Bv4NrstEr+/9YkSPJHrcO7UsUKf7pWM= +github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 h1:hT8rVHwugYE2lEfdFE0QWVo81lF7jMrYJVDWI+f+VxU= +github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0/go.mod h1:8tu/lYfQfFe6IGnaOdrpVgEL2IrrDOf6/m9RQum4NkY= +github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.1 h1:EyBZibRTVAs6ECHZOw5/wlylS9OcTzwyjeQMudmREjE= +github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.1/go.mod h1:JKpmtYhhPs7D97NL/ltqz7yCkERFW5dOlHyVl66ZYF8= +github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.2 h1:5ffmXjPtwRExp1zc7gENLgCPyHFbhEPwVTkTiH9niSk= +github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.2/go.mod h1:Ru7vg1iQ7cR4i7SZ/JTLYN9kaXtbL69UdgG0OQWQxW0= +github.com/aws/aws-sdk-go-v2/service/sqs v1.31.1 h1:124rVNP6NbCfBZwiX1kfjMQrnsJtnpKeB0GalkuqSXo= +github.com/aws/aws-sdk-go-v2/service/sqs v1.31.1/go.mod h1:YijRvM1SAmuiIQ9pjfwahIEE3HMHUkx9P5oplL/Jnj4= +github.com/aws/aws-sdk-go-v2/service/sso v1.20.1 h1:utEGkfdQ4L6YW/ietH7111ZYglLJvS+sLriHJ1NBJEQ= +github.com/aws/aws-sdk-go-v2/service/sso v1.20.1/go.mod h1:RsYqzYr2F2oPDdpy+PdhephuZxTfjHQe7SOBcZGoAU8= +github.com/aws/aws-sdk-go-v2/service/ssooidc v1.23.1 h1:9/GylMS45hGGFCcMrUZDVayQE1jYSIN6da9jo7RAYIw= +github.com/aws/aws-sdk-go-v2/service/ssooidc v1.23.1/go.mod h1:YjAPFn4kGFqKC54VsHs5fn5B6d+PCY2tziEa3U/GB5Y= +github.com/aws/aws-sdk-go-v2/service/sts v1.28.1 h1:3I2cBEYgKhrWlwyZgfpSO2BpaMY1LHPqXYk/QGlu2ew= +github.com/aws/aws-sdk-go-v2/service/sts v1.28.1/go.mod h1:uQ7YYKZt3adCRrdCBREm1CD3efFLOUNH77MrUCvx5oA= +github.com/aws/smithy-go v1.20.1 h1:4SZlSlMr36UEqC7XOyRVb27XMeZubNcBNN+9IgEPIQw= +github.com/aws/smithy-go v1.20.1/go.mod h1:krry+ya/rV9RDcV/Q16kpu6ypI4K2czasz0NC3qS14E= github.com/davecgh/go-spew v0.0.0-20161028175848-04cdfd42973b/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= @@ -20,8 +48,8 @@ github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y= github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8= -github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= @@ -69,7 +97,6 @@ github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= -github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v0.0.0-20161117074351-18a02ba4a312/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= diff --git a/smoke_tests/fixtures/fixtures.go b/smoke_tests/fixtures/fixtures.go index 5380790c..5fb520b2 100644 --- a/smoke_tests/fixtures/fixtures.go +++ b/smoke_tests/fixtures/fixtures.go @@ -1,6 +1,3 @@ package fixtures var REQUEST_ID = "00000000-0000-0000-0000-000000000000" - -var BASE_URL = "http://region.host:port/accountID" -var BASE_ARN = "arn:aws:sqs:region:accountID" diff --git a/smoke_tests/fixtures/requests.go b/smoke_tests/fixtures/requests.go index aa8d43d3..6c18fb7f 100644 --- a/smoke_tests/fixtures/requests.go +++ b/smoke_tests/fixtures/requests.go @@ -24,7 +24,7 @@ var GetQueueAttributesRequestBodyXML = struct { Action: "GetQueueAttributes", Version: "2012-11-05", Attribute1: "All", - QueueUrl: fmt.Sprintf("%s/new-queue-1", BASE_URL), + QueueUrl: fmt.Sprintf("%s/new-queue-1", af.BASE_URL), } var CreateQueueV1RequestBodyJSON = models.CreateQueueRequest{ diff --git a/smoke_tests/fixtures/responses.go b/smoke_tests/fixtures/responses.go index c3f87c01..62ed5a66 100644 --- a/smoke_tests/fixtures/responses.go +++ b/smoke_tests/fixtures/responses.go @@ -3,6 +3,8 @@ package fixtures import ( "fmt" + af "github.com/Admiral-Piett/goaws/app/fixtures" + "github.com/Admiral-Piett/goaws/app" ) @@ -39,7 +41,7 @@ var BASE_GET_QUEUE_ATTRIBUTES_RESPONSE = app.GetQueueAttributesResponse{ }, { Name: "QueueArn", - Value: fmt.Sprintf("%s:new-queue-1", BASE_ARN), + Value: fmt.Sprintf("%s:new-queue-1", af.BASE_ARN), }, { Name: "RedrivePolicy", diff --git a/smoke_tests/sqs_create_queue_test.go b/smoke_tests/sqs_create_queue_test.go index f14bdc25..24989be9 100644 --- a/smoke_tests/sqs_create_queue_test.go +++ b/smoke_tests/sqs_create_queue_test.go @@ -1,12 +1,17 @@ package smoke_tests import ( + "context" "encoding/json" "encoding/xml" "fmt" "net/http" "testing" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/config" + "github.com/aws/aws-sdk-go-v2/service/sqs" + "github.com/Admiral-Piett/goaws/app/utils" "github.com/Admiral-Piett/goaws/app/models" @@ -16,6 +21,7 @@ import ( "github.com/Admiral-Piett/goaws/app" "github.com/stretchr/testify/assert" + af "github.com/Admiral-Piett/goaws/app/fixtures" sf "github.com/Admiral-Piett/goaws/smoke_tests/fixtures" "github.com/gavv/httpexpect/v2" @@ -23,6 +29,7 @@ import ( // TODO - Is there a way to also capture the defaults we set and/or load from the config here? (review the xml // code below) +// NOTE: Actually I think you can just adjust the app.CurrentEnvironment memory space...it travels across tests it seems. func Test_CreateQueueV1_json_no_attributes(t *testing.T) { server := generateServer() defer func() { @@ -32,26 +39,80 @@ func Test_CreateQueueV1_json_no_attributes(t *testing.T) { e := httpexpect.Default(t, server.URL) - dupe, _ := copystructure.Copy(sf.CreateQueueV1RequestBodyJSON) - cqr, _ := dupe.(models.CreateQueueRequest) - cqr.Attributes = models.Attributes{} + 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, + }) + + assert.Equal(t, fmt.Sprintf("%s/new-queue-1", af.BASE_URL), *sdkResponse.QueueUrl) + r := e.POST("/"). - WithHeaders(map[string]string{ - "Content-Type": "application/x-amz-json-1.0", - "X-Amz-Target": "AmazonSQS.CreateQueue", - }). - WithJSON(cqr). + WithForm(sf.ListQueuesRequestBodyXML). Expect(). Status(http.StatusOK). Body().Raw() - exp1 := models.CreateQueueResult{QueueUrl: fmt.Sprintf("%s/new-queue-1", sf.BASE_URL)} - - r1 := models.CreateQueueResult{} - json.Unmarshal([]byte(r), &r1) - assert.Equal(t, exp1, r1) + exp2 := app.ListQueuesResponse{ + Xmlns: "http://queue.amazonaws.com/doc/2012-11-05/", + Result: app.ListQueuesResult{QueueUrl: []string{fmt.Sprintf("%s/new-queue-1", af.BASE_URL)}}, + Metadata: app.ResponseMetadata{RequestId: sf.REQUEST_ID}, + } + r2 := app.ListQueuesResponse{} + xml.Unmarshal([]byte(r), &r2) + assert.Equal(t, exp2, r2) r = e.POST("/"). + WithForm(sf.GetQueueAttributesRequestBodyXML). + Expect(). + Status(http.StatusOK). + Body().Raw() + + r3 := app.GetQueueAttributesResponse{} + xml.Unmarshal([]byte(r), &r3) + assert.Equal(t, sf.BASE_GET_QUEUE_ATTRIBUTES_RESPONSE, r3) +} + +func Test_CreateQueueV1_json_with_attributes(t *testing.T) { + server := generateServer() + defer func() { + server.Close() + utils.ResetResources() + }() + + e := httpexpect.Default(t, server.URL) + + redriveQueue := "redrive-queue" + + //sdkConfig, _ := config.LoadDefaultConfig(context.TODO()) + sdkConfig := utils.GenerateLocalProxyConfig(9090) + sdkConfig.BaseEndpoint = aws.String(server.URL) + + sqsClient := sqs.NewFromConfig(sdkConfig) + sdkResponse, _ := sqsClient.CreateQueue(context.TODO(), &sqs.CreateQueueInput{ + QueueName: &redriveQueue, + }) + + // TODO - HERE - Sub-Doc attributes not working for some reason? Check proxyman? + sdkResponse, err := sqsClient.CreateQueue(context.TODO(), &sqs.CreateQueueInput{ + QueueName: &af.QueueName, + Attributes: map[string]string{ + "DelaySeconds": "1", + "MaximumMessageSize": "2", + "MessageRetentionPeriod": "3", + //"Policy": "{\"this-is\": \"the-policy\"}", + "ReceiveMessageWaitTimeSeconds": "4", + "VisibilityTimeout": "5", + "RedrivePolicy": fmt.Sprintf(`{"maxReceiveCount":"100","deadLetterTargetArn":"%s:%s"}`, af.BASE_ARN, redriveQueue), + //"RedriveAllowPolicy": "{\"this-is\": \"the-redrive-allow-policy\"}", + }, + }) + + assert.Nil(t, err) + assert.Equal(t, fmt.Sprintf("%s/new-queue-1", af.BASE_URL), *sdkResponse.QueueUrl) + + r := e.POST("/"). WithForm(sf.ListQueuesRequestBodyXML). Expect(). Status(http.StatusOK). @@ -59,7 +120,7 @@ func Test_CreateQueueV1_json_no_attributes(t *testing.T) { exp2 := app.ListQueuesResponse{ Xmlns: "http://queue.amazonaws.com/doc/2012-11-05/", - Result: app.ListQueuesResult{QueueUrl: []string{fmt.Sprintf("%s/new-queue-1", sf.BASE_URL)}}, + Result: app.ListQueuesResult{QueueUrl: []string{fmt.Sprintf("%s/%s", af.BASE_URL, redriveQueue), fmt.Sprintf("%s/new-queue-1", af.BASE_URL)}}, Metadata: app.ResponseMetadata{RequestId: sf.REQUEST_ID}, } r2 := app.ListQueuesResponse{} @@ -72,12 +133,18 @@ func Test_CreateQueueV1_json_no_attributes(t *testing.T) { Status(http.StatusOK). Body().Raw() + dupe, _ := copystructure.Copy(sf.BASE_GET_QUEUE_ATTRIBUTES_RESPONSE) + exp3, _ := dupe.(app.GetQueueAttributesResponse) + exp3.Result.Attrs[0].Value = "5" + exp3.Result.Attrs[1].Value = "1" + exp3.Result.Attrs[2].Value = "4" + exp3.Result.Attrs[8].Value = fmt.Sprintf(`{"maxReceiveCount": "100", "deadLetterTargetArn":"%s"}`, redriveQueue) r3 := app.GetQueueAttributesResponse{} xml.Unmarshal([]byte(r), &r3) - assert.Equal(t, sf.BASE_GET_QUEUE_ATTRIBUTES_RESPONSE, r3) + assert.Equal(t, exp3, r3) } -func Test_CreateQueueV1_json_with_attributes(t *testing.T) { +func Test_CreateQueueV1_json_with_attributes_as_ints(t *testing.T) { server := generateServer() defer func() { server.Close() @@ -96,7 +163,7 @@ func Test_CreateQueueV1_json_with_attributes(t *testing.T) { Status(http.StatusOK). Body().Raw() - exp1 := models.CreateQueueResult{QueueUrl: fmt.Sprintf("%s/new-queue-1", sf.BASE_URL)} + exp1 := models.CreateQueueResult{QueueUrl: fmt.Sprintf("%s/new-queue-1", af.BASE_URL)} r1 := models.CreateQueueResult{} json.Unmarshal([]byte(r), &r1) @@ -110,7 +177,7 @@ func Test_CreateQueueV1_json_with_attributes(t *testing.T) { exp2 := app.ListQueuesResponse{ Xmlns: "http://queue.amazonaws.com/doc/2012-11-05/", - Result: app.ListQueuesResult{QueueUrl: []string{fmt.Sprintf("%s/new-queue-1", sf.BASE_URL)}}, + Result: app.ListQueuesResult{QueueUrl: []string{fmt.Sprintf("%s/new-queue-1", af.BASE_URL)}}, Metadata: app.ResponseMetadata{RequestId: sf.REQUEST_ID}, } r2 := app.ListQueuesResponse{} @@ -192,7 +259,7 @@ func Test_CreateQueueV1_json_with_attributes_ints_as_strings(t *testing.T) { DeadLetterTargetArn string `json:"deadLetterTargetArn"` }{ MaxReceiveCount: "100", - DeadLetterTargetArn: fmt.Sprintf("%s:new-queue-1", sf.BASE_ARN), + DeadLetterTargetArn: fmt.Sprintf("%s:new-queue-1", af.BASE_ARN), }, VisibilityTimeout: "30"}, } @@ -206,7 +273,7 @@ func Test_CreateQueueV1_json_with_attributes_ints_as_strings(t *testing.T) { Status(http.StatusOK). Body().Raw() - exp1 := models.CreateQueueResult{QueueUrl: fmt.Sprintf("%s/new-string-queue", sf.BASE_URL)} + exp1 := models.CreateQueueResult{QueueUrl: fmt.Sprintf("%s/new-string-queue", af.BASE_URL)} r1 := models.CreateQueueResult{} json.Unmarshal([]byte(r), &r1) @@ -219,7 +286,7 @@ func Test_CreateQueueV1_json_with_attributes_ints_as_strings(t *testing.T) { }{ Action: "GetQueueAttributes", Attribute1: "All", - QueueUrl: fmt.Sprintf("%s/new-string-queue", sf.BASE_URL), + QueueUrl: fmt.Sprintf("%s/new-string-queue", af.BASE_URL), } r = e.POST("/"). WithForm(gqar). @@ -231,7 +298,7 @@ func Test_CreateQueueV1_json_with_attributes_ints_as_strings(t *testing.T) { exp3, _ := dupe.(app.GetQueueAttributesResponse) exp3.Result.Attrs[0].Value = "30" exp3.Result.Attrs[1].Value = "1" - exp3.Result.Attrs[7].Value = fmt.Sprintf("%s:new-string-queue", sf.BASE_ARN) + exp3.Result.Attrs[7].Value = fmt.Sprintf("%s:new-string-queue", af.BASE_ARN) exp3.Result.Attrs[8].Value = "{\"maxReceiveCount\": \"100\", \"deadLetterTargetArn\":\"new-queue-1\"}" r3 := app.GetQueueAttributesResponse{} xml.Unmarshal([]byte(r), &r3) @@ -257,7 +324,7 @@ func Test_CreateQueueV1_xml_no_attributes(t *testing.T) { exp1 := models.CreateQueueResponse{ Xmlns: "http://queue.amazonaws.com/doc/2012-11-05/", - Result: models.CreateQueueResult{QueueUrl: fmt.Sprintf("%s/new-queue-1", sf.BASE_URL)}, + Result: models.CreateQueueResult{QueueUrl: fmt.Sprintf("%s/new-queue-1", af.BASE_URL)}, Metadata: app.ResponseMetadata{RequestId: sf.REQUEST_ID}, } @@ -273,7 +340,7 @@ func Test_CreateQueueV1_xml_no_attributes(t *testing.T) { exp2 := app.ListQueuesResponse{ Xmlns: "http://queue.amazonaws.com/doc/2012-11-05/", - Result: app.ListQueuesResult{QueueUrl: []string{fmt.Sprintf("%s/new-queue-1", sf.BASE_URL)}}, + Result: app.ListQueuesResult{QueueUrl: []string{fmt.Sprintf("%s/new-queue-1", af.BASE_URL)}}, Metadata: app.ResponseMetadata{RequestId: sf.REQUEST_ID}, } r2 := app.ListQueuesResponse{} @@ -335,7 +402,7 @@ func Test_CreateQueueV1_xml_with_attributes(t *testing.T) { WithFormField("Attribute.6.Name", "ReceiveMessageWaitTimeSeconds"). WithFormField("Attribute.6.Value", "5"). WithFormField("Attribute.7.Name", "RedrivePolicy"). - WithFormField("Attribute.7.Value", fmt.Sprintf("{\"maxReceiveCount\": 100, \"deadLetterTargetArn\":\"%s:new-queue-1\"}", sf.BASE_ARN)). + WithFormField("Attribute.7.Value", fmt.Sprintf("{\"maxReceiveCount\": 100, \"deadLetterTargetArn\":\"%s:new-queue-1\"}", af.BASE_ARN)). WithFormField("Attribute.8.Name", "RedriveAllowPolicy"). WithFormField("Attribute.8.Value", "{\"this-is\": \"the-redrive-allow-policy\"}"). Expect(). @@ -344,7 +411,7 @@ func Test_CreateQueueV1_xml_with_attributes(t *testing.T) { exp1 := models.CreateQueueResponse{ Xmlns: "http://queue.amazonaws.com/doc/2012-11-05/", - Result: models.CreateQueueResult{QueueUrl: fmt.Sprintf("%s/new-queue-2", sf.BASE_URL)}, + Result: models.CreateQueueResult{QueueUrl: fmt.Sprintf("%s/new-queue-2", af.BASE_URL)}, Metadata: app.ResponseMetadata{RequestId: sf.REQUEST_ID}, } @@ -359,7 +426,7 @@ func Test_CreateQueueV1_xml_with_attributes(t *testing.T) { }{ Action: "GetQueueAttributes", Attribute1: "All", - QueueUrl: fmt.Sprintf("%s/new-queue-2", sf.BASE_URL), + QueueUrl: fmt.Sprintf("%s/new-queue-2", af.BASE_URL), } r = e.POST("/"). WithForm(gqar). @@ -372,7 +439,7 @@ func Test_CreateQueueV1_xml_with_attributes(t *testing.T) { exp3.Result.Attrs[0].Value = "1" exp3.Result.Attrs[1].Value = "3" exp3.Result.Attrs[2].Value = "5" - exp3.Result.Attrs[7].Value = fmt.Sprintf("%s:new-queue-2", sf.BASE_ARN) + exp3.Result.Attrs[7].Value = fmt.Sprintf("%s:new-queue-2", af.BASE_ARN) exp3.Result.Attrs[8].Value = "{\"maxReceiveCount\": \"100\", \"deadLetterTargetArn\":\"new-queue-1\"}" r3 := app.GetQueueAttributesResponse{} xml.Unmarshal([]byte(r), &r3)