Skip to content

Commit d3f811d

Browse files
authored
Merge pull request #53 from lizzzcai/refine-sync-http-variables-tests
update cloud event input data to json format
2 parents d08c60f + 480fa48 commit d3f811d

6 files changed

Lines changed: 30 additions & 13 deletions

File tree

test/declarative/sync-http-variables/http.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package main
22

33
import (
4+
"context"
45
"encoding/json"
56
"net/http"
6-
"context"
77

88
ofctx "github.com/OpenFunction/functions-framework-go/context"
99
"github.com/OpenFunction/functions-framework-go/functions"
@@ -17,16 +17,20 @@ func init() {
1717
functions.WithFunctionMethods("GET", "POST"),
1818
)
1919

20-
functions.CloudEvent("Foo", foo,
20+
functions.CloudEvent("Foo", foo,
2121
functions.WithFunctionPath("/foo/{name}"),
2222
)
2323

24-
functions.OpenFunction("Bar", bar,
24+
functions.OpenFunction("Bar", bar,
2525
functions.WithFunctionPath("/bar/{name}"),
2626
functions.WithFunctionMethods("GET", "POST"),
2727
)
2828
}
2929

30+
type Message struct {
31+
Data string `json:"data"`
32+
}
33+
3034
func hello(w http.ResponseWriter, r *http.Request) {
3135
vars := ofctx.VarsFromCtx(r.Context())
3236
response := map[string]string{
@@ -39,8 +43,15 @@ func hello(w http.ResponseWriter, r *http.Request) {
3943

4044
func foo(ctx context.Context, ce cloudevents.Event) error {
4145
vars := ofctx.VarsFromCtx(ctx)
46+
47+
msg := &Message{}
48+
err := json.Unmarshal(ce.Data(), msg)
49+
if err != nil {
50+
return err
51+
}
52+
4253
response := map[string]string{
43-
string(ce.Data()): vars["name"],
54+
msg.Data: vars["name"],
4455
}
4556
responseBytes, _ := json.Marshal(response)
4657
klog.Infof("cloudevent - Data: %s", string(responseBytes))
@@ -49,8 +60,14 @@ func foo(ctx context.Context, ce cloudevents.Event) error {
4960

5061
func bar(ctx ofctx.Context, in []byte) (ofctx.Out, error) {
5162
vars := ofctx.VarsFromCtx(ctx.GetNativeContext())
63+
msg := &Message{}
64+
err := json.Unmarshal(in, msg)
65+
if err != nil {
66+
return ctx.ReturnOnInternalError(), err
67+
}
68+
5269
response := map[string]string{
53-
string(in): vars["name"],
70+
msg.Data: vars["name"],
5471
}
5572
responseBytes, _ := json.Marshal(response)
5673
return ctx.ReturnOnSuccess().WithData(responseBytes), nil

test/declarative/sync-http-variables/verify-cloudevent-binary.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
url=http://$1
44
while true; do
5-
st=$(curl -s -o /dev/null -w "%{http_code}" "$url" -H "Ce-Specversion: 1.0" -H "Ce-Type: io.openfunction.samples.helloworld" -H "Ce-Source: io.openfunction.samples/helloworldsource" -H "Ce-Id: 536808d3-88be-4077-9d7a-a3f162705f79" -H "Content-Type: application/json" -d 'hello')
5+
st=$(curl -s -o /dev/null -w "%{http_code}" "$url" -H "Ce-Specversion: 1.0" -H "Ce-Type: io.openfunction.samples.helloworld" -H "Ce-Source: io.openfunction.samples/helloworldsource" -H "Ce-Id: 536808d3-88be-4077-9d7a-a3f162705f79" -H "Content-Type: application/json" -d '{"data":"hello"}')
66
if [ "$st" -eq 200 ]; then
77
data_result=$(KUBECONFIG=/tmp/e2e-k8s.config kubectl logs --tail=2 -l app="sync-http-variables" -c http | grep Data | awk '{ print $8 }' | yq -P '.' -)
88
plugin_result=$(KUBECONFIG=/tmp/e2e-k8s.config kubectl logs --tail=2 -l app="sync-http-variables" -c http | grep plugin | awk '{ print $8 }' | yq -P '.' -)

test/declarative/sync-http-variables/verify-cloudevent-structured.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
url=http://$1
44
while true; do
5-
st=$(curl -s -o /dev/null -w "%{http_code}" "$url" -H "Content-Type: application/json" -d '{"specversion" : "1.0", "type" : "example.com.cloud.event", "source" : "https://example.com/cloudevents/pull", "subject" : "123", "id" : "A234-1234-1234", "time" : "2018-04-05T17:31:00Z", "data" : "hello"}')
5+
st=$(curl -s -o /dev/null -w "%{http_code}" "$url" -H "Content-Type: application/cloudevents+json" -d '{"specversion":"1.0","type":"dev.knative.samples.helloworld","source":"dev.knative.samples/helloworldsource","id":"536808d3-88be-4077-9d7a-a3f162705f79","data":{"data":"hello"}}')
66
if [ "$st" -eq 200 ]; then
77
data_result=$(KUBECONFIG=/tmp/e2e-k8s.config kubectl logs --tail=2 -l app="sync-http-variables" -c http | grep Data | awk '{ print $8 }' | yq -P '.' -)
88
plugin_result=$(KUBECONFIG=/tmp/e2e-k8s.config kubectl logs --tail=2 -l app="sync-http-variables" -c http | grep plugin | awk '{ print $8 }' | yq -P '.' -)

test/declarative/sync-http-variables/verify-ofn-cloudevent-binary.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
url=http://$1
44
while true; do
5-
st=$(curl -s -o /dev/null -w "%{http_code}" "$url" -H "Ce-Specversion: 1.0" -H "Ce-Type: io.openfunction.samples.helloworld" -H "Ce-Source: io.openfunction.samples/helloworldsource" -H "Ce-Id: 536808d3-88be-4077-9d7a-a3f162705f79" -H "Content-Type: application/json" -d 'hello')
5+
st=$(curl -s -o /dev/null -w "%{http_code}" "$url" -H "Ce-Specversion: 1.0" -H "Ce-Type: io.openfunction.samples.helloworld" -H "Ce-Source: io.openfunction.samples/helloworldsource" -H "Ce-Id: 536808d3-88be-4077-9d7a-a3f162705f79" -H "Content-Type: application/json" -d '{"data":"hello"}')
66
if [ "$st" -eq 200 ]; then
7-
data_result=$(curl -X POST -H "Content-type: application/json" -H "Accept: application/json" -s "$url" -H "Ce-Specversion: 1.0" -H "Ce-Type: io.openfunction.samples.helloworld" -H "Ce-Source: io.openfunction.samples/helloworldsource" -H "Ce-Id: 536808d3-88be-4077-9d7a-a3f162705f79" -H "Content-Type: application/json" -d 'hello' | yq -P ".")
7+
data_result=$(curl -X POST -H "Content-type: application/json" -H "Accept: application/json" -s "$url" -H "Ce-Specversion: 1.0" -H "Ce-Type: io.openfunction.samples.helloworld" -H "Ce-Source: io.openfunction.samples/helloworldsource" -H "Ce-Id: 536808d3-88be-4077-9d7a-a3f162705f79" -H "Content-Type: application/json" -d '{"data":"hello"}' | yq -P ".")
88
plugin_result=$(KUBECONFIG=/tmp/e2e-k8s.config kubectl logs --tail=1 -l app="sync-http-variables" -c http | grep plugin | awk '{ print $8 }' | yq -P '.' -)
99
break
1010
else

test/declarative/sync-http-variables/verify-ofn-cloudevent-structured.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
url=http://$1
44
while true; do
5-
st=$(curl -s -o /dev/null -w "%{http_code}" "$url" -H "Content-Type: application/json" -d '{"specversion" : "1.0", "type" : "example.com.cloud.event", "source" : "https://example.com/cloudevents/pull", "subject" : "123", "id" : "A234-1234-1234", "time" : "2018-04-05T17:31:00Z", "data" : "hello"}')
5+
st=$(curl -s -o /dev/null -w "%{http_code}" "$url" -H "Content-Type: application/cloudevents+json" -d '{"specversion":"1.0","type":"dev.knative.samples.helloworld","source":"dev.knative.samples/helloworldsource","id":"536808d3-88be-4077-9d7a-a3f162705f79","data":{"data":"hello"}}')
66
if [ "$st" -eq 200 ]; then
7-
data_result=$(curl -X POST -H "Content-Type: application/json" -d '{"specversion" : "1.0", "type" : "example.com.cloud.event", "source" : "https://example.com/cloudevents/pull", "subject" : "123", "id" : "A234-1234-1234", "time" : "2018-04-05T17:31:00Z", "data" : "hello"}' | yq -P ".")
7+
data_result=$(curl -X POST -H "Content-Type: application/cloudevents+json" -H "Accept: application/json" -s "$url" -d '{"specversion":"1.0","type":"dev.knative.samples.helloworld","source":"dev.knative.samples/helloworldsource","id":"536808d3-88be-4077-9d7a-a3f162705f79","data":{"data":"hello"}}' | yq -P ".")
88
plugin_result=$(KUBECONFIG=/tmp/e2e-k8s.config kubectl logs --tail=1 -l app="sync-http-variables" -c http | grep plugin | awk '{ print $8 }' | yq -P '.' -)
99
break
1010
else

test/declarative/sync-http-variables/verify-ofn-http.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
url=http://$1
44
while true; do
5-
st=$(curl -s -o /dev/null -w "%{http_code}" -X GET "$url")
5+
st=$(curl -s -o /dev/null -w "%{http_code}" -X GET "$url" -d '{"data":"hello"}')
66
if [ "$st" -eq 200 ]; then
7-
data_result=$(curl -X POST -H "Content-type: application/json" -H "Accept: application/json" -s "$url" -d 'hello' | yq -P ".")
7+
data_result=$(curl -X POST -H "Content-type: application/json" -H "Accept: application/json" -s "$url" -d '{"data":"hello"}' | yq -P ".")
88
plugin_result=$(KUBECONFIG=/tmp/e2e-k8s.config kubectl logs --tail=1 -l app="sync-http-variables" -c http | awk '{ print $8 }' | yq -P '.' -)
99
break
1010
else

0 commit comments

Comments
 (0)