Skip to content
This repository has been archived by the owner on Dec 11, 2023. It is now read-only.

Commit

Permalink
Merge pull request #56 from ManoManoTech/lambda-contract
Browse files Browse the repository at this point in the history
feat: align expected lambda headers
  • Loading branch information
tzununbekov authored May 26, 2023
2 parents 9a41b40 + 643bcf2 commit be9735d
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 5 deletions.
10 changes: 6 additions & 4 deletions pkg/converter/cloudevents/cloudevents.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ import (

// CloudEvents request constant attributes.
const (
ContentType = "application/cloudevents+json"
ContextKey = "Lambda-Runtime-Cloudevents-Context"
ContentType = "application/cloudevents+json"
CeContextKey = "Lambda-Runtime-Cloudevents-Context"
ClientContextKey = "Lambda-Runtime-Client-Context"
)

// CloudEvent is a data structure required to map KLR responses to cloudevents
Expand Down Expand Up @@ -142,11 +143,12 @@ func (ce *CloudEvent) Request(request []byte, headers http.Header) ([]byte, map[

ceContext, err := json.Marshal(context)
if err != nil {
return nil, nil, fmt.Errorf("cannot encode request context: %w", err)
return nil, nil, fmt.Errorf("cannot encode request event context: %w", err)
}

runtimeContext := map[string]string{
ContextKey: string(ceContext),
ClientContextKey: fmt.Sprintf("{\"custom\":%s}", ceContext),
CeContextKey: string(ceContext),
}

return body, runtimeContext, nil
Expand Down
73 changes: 73 additions & 0 deletions pkg/converter/cloudevents/cloudevents_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package cloudevents

import (
"net/http"
"reflect"
"testing"
)

func TestCloudEvent_Request(t *testing.T) {
tests := []struct {
name string
request string
headers http.Header
expectedBody string
expectedRuntimeContext map[string]string
wantErr bool
}{
{
name: "Event of type application/cloudevents+json",
request: `{"source":"test","data":{"foo":"bar"}}`,
headers: http.Header{
"Content-Type": {"application/cloudevents+json"},
},
expectedBody: `{"foo":"bar"}`,
expectedRuntimeContext: map[string]string{
CeContextKey: `{"source":"test"}`,
ClientContextKey: `{"custom":{"source":"test"}}`,
},
},
{
name: "Event of type application/json",
request: `{"foo":"bar"}`,
headers: http.Header{
"Content-Type": {"application/json"},
"ce-source": {"test"},
},
expectedBody: `{"foo":"bar"}`,
expectedRuntimeContext: map[string]string{
CeContextKey: `{"source":"test"}`,
ClientContextKey: `{"custom":{"source":"test"}}`,
},
},
{
name: "Event of other type",
request: `hello world`,
headers: http.Header{
"Content-Type": {"text/plain"},
},
expectedBody: `hello world`,
expectedRuntimeContext: nil,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ce := &CloudEvent{}
body, runtimeContext, err := ce.Request([]byte(tt.request), tt.headers)

if (err != nil) != tt.wantErr {
t.Errorf("Request() error = %v, wantErr %v", err, tt.wantErr)
return
}

if !reflect.DeepEqual(string(body), tt.expectedBody) {
t.Errorf("Request() got = %v, want %v", string(body), tt.expectedBody)
}

if !reflect.DeepEqual(runtimeContext, tt.expectedRuntimeContext) {
t.Errorf("Request() got1 = %v, want %v", runtimeContext, tt.expectedRuntimeContext)
}
})
}
}
2 changes: 1 addition & 1 deletion pkg/metrics/cloudevents.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func CETagsFromContext(context map[string]string) (tag.Mutator, tag.Mutator) {
if context == nil {
return DefaultRequestType, DefaultRequestSource
}
ceContext, exists := context[cloudevents.ContextKey]
ceContext, exists := context[cloudevents.CeContextKey]
if !exists {
return DefaultRequestType, DefaultRequestSource
}
Expand Down

0 comments on commit be9735d

Please sign in to comment.