Skip to content

Commit

Permalink
super roughly passed
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 93f700c commit 681ddcd
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 9 deletions.
52 changes: 48 additions & 4 deletions app/gosqs/gosqs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gosqs

import (
"encoding/json"
"encoding/xml"
"fmt"
"net/http"
Expand Down Expand Up @@ -114,9 +115,45 @@ func ListQueues(w http.ResponseWriter, req *http.Request) {
}
}

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

type Attributes map[string]string

func parseCreateQueueRequestBody(w http.ResponseWriter, req *http.Request) (bool, CreateQueueRequest, Attributes) {
requestBody := new(CreateQueueRequest)
attributes := map[string]string{}

byJson := false

switch req.Header.Get("Content-Type") {
case "application/x-amz-json-1.0":
//Read body data to parse json
decoder := json.NewDecoder(req.Body)
err := decoder.Decode(&requestBody)
if err != nil {
panic(err)
}
// TODO: parse from json, and find actual attribute format in aws-json protocol.
attributes["VisibilityTimeout"] = "60"
attributes["MaximumMessageSize"] = "2048"
byJson = true
default:
requestBody.QueueName = req.FormValue("QueueName")
attributes = extractQueueAttributes(req.Form)
}

return byJson, *requestBody, attributes
}

func CreateQueue(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/xml")
queueName := req.FormValue("QueueName")
byJson, requestBody, attr := parseCreateQueueRequestBody(w, req)

queueName := requestBody.QueueName

queueUrl := "http://" + app.CurrentEnvironment.Host + ":" + app.CurrentEnvironment.Port +
"/" + app.CurrentEnvironment.AccountID + "/" + queueName
Expand All @@ -139,9 +176,16 @@ func CreateQueue(w http.ResponseWriter, req *http.Request) {
EnableDuplicates: app.CurrentEnvironment.EnableDuplicates,
Duplicates: make(map[string]time.Time),
}
if err := validateAndSetQueueAttributes(queue, req.Form); err != nil {
createErrorResponse(w, req, err.Error())
return
if byJson {
if err := validateAndSetQueueAttributesJson(queue, attr); err != nil {
createErrorResponse(w, req, err.Error())
return
}
} else {
if err := validateAndSetQueueAttributes(queue, req.Form); err != nil {
createErrorResponse(w, req, err.Error())
return
}
}
app.SyncQueues.Lock()
app.SyncQueues.Queues[queueName] = queue
Expand Down
10 changes: 5 additions & 5 deletions app/gosqs/gosqs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,11 @@ func TestCreateQueuehandler_POST_CreateQueue(t *testing.T) {
}
}

type CreateQueueRequest struct {
QueueName string `json: QueueName`
VisibilityTimeout int `json: VisibilityTimeout`
MaximumMessageSize int `json: MaximumMessageSize`
}
// 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"
Expand Down
5 changes: 5 additions & 0 deletions app/gosqs/queue_attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ var (
// TODO Currently it only supports VisibilityTimeout, MaximumMessageSize, DelaySeconds, RedrivePolicy and ReceiveMessageWaitTimeSeconds attributes.
func validateAndSetQueueAttributes(q *app.Queue, u url.Values) error {
attr := extractQueueAttributes(u)

return validateAndSetQueueAttributesJson(q, attr)
}

func validateAndSetQueueAttributesJson(q *app.Queue, attr Attributes) error {
visibilityTimeout, _ := strconv.Atoi(attr["VisibilityTimeout"])
if visibilityTimeout != 0 {
q.TimeoutSecs = visibilityTimeout
Expand Down

0 comments on commit 681ddcd

Please sign in to comment.