Skip to content

Commit

Permalink
MLPAB-2035 Add setup for pact test of getlist and fix output (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
hawx authored Apr 23, 2024
1 parent 2f7c7f1 commit 077ebd8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 10 additions & 4 deletions lambda/getlist/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
Expand All @@ -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()
Expand Down
12 changes: 9 additions & 3 deletions mock-apigw/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"crypto/rand"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -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"
Expand All @@ -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 {
Expand All @@ -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,
Expand Down

0 comments on commit 077ebd8

Please sign in to comment.