forked from Azure/azure-sdk-for-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cncf_events.py
More file actions
124 lines (108 loc) · 4.32 KB
/
test_cncf_events.py
File metadata and controls
124 lines (108 loc) · 4.32 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import json
from devtools_testutils import AzureRecordedTestCase, recorded_by_proxy
from azure.core.credentials import AzureKeyCredential, AzureSasCredential
from azure.eventgrid import EventGridClient, ClientLevel
from cloudevents.http import CloudEvent
from eventgrid_preparer import (
EventGridPreparer,
)
class TestEventGridPublisherClientCncf(AzureRecordedTestCase):
def create_eg_publisher_client(self, endpoint):
credential = self.get_credential(EventGridClient)
client = self.create_client_from_credential(
EventGridClient, credential=credential, endpoint=endpoint, level=ClientLevel.BASIC
)
return client
@EventGridPreparer()
@recorded_by_proxy
def test_send_cncf_data_dict(
self, eventgrid_cloud_event_topic_endpoint
):
client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint)
attributes = {
"type": "com.example.sampletype1",
"source": "https://example.com/event-producer",
}
data = {"message": "Hello World!"}
cloud_event = CloudEvent(attributes, data)
def callback(request):
req = json.loads(request.http_request.body)
assert req[0].get("data") is not None
assert isinstance(req[0], dict)
assert req[0].get("type") == "com.example.sampletype1"
assert req[0].get("source") == "https://example.com/event-producer"
client.send(cloud_event, raw_request_hook=callback)
@EventGridPreparer()
@recorded_by_proxy
def test_send_cncf_data_base64_using_data(
self, eventgrid_cloud_event_topic_endpoint
):
client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint)
attributes = {
"type": "com.example.sampletype1",
"source": "https://example.com/event-producer",
}
data = b"hello world"
cloud_event = CloudEvent(attributes, data)
def callback(request):
req = json.loads(request.http_request.body)
assert req[0].get("data_base64") is not None
assert req[0].get("data") is None
client.send(cloud_event, raw_request_hook=callback)
@EventGridPreparer()
@recorded_by_proxy
def test_send_cncf_data_none(
self, eventgrid_cloud_event_topic_endpoint
):
client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint)
attributes = {
"type": "com.example.sampletype1",
"source": "https://example.com/event-producer",
}
data = None
cloud_event = CloudEvent(attributes, data)
client.send(cloud_event)
@EventGridPreparer()
@recorded_by_proxy
def test_send_cncf_data_str(
self, eventgrid_cloud_event_topic_endpoint
):
client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint)
attributes = {
"type": "com.example.sampletype1",
"source": "https://example.com/event-producer",
}
data = "hello world"
def callback(request):
req = json.loads(request.http_request.body)
assert req[0].get("data_base64") is None
assert req[0].get("data") is not None
cloud_event = CloudEvent(attributes, data)
client.send(cloud_event, raw_request_hook=callback)
@EventGridPreparer()
@recorded_by_proxy
def test_send_cncf_data_as_list(
self, eventgrid_cloud_event_topic_endpoint
):
client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint)
attributes = {
"type": "com.example.sampletype1",
"source": "https://example.com/event-producer",
}
data = "hello world"
cloud_event = CloudEvent(attributes, data)
client.send([cloud_event])
@EventGridPreparer()
@recorded_by_proxy
def test_send_cncf_data_with_extensions(
self, eventgrid_cloud_event_topic_endpoint
):
client = self.create_eg_publisher_client(eventgrid_cloud_event_topic_endpoint)
attributes = {
"type": "com.example.sampletype1",
"source": "https://example.com/event-producer",
"ext1": "extension",
}
data = "hello world"
cloud_event = CloudEvent(attributes, data)
client.send([cloud_event])