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

Commit be9735d

Browse files
authored
Merge pull request #56 from ManoManoTech/lambda-contract
feat: align expected lambda headers
2 parents 9a41b40 + 643bcf2 commit be9735d

File tree

3 files changed

+80
-5
lines changed

3 files changed

+80
-5
lines changed

pkg/converter/cloudevents/cloudevents.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ import (
3131

3232
// CloudEvents request constant attributes.
3333
const (
34-
ContentType = "application/cloudevents+json"
35-
ContextKey = "Lambda-Runtime-Cloudevents-Context"
34+
ContentType = "application/cloudevents+json"
35+
CeContextKey = "Lambda-Runtime-Cloudevents-Context"
36+
ClientContextKey = "Lambda-Runtime-Client-Context"
3637
)
3738

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

143144
ceContext, err := json.Marshal(context)
144145
if err != nil {
145-
return nil, nil, fmt.Errorf("cannot encode request context: %w", err)
146+
return nil, nil, fmt.Errorf("cannot encode request event context: %w", err)
146147
}
147148

148149
runtimeContext := map[string]string{
149-
ContextKey: string(ceContext),
150+
ClientContextKey: fmt.Sprintf("{\"custom\":%s}", ceContext),
151+
CeContextKey: string(ceContext),
150152
}
151153

152154
return body, runtimeContext, nil
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package cloudevents
2+
3+
import (
4+
"net/http"
5+
"reflect"
6+
"testing"
7+
)
8+
9+
func TestCloudEvent_Request(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
request string
13+
headers http.Header
14+
expectedBody string
15+
expectedRuntimeContext map[string]string
16+
wantErr bool
17+
}{
18+
{
19+
name: "Event of type application/cloudevents+json",
20+
request: `{"source":"test","data":{"foo":"bar"}}`,
21+
headers: http.Header{
22+
"Content-Type": {"application/cloudevents+json"},
23+
},
24+
expectedBody: `{"foo":"bar"}`,
25+
expectedRuntimeContext: map[string]string{
26+
CeContextKey: `{"source":"test"}`,
27+
ClientContextKey: `{"custom":{"source":"test"}}`,
28+
},
29+
},
30+
{
31+
name: "Event of type application/json",
32+
request: `{"foo":"bar"}`,
33+
headers: http.Header{
34+
"Content-Type": {"application/json"},
35+
"ce-source": {"test"},
36+
},
37+
expectedBody: `{"foo":"bar"}`,
38+
expectedRuntimeContext: map[string]string{
39+
CeContextKey: `{"source":"test"}`,
40+
ClientContextKey: `{"custom":{"source":"test"}}`,
41+
},
42+
},
43+
{
44+
name: "Event of other type",
45+
request: `hello world`,
46+
headers: http.Header{
47+
"Content-Type": {"text/plain"},
48+
},
49+
expectedBody: `hello world`,
50+
expectedRuntimeContext: nil,
51+
},
52+
}
53+
54+
for _, tt := range tests {
55+
t.Run(tt.name, func(t *testing.T) {
56+
ce := &CloudEvent{}
57+
body, runtimeContext, err := ce.Request([]byte(tt.request), tt.headers)
58+
59+
if (err != nil) != tt.wantErr {
60+
t.Errorf("Request() error = %v, wantErr %v", err, tt.wantErr)
61+
return
62+
}
63+
64+
if !reflect.DeepEqual(string(body), tt.expectedBody) {
65+
t.Errorf("Request() got = %v, want %v", string(body), tt.expectedBody)
66+
}
67+
68+
if !reflect.DeepEqual(runtimeContext, tt.expectedRuntimeContext) {
69+
t.Errorf("Request() got1 = %v, want %v", runtimeContext, tt.expectedRuntimeContext)
70+
}
71+
})
72+
}
73+
}

pkg/metrics/cloudevents.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func CETagsFromContext(context map[string]string) (tag.Mutator, tag.Mutator) {
4040
if context == nil {
4141
return DefaultRequestType, DefaultRequestSource
4242
}
43-
ceContext, exists := context[cloudevents.ContextKey]
43+
ceContext, exists := context[cloudevents.CeContextKey]
4444
if !exists {
4545
return DefaultRequestType, DefaultRequestSource
4646
}

0 commit comments

Comments
 (0)