Skip to content

Commit 5cfb987

Browse files
committed
Few more tests
Signed-off-by: Denis Makogon <[email protected]>
1 parent 9122458 commit 5cfb987

File tree

6 files changed

+152
-39
lines changed

6 files changed

+152
-39
lines changed

cloudevents/sdk/converters/base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ class Converter(object):
2121

2222
TYPE = None
2323

24-
def __init__(self,
25-
event_class: base.BaseEvent,
26-
supported_media_types: typing.Mapping[str, bool]):
24+
def __init__(
25+
self, event_class: base.BaseEvent,
26+
supported_media_types: typing.Mapping[str, bool]):
2727
self.event = event_class()
2828
self.supported_media_types = supported_media_types
2929

cloudevents/sdk/event/base.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,18 @@ def UnmarshalJSON(self, b: typing.IO):
6868

6969
def UnmarshalBinary(self, headers: dict, body: typing.IO):
7070
props = self.Properties()
71+
exts = props.get("extensions")
7172
for key in props:
72-
self.Set(key, headers.get("ce-{0}".format(key)))
73+
formatted_key = "ce-{0}".format(key)
74+
if key != "extensions":
75+
self.Set(key, headers.get("ce-{0}".format(key)))
76+
if formatted_key in headers:
77+
del headers[formatted_key]
78+
79+
# rest of headers suppose to an extension?
80+
81+
exts.update(**headers)
82+
self.Set("extensions", exts)
7383

7484
data = None
7585
if body:
@@ -82,9 +92,12 @@ def MarshalBinary(self) -> (dict, object):
8292
props = self.Properties()
8393
for key, value in props.items():
8494
if key not in ["data", "extensions"]:
85-
headers["ce-{0}".format(key)] = value
95+
if value is not None:
96+
headers["ce-{0}".format(key)] = value
8697

8798
exts = props.get("extensions")
88-
headers.update(**exts)
99+
if len(exts) > 0:
100+
headers.update(**exts)
101+
89102
data, _ = self.Get("data")
90103
return headers, data

cloudevents/tests/data.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
# not use this file except in compliance with the License. You may obtain
5+
# a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.
14+
15+
16+
ce_type = "word.found.exclamation"
17+
ce_id = "16fb5f0b-211e-1102-3dfe-ea6e2806f124"
18+
headers = {
19+
"ce-specversion": "0.1",
20+
"ce-type": ce_type,
21+
"ce-id": ce_id,
22+
"ce-time": "2018-10-23T12:28:23.3464579Z",
23+
"ce-source": "pytest",
24+
"Content-Type": "application/json"
25+
}
26+
ce = {
27+
"specversion": "0.1",
28+
"type": "word.found.exclamation",
29+
"id": "16fb5f0b-211e-1102-3dfe-ea6e2806f124",
30+
"time": "2018-10-23T12:28:23.3464579Z",
31+
"source": "pytest",
32+
"contenttype": "application/json"
33+
}

cloudevents/tests/test_event_converter.py renamed to cloudevents/tests/test_event_from_request_converter.py

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,49 +25,35 @@
2525
from cloudevents.sdk.converters import binary
2626
from cloudevents.sdk.converters import structured
2727

28+
from cloudevents.tests import data
29+
2830

2931
def test_binary_converter_upstream():
30-
headers = {
31-
"ce-specversion": "0.1",
32-
"ce-type": "word.found.exclamation",
33-
"ce-id": "16fb5f0b-211e-1102-3dfe-ea6e2806f124",
34-
"ce-time": "2018-10-23T12:28:23.3464579Z",
35-
"ce-source": "pytest",
36-
"Content-Type": "application/json"
37-
}
3832
m = marshaller.NewHTTPMarshaller(
3933
[
4034
binary.NewBinaryHTTPCloudEventConverter(upstream.Event)
4135
]
4236
)
43-
event = m.FromRequest(headers, None)
37+
event = m.FromRequest(data.headers, None)
4438
assert event is not None
45-
assert event.Get("type") == (headers.get("ce-type"), True)
46-
assert event.Get("id") == (headers.get("ce-id"), True)
39+
assert event.Get("type") == (data.ce_type, True)
40+
assert event.Get("id") == (data.ce_id, True)
4741

4842

4943
def test_structured_converter_upstream():
50-
ce = {
51-
"specversion": "0.1",
52-
"type": "word.found.exclamation",
53-
"id": "16fb5f0b-211e-1102-3dfe-ea6e2806f124",
54-
"time": "2018-10-23T12:28:23.3464579Z",
55-
"source": "pytest",
56-
"contenttype": "application/json"
57-
}
5844
m = marshaller.NewHTTPMarshaller(
5945
[
6046
structured.NewJSONHTTPCloudEventConverter(upstream.Event)
6147
]
6248
)
6349
event = m.FromRequest(
6450
{"Content-Type": "application/cloudevents+json"},
65-
io.StringIO(ujson.dumps(ce))
51+
io.StringIO(ujson.dumps(data.ce))
6652
)
6753

6854
assert event is not None
69-
assert event.Get("type") == (ce.get("type"), True)
70-
assert event.Get("id") == (ce.get("id"), True)
55+
assert event.Get("type") == (data.ce_type, True)
56+
assert event.Get("id") == (data.ce_id, True)
7157

7258

7359
# todo: clarify whether spec 0.1 doesn't support binary format
@@ -79,24 +65,28 @@ def test_binary_converter_v01():
7965

8066

8167
def test_structured_converter_v01():
82-
ce = {
83-
"specversion": "0.1",
84-
"type": "word.found.exclamation",
85-
"id": "16fb5f0b-211e-1102-3dfe-ea6e2806f124",
86-
"time": "2018-10-23T12:28:23.3464579Z",
87-
"source": "pytest",
88-
"contenttype": "application/json"
89-
}
9068
m = marshaller.NewHTTPMarshaller(
9169
[
9270
structured.NewJSONHTTPCloudEventConverter(v01.Event)
9371
]
9472
)
9573
event = m.FromRequest(
9674
{"Content-Type": "application/cloudevents+json"},
97-
io.StringIO(ujson.dumps(ce))
75+
io.StringIO(ujson.dumps(data.ce))
9876
)
9977

10078
assert event is not None
101-
assert event.Get("type") == (ce.get("type"), True)
102-
assert event.Get("id") == (ce.get("id"), True)
79+
assert event.Get("type") == (data.ce_type, True)
80+
assert event.Get("id") == (data.ce_id, True)
81+
82+
83+
def test_default_http_marshaller():
84+
m = marshaller.NewDefaultHTTPMarshaller(upstream.Event)
85+
86+
event = m.FromRequest(
87+
{"Content-Type": "application/cloudevents+json"},
88+
io.StringIO(ujson.dumps(data.ce))
89+
)
90+
assert event is not None
91+
assert event.Get("type") == (data.ce_type, True)
92+
assert event.Get("id") == (data.ce_id, True)
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
# not use this file except in compliance with the License. You may obtain
5+
# a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.
14+
15+
import io
16+
import ujson
17+
import copy
18+
19+
from cloudevents.sdk import converters
20+
from cloudevents.sdk import marshaller
21+
22+
from cloudevents.sdk.converters import structured
23+
from cloudevents.sdk.event import v01
24+
from cloudevents.sdk.event import upstream
25+
26+
27+
from cloudevents.tests import data
28+
29+
30+
def test_binary_event_to_request_upstream():
31+
m = marshaller.NewDefaultHTTPMarshaller(upstream.Event)
32+
event = m.FromRequest(
33+
{"Content-Type": "application/cloudevents+json"},
34+
io.StringIO(ujson.dumps(data.ce))
35+
)
36+
37+
assert event is not None
38+
assert event.Get("type") == (data.ce_type, True)
39+
assert event.Get("id") == (data.ce_id, True)
40+
41+
new_headers, _ = m.ToRequest(event, converters.TypeBinary, lambda x: x)
42+
assert new_headers is not None
43+
assert "ce-specversion" in new_headers
44+
45+
46+
def test_structured_event_to_request_upstream():
47+
copy_of_ce = copy.deepcopy(data.ce)
48+
m = marshaller.NewDefaultHTTPMarshaller(upstream.Event)
49+
event = m.FromRequest(
50+
{"Content-Type": "application/cloudevents+json"},
51+
io.StringIO(ujson.dumps(data.ce)))
52+
assert event is not None
53+
assert event.Get("type") == (data.ce_type, True)
54+
assert event.Get("id") == (data.ce_id, True)
55+
56+
new_headers, _ = m.ToRequest(event, converters.TypeStructured, lambda x: x)
57+
for key in new_headers:
58+
assert key in copy_of_ce
59+
60+
61+
def test_structured_event_to_request_v01():
62+
copy_of_ce = copy.deepcopy(data.ce)
63+
m = marshaller.NewHTTPMarshaller(
64+
[
65+
structured.NewJSONHTTPCloudEventConverter(v01.Event)
66+
]
67+
)
68+
event = m.FromRequest(
69+
{"Content-Type": "application/cloudevents+json"},
70+
io.StringIO(ujson.dumps(data.ce)))
71+
assert event is not None
72+
assert event.Get("type") == (data.ce_type, True)
73+
assert event.Get("id") == (data.ce_id, True)
74+
75+
new_headers, _ = m.ToRequest(event, converters.TypeStructured, lambda x: x)
76+
for key in new_headers:
77+
assert key in copy_of_ce
File renamed without changes.

0 commit comments

Comments
 (0)