Skip to content

Commit

Permalink
Add tests for GetQueueAttributes (all and selected)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbklein authored and Admiral-Piett committed Mar 22, 2023
1 parent 3d9cd84 commit bc027e4
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/gosqs/gosqs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
147 changes: 147 additions & 0 deletions app/gosqs/gosqs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bc027e4

Please sign in to comment.