-
Notifications
You must be signed in to change notification settings - Fork 214
/
direct.go
102 lines (79 loc) · 2.43 KB
/
direct.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
Copyright 2021 The CloudEvents Authors
SPDX-License-Identifier: Apache-2.0
*/
package http
import (
"context"
"net/http/httptest"
"testing"
"time"
"github.com/cloudevents/sdk-go/v2/client"
"github.com/google/uuid"
cloudevents "github.com/cloudevents/sdk-go/v2"
cehttp "github.com/cloudevents/sdk-go/v2/protocol/http"
)
// Direct Test:
// Obj -> Send -> Wire Format -> Receive -> Got
// Given: ^ ^ ^==Want
// Obj is an event of a version.
// Client is a set to binary or
type DirectTapTest struct {
now time.Time
event *cloudevents.Event
serverReturnedStatusCode int
want *cloudevents.Event
wantResult cloudevents.Result
asSent *TapValidation
}
type DirectTapTestCases map[string]DirectTapTest
func ClientDirect(t *testing.T, tc DirectTapTest, copts ...client.Option) {
tap := NewTap()
tap.statusCode = tc.serverReturnedStatusCode
server := httptest.NewServer(tap)
defer server.Close()
opts := make([]cehttp.Option, 0)
opts = append(opts, cloudevents.WithTarget(server.URL))
opts = append(opts, cloudevents.WithPort(0)) // random port
protocol, err := cloudevents.NewHTTP(opts...)
if err != nil {
t.Fatal(err)
}
tap.handler = protocol
copts = append(copts, cloudevents.WithEventDefaulter(AlwaysThen(tc.now)))
ce, err := cloudevents.NewClient(protocol, copts...)
if err != nil {
t.Fatal(err)
}
testID := uuid.New().String()
tc.event.SetExtension(unitTestIDKey, testID)
recvCtx, recvCancel := context.WithTimeout(context.Background(), time.Second*5)
defer recvCancel()
var got *cloudevents.Event
go func() {
if err := ce.StartReceiver(recvCtx, func(event cloudevents.Event) {
event.SetExtension(unitTestIDKey, nil)
got = &event
recvCancel()
}); err != nil {
t.Log(err)
}
}()
result := ce.Send(context.Background(), *tc.event)
if result != nil {
if tc.wantResult == nil {
if !cloudevents.IsACK(result) {
t.Errorf("expected ACK, got %s", result)
}
} else if !cloudevents.ResultIs(result, tc.wantResult) {
t.Errorf("result.IsUndelivered = %v", cloudevents.IsUndelivered(result))
t.Fatalf("expected %s, got %s", tc.wantResult, result)
}
}
// Wait until the receiver is done.
<-recvCtx.Done()
assertEventEqualityExact(t, "event", tc.want, got)
if req, ok := tap.req[testID]; ok {
assertTappedEquality(t, "http request", tc.asSent, &req)
}
}