From 077ebd8da38c8d100e4464038f091ec7d380648c Mon Sep 17 00:00:00 2001 From: Joshua Hawxwell Date: Tue, 23 Apr 2024 09:42:13 +0100 Subject: [PATCH] MLPAB-2035 Add setup for pact test of getlist and fix output (#178) --- Makefile | 2 +- lambda/getlist/main.go | 14 ++++++++++---- mock-apigw/main.go | 12 +++++++++--- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 14df08f2..6bc03c45 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ help: @grep --no-filename -E '^[0-9a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' build: ## Build containers - docker compose build --parallel lambda-create lambda-update lambda-get apigw + docker compose build --parallel lambda-create lambda-update lambda-get lambda-getlist apigw up: ## Start application docker compose up -d apigw diff --git a/lambda/getlist/main.go b/lambda/getlist/main.go index 19ffc013..1e22ac46 100644 --- a/lambda/getlist/main.go +++ b/lambda/getlist/main.go @@ -35,6 +35,14 @@ type Lambda struct { logger Logger } +type lpasRequest struct { + UIDs []string `json:"uids"` +} + +type lpasResponse struct { + Lpas []shared.Lpa `json:"lpas"` +} + func (l *Lambda) HandleEvent(ctx context.Context, event events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) { _, err := l.verifier.VerifyHeader(event) if err != nil { @@ -49,9 +57,7 @@ func (l *Lambda) HandleEvent(ctx context.Context, event events.APIGatewayProxyRe Body: "{\"code\":\"INTERNAL_SERVER_ERROR\",\"detail\":\"Internal server error\"}", } - var req struct { - UIDs []string `json:"uids"` - } + var req lpasRequest if err := json.Unmarshal([]byte(event.Body), &req); err != nil { l.logger.Error("error unmarshalling request", slog.Any("err", err)) return shared.ProblemInternalServerError.Respond() @@ -63,7 +69,7 @@ func (l *Lambda) HandleEvent(ctx context.Context, event events.APIGatewayProxyRe return shared.ProblemInternalServerError.Respond() } - body, err := json.Marshal(lpas) + body, err := json.Marshal(lpasResponse{Lpas: lpas}) if err != nil { l.logger.Error("error marshalling LPA", slog.Any("err", err)) return shared.ProblemInternalServerError.Respond() diff --git a/mock-apigw/main.go b/mock-apigw/main.go index 0aef6ae9..401af492 100644 --- a/mock-apigw/main.go +++ b/mock-apigw/main.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "crypto/rand" "encoding/json" "fmt" @@ -35,6 +36,9 @@ func delegateHandler(w http.ResponseWriter, r *http.Request) { return } + var reqBody bytes.Buffer + _, _ = io.Copy(&reqBody, r.Body) + if LPAPath.MatchString(r.URL.Path) && r.Method == http.MethodPut { uid = LPAPath.FindStringSubmatch(r.URL.Path)[1] lambdaName = "create" @@ -46,6 +50,11 @@ func delegateHandler(w http.ResponseWriter, r *http.Request) { lambdaName = "update" } else if r.URL.Path == "/lpas" && r.Method == http.MethodPost { lambdaName = "getlist" + bs := reqBody.Bytes() + for oldUID, newUID := range uidMap { + bs = bytes.ReplaceAll(bs, []byte(oldUID), []byte(newUID)) + } + reqBody = *bytes.NewBuffer(bs) } if newUID, ok := uidMap[uid]; ok { @@ -59,9 +68,6 @@ func delegateHandler(w http.ResponseWriter, r *http.Request) { url := fmt.Sprintf("http://lambda-%s:8080/2015-03-31/functions/function/invocations", lambdaName) - reqBody := new(strings.Builder) - _, _ = io.Copy(reqBody, r.Body) - body := events.APIGatewayProxyRequest{ Body: reqBody.String(), Path: r.URL.Path,