From bc027e4ec99c700f0935c75c9a57f6cc21157cb2 Mon Sep 17 00:00:00 2001 From: "Michael B. Klein" Date: Wed, 15 Mar 2023 23:08:02 +0000 Subject: [PATCH] Add tests for GetQueueAttributes (all and selected) --- .github/README.md | 2 +- app/gosqs/gosqs.go | 2 +- app/gosqs/gosqs_test.go | 147 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 149 insertions(+), 2 deletions(-) diff --git a/.github/README.md b/.github/README.md index 68b0f6bd..3c59106e 100644 --- a/.github/README.md +++ b/.github/README.md @@ -14,7 +14,7 @@ All SNS/SQS APIs have been implemented except: Here is a list of the APIs: - [x] ListQueues - [x] CreateQueue - - [x] GetQueueAttributes (Always returns all attributes - unsupporterd arttributes are mocked) + - [x] GetQueueAttributes (unsupported attributes are mocked) - [x] GetQueueUrl - [x] SendMessage - [x] SendMessageBatch diff --git a/app/gosqs/gosqs.go b/app/gosqs/gosqs.go index f4a98291..79ef356d 100644 --- a/app/gosqs/gosqs.go +++ b/app/gosqs/gosqs.go @@ -866,7 +866,7 @@ func GetQueueAttributes(w http.ResponseWriter, req *http.Request) { attribs = append(attribs, attr) } if include_attr("DelaySeconds") { - attr = app.Attribute{Name: "DelaySeconds", Value: strconv.Itoa(queue.DelaySecs)} + attr := app.Attribute{Name: "DelaySeconds", Value: strconv.Itoa(queue.DelaySecs)} attribs = append(attribs, attr) } if include_attr("ReceiveMessageWaitTimeSeconds") { diff --git a/app/gosqs/gosqs_test.go b/app/gosqs/gosqs_test.go index 4581a25c..7298b170 100644 --- a/app/gosqs/gosqs_test.go +++ b/app/gosqs/gosqs_test.go @@ -1939,6 +1939,153 @@ func TestSendMessage_POST_DelaySeconds(t *testing.T) { } } +func TestGetQueueAttributes_GetAllAttributes(t *testing.T) { + done := make(chan struct{}, 0) + go PeriodicTasks(1*time.Second, done) + + // create a queue + req, err := http.NewRequest("POST", "/", nil) + if err != nil { + t.Fatal(err) + } + + form := url.Values{} + form.Add("Action", "CreateQueue") + form.Add("QueueName", "get-queue-attributes") + form.Add("Version", "2012-11-05") + req.PostForm = form + + rr := httptest.NewRecorder() + http.HandlerFunc(CreateQueue).ServeHTTP(rr, req) + + if status := rr.Code; status != http.StatusOK { + t.Errorf("handler returned wrong status code: got \n%v want %v", + status, http.StatusOK) + } + + // get queue attributes + req, err = http.NewRequest("GET", "/queue/get-queue-attributes?Action=GetQueueAttributes&AttributeName.1=All", nil) + if err != nil { + t.Fatal(err) + } + + rr = httptest.NewRecorder() + http.HandlerFunc(GetQueueAttributes).ServeHTTP(rr, req) + + if status := rr.Code; status != http.StatusOK { + t.Errorf("handler returned wrong status code: got \n%v want %v", + status, http.StatusOK) + } + + resp := app.GetQueueAttributesResponse{} + err = xml.Unmarshal(rr.Body.Bytes(), &resp) + if err != nil { + t.Fatalf("unexpected unmarshal error: %s", err) + } + + hasAttribute := func(attrs []app.Attribute, name string) bool { + for _, attr := range attrs { + if attr.Name == name { + return true + } + } + return false + } + + + ok := hasAttribute(resp.Result.Attrs, "VisibilityTimeout") && + hasAttribute(resp.Result.Attrs, "DelaySeconds") && + hasAttribute(resp.Result.Attrs, "ReceiveMessageWaitTimeSeconds") && + hasAttribute(resp.Result.Attrs, "ApproximateNumberOfMessages") && + hasAttribute(resp.Result.Attrs, "ApproximateNumberOfMessagesNotVisible") && + hasAttribute(resp.Result.Attrs, "CreatedTimestamp") && + hasAttribute(resp.Result.Attrs, "LastModifiedTimestamp") && + hasAttribute(resp.Result.Attrs, "QueueArn") && + hasAttribute(resp.Result.Attrs, "RedrivePolicy") + + if !ok { + t.Fatal("handler should return all attributes") + } + + done <- struct{}{} +} + +func TestGetQueueAttributes_GetSelectedAttributes(t *testing.T) { + done := make(chan struct{}, 0) + go PeriodicTasks(1*time.Second, done) + + // create a queue + req, err := http.NewRequest("POST", "/", nil) + if err != nil { + t.Fatal(err) + } + + form := url.Values{} + form.Add("Action", "CreateQueue") + form.Add("QueueName", "get-queue-attributes") + form.Add("Version", "2012-11-05") + req.PostForm = form + + rr := httptest.NewRecorder() + http.HandlerFunc(CreateQueue).ServeHTTP(rr, req) + + if status := rr.Code; status != http.StatusOK { + t.Errorf("handler returned wrong status code: got \n%v want %v", + status, http.StatusOK) + } + + // get queue attributes + req, err = http.NewRequest("GET", "/queue/get-queue-attributes?Action=GetQueueAttributes&AttributeName.1=ApproximateNumberOfMessages&AttributeName.2=ApproximateNumberOfMessagesNotVisible&AttributeName.2=ApproximateNumberOfMessagesNotVisible", nil) + if err != nil { + t.Fatal(err) + } + + rr = httptest.NewRecorder() + http.HandlerFunc(GetQueueAttributes).ServeHTTP(rr, req) + + if status := rr.Code; status != http.StatusOK { + t.Errorf("handler returned wrong status code: got \n%v want %v", + status, http.StatusOK) + } + + resp := app.GetQueueAttributesResponse{} + err = xml.Unmarshal(rr.Body.Bytes(), &resp) + if err != nil { + t.Fatalf("unexpected unmarshal error: %s", err) + } + + hasAttribute := func(attrs []app.Attribute, name string) bool { + for _, attr := range attrs { + if attr.Name == name { + return true + } + } + return false + } + + + ok := hasAttribute(resp.Result.Attrs, "ApproximateNumberOfMessages") && + hasAttribute(resp.Result.Attrs, "ApproximateNumberOfMessagesNotVisible") + + if !ok { + t.Fatal("handler should return requested attributes") + } + + ok = !(hasAttribute(resp.Result.Attrs, "VisibilityTimeout") || + hasAttribute(resp.Result.Attrs, "DelaySeconds") || + hasAttribute(resp.Result.Attrs, "ReceiveMessageWaitTimeSeconds") || + hasAttribute(resp.Result.Attrs, "CreatedTimestamp") || + hasAttribute(resp.Result.Attrs, "LastModifiedTimestamp") || + hasAttribute(resp.Result.Attrs, "QueueArn") || + hasAttribute(resp.Result.Attrs, "RedrivePolicy")) + + if !ok { + t.Fatal("handler should return only requested attributes") + } + + done <- struct{}{} +} + // waitTimeout waits for the waitgroup for the specified max timeout. // Returns true if waiting timed out. // credits: https://stackoverflow.com/questions/32840687/timeout-for-waitgroup-wait