Skip to content

Commit

Permalink
AWS Lambda: Address GHA Validation Issues
Browse files Browse the repository at this point in the history
Signed-off-by: Trevor Bramwell <[email protected]>
  • Loading branch information
bramwelt committed Apr 4, 2024
1 parent d55834d commit a1eb277
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
13 changes: 6 additions & 7 deletions pkg/middlewares/awslambda/aws_lambda.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"context"
"encoding/base64"
"encoding/json"
_ "errors"
"fmt"
"io"
"net/http"
Expand All @@ -58,15 +57,15 @@ const (
)

// awsLambda is a middleware that provides routing to aws lambda
// functions
// functions.
type awsLambda struct {
next http.Handler
functionArn string
name string
client *lambda.Client
}

// New builds a new AwsLambda middleware
// New builds a new AwsLambda middleware.
func New(ctx context.Context, next http.Handler, config dynamic.AWSLambda, name string) (http.Handler, error) {
logger := log.FromContext(middlewares.GetLoggerCtx(ctx, name, typeName))
logger.Debug("Creating middleware")
Expand Down Expand Up @@ -125,7 +124,7 @@ func New(ctx context.Context, next http.Handler, config dynamic.AWSLambda, name
}, nil
}

// GetTracingInformation
// GetTracingInformation.
func (a *awsLambda) GetTracingInformation() (string, ext.SpanKindEnum) {
return a.name, tracing.SpanKindNoneEnum
}
Expand Down Expand Up @@ -224,7 +223,7 @@ func (a *awsLambda) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
}
}

// bodyToBase64 ensures the request body is base64 encoded
// bodyToBase64 ensures the request body is base64 encoded.
func bodyToBase64(req *http.Request) (bool, string, error) {
base64Encoded := false
body := ""
Expand Down Expand Up @@ -254,7 +253,7 @@ func (a *awsLambda) invokeFunction(ctx context.Context, request events.APIGatewa

payload, err := json.Marshal(request)
if err != nil {
return resp, fmt.Errorf("failed to marshal request: %v", err)
return resp, fmt.Errorf("failed to marshal request: %w", err)
}

result, err := a.client.Invoke(ctx, &lambda.InvokeInput{
Expand All @@ -271,7 +270,7 @@ func (a *awsLambda) invokeFunction(ctx context.Context, request events.APIGatewa

err = json.Unmarshal(result.Payload, &resp)
if err != nil {
return resp, fmt.Errorf("failed to unmarshal response: %s, %v", result.Payload, err)
return resp, fmt.Errorf("failed to unmarshal response: %s, %w", result.Payload, err)
}

return resp, nil
Expand Down
7 changes: 4 additions & 3 deletions pkg/middlewares/awslambda/aws_lambda_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ func Test_AWSLambdaMiddleware_Invoke(t *testing.T) {
recorder := httptest.NewRecorder()

var buf bytes.Buffer
buf.Write([]byte("This is the body"))
b := []byte("This is the body")
buf.Write(b)

req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("%s/%s", mockserver.URL, "test/example/path?a=1&b=2&c=3&c=4&d[]=5&d[]=6"), &buf)
if err != nil {
Expand All @@ -83,7 +84,7 @@ func Test_AWSLambdaMiddleware_Invoke(t *testing.T) {
resp := recorder.Result()
rBody, _ := io.ReadAll(resp.Body)

Check failure on line 85 in pkg/middlewares/awslambda/aws_lambda_test.go

View workflow job for this annotation

GitHub Actions / test-unit

rBody declared and not used

Check failure on line 85 in pkg/middlewares/awslambda/aws_lambda_test.go

View workflow job for this annotation

GitHub Actions / validate

rBody declared and not used

assert.Equal(t, rBody, []byte("response_body"))
assert.Equal(t, []byte("response_body"), rbody)

Check failure on line 87 in pkg/middlewares/awslambda/aws_lambda_test.go

View workflow job for this annotation

GitHub Actions / test-unit

undefined: rbody

Check failure on line 87 in pkg/middlewares/awslambda/aws_lambda_test.go

View workflow job for this annotation

GitHub Actions / validate

undefined: rbody (typecheck)
assert.Equal(t, resp.StatusCode, http.StatusTeapot)
}

Expand All @@ -106,7 +107,7 @@ func Test_AWSLambdaMiddleware_GetTracingInformation(t *testing.T) {
// Test_AWSLambdaMiddleware_bodyToBase64_empty
func Test_AWSLambdaMiddleware_bodyToBase64_empty(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, "/", nil)
assert.Nil(t, err)
assert.NoError(t, err)
isEncoded, body, err := bodyToBase64(req)

assert.Equal(t, isEncoded, false)
Expand Down

0 comments on commit a1eb277

Please sign in to comment.