Skip to content

Commit

Permalink
add sample test
Browse files Browse the repository at this point in the history
  • Loading branch information
kojisaiki authored and Admiral-Piett committed Sep 20, 2024
1 parent 615ad59 commit 93f700c
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions app/gosqs/gosqs_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package gosqs

import (
"bytes"
"context"
"encoding/json"
"encoding/xml"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -145,6 +147,62 @@ func TestCreateQueuehandler_POST_CreateQueue(t *testing.T) {
}
}

type CreateQueueRequest struct {
QueueName string `json: QueueName`
VisibilityTimeout int `json: VisibilityTimeout`
MaximumMessageSize int `json: MaximumMessageSize`
}

func TestCreateQueuehandler_POST_CreateQueue_aws_json(t *testing.T) {
queueName := "UnitTestQueue1"
requestObj := new(CreateQueueRequest)
requestObj.QueueName = queueName
requestObj.VisibilityTimeout = 60
requestObj.MaximumMessageSize = 2048
requestJson, _ := json.Marshal(requestObj)
// Create a request to pass to our handler. We don't have any query parameters for now, so we'll
// pass 'nil' as the third parameter.
req, err := http.NewRequest("POST", "/", bytes.NewBuffer(requestJson))
if err != nil {
t.Fatal(err)
}
req.Header.Set("X-Amz-Target", "AmazonSQS.CreateQueue")
req.Header.Set("Content-Type", "application/x-amz-json-1.0")

// We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
rr := httptest.NewRecorder()
handler := http.HandlerFunc(CreateQueue)

// Our handlers satisfy http.Handler, so we can call their ServeHTTP method
// directly and pass in our Request and ResponseRecorder.
handler.ServeHTTP(rr, req)

// Check the status code is what we expect.
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}

// Check the response body is what we expect.
expected := queueName
if !strings.Contains(rr.Body.String(), expected) {
t.Errorf("handler returned unexpected body: got %v want %v",
rr.Body.String(), expected)
}
expectedQueue := &app.Queue{
Name: queueName,
URL: "http://://" + queueName,
Arn: "arn:aws:sqs:::" + queueName,
TimeoutSecs: 60,
MaximumMessageSize: 2048,
Duplicates: make(map[string]time.Time),
}
actualQueue := app.SyncQueues.Queues[queueName]
if !reflect.DeepEqual(expectedQueue, actualQueue) {
t.Fatalf("expected %+v, got %+v", expectedQueue, actualQueue)
}
}

func TestCreateFIFOQueuehandler_POST_CreateQueue(t *testing.T) {
req, err := http.NewRequest("POST", "/", nil)
if err != nil {
Expand Down

0 comments on commit 93f700c

Please sign in to comment.