Skip to content

Commit

Permalink
Try routing refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
kojisaiki committed Jan 3, 2024
1 parent b013bcf commit eb9d898
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
41 changes: 38 additions & 3 deletions app/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package router
import (
"io"
"net/http"
"strings"

log "github.com/sirupsen/logrus"

Expand Down Expand Up @@ -65,14 +66,15 @@ func health(w http.ResponseWriter, req *http.Request) {
}

func actionHandler(w http.ResponseWriter, req *http.Request) {
action := extractAction(req)
log.WithFields(
log.Fields{
"action": req.FormValue("Action"),
"action": action,
"url": req.URL,
}).Debug("Handling URL request")
fn, ok := routingTable[req.FormValue("Action")]
fn, ok := routingTable[action]
if !ok {
log.Println("Bad Request - Action:", req.FormValue("Action"))
log.Println("Bad Request - Action:", action)
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, "Bad Request")
return
Expand All @@ -85,3 +87,36 @@ func pemHandler(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write(sns.PemKEY)
}

type AwsProtocol int

const (
AwsJsonProtocol AwsProtocol = iota
AwsQueryProtocol AwsProtocol = iota
)

// Extract target Action from the request.
// How contains the Action name is different with aws-query protocol and aws-json protocol.
func extractAction(req *http.Request) string {
protocol := resolveProtocol(req)
switch protocol {
case AwsJsonProtocol:
// Get action from X-Amz-Target header
action := req.Header.Get("X-Amz-Target")
// Action value will be like as "AmazonSQS.CreateQueue".
// After dot should be the action name.
return strings.Split(action, ".")[1]
case AwsQueryProtocol:
return req.FormValue("Action")
}
return ""
}

// Determine which protocol is used.
func resolveProtocol(req *http.Request) AwsProtocol {
// Use content-type to determine protocol
if req.Header.Get("Content-Type") == "application/x-amz-json-1.0" {
return AwsJsonProtocol
}
return AwsQueryProtocol
}
24 changes: 24 additions & 0 deletions app/router/router_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package router

import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -91,6 +93,28 @@ func TestIndexServerhandler_POST_GoodRequest_With_URL(t *testing.T) {
}
}

func TestIndexServerhandler_POST_GoodRequest_With_URL_And_Aws_Json_Protocol(t *testing.T) {
json, _ := json.Marshal(map[string]string{
"QueueName": "local-queue1",
})
req, err := http.NewRequest("POST", "/100010001000/local-queue1", bytes.NewBuffer(json))
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")

rr := httptest.NewRecorder()

New().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)
}
}

func TestIndexServerhandler_GET_GoodRequest_Pem_cert(t *testing.T) {

req, err := http.NewRequest("GET", "/SimpleNotificationService/100010001000.pem", nil)
Expand Down

0 comments on commit eb9d898

Please sign in to comment.