From 8c05aee21b89a184bf969d71e354223fbae44bd7 Mon Sep 17 00:00:00 2001 From: Curtis Mason Date: Tue, 28 Jul 2020 12:40:12 -0700 Subject: [PATCH 1/5] Added tests to http-json-cloudevents Signed-off-by: Curtis Mason --- samples/http-json-cloudevents/README.md | 6 +++ .../test_verify_sample.py | 39 +++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/samples/http-json-cloudevents/README.md b/samples/http-json-cloudevents/README.md index a72244dc..da3ef38f 100644 --- a/samples/http-json-cloudevents/README.md +++ b/samples/http-json-cloudevents/README.md @@ -18,3 +18,9 @@ cloudevent to your local server: ```sh python3 client.py http://localhost:3000/ ``` + +## Test + +```sh +pytest +``` diff --git a/samples/http-json-cloudevents/test_verify_sample.py b/samples/http-json-cloudevents/test_verify_sample.py index e69de29b..8d277e50 100644 --- a/samples/http-json-cloudevents/test_verify_sample.py +++ b/samples/http-json-cloudevents/test_verify_sample.py @@ -0,0 +1,39 @@ +from cloudevents.sdk.http import CloudEvent, to_binary_http, to_structured_http +from server import app +import pytest + + +@pytest.fixture +def client(): + app.testing = True + return app.test_client() + + +def test_binary_request(client): + # This data defines a binary cloudevent + attributes = { + "type": "com.example.sampletype1", + "source": "https://example.com/event-producer", + } + data = {"message": "Hello World!"} + + event = CloudEvent(attributes, data) + headers, body = to_binary_http(event) + + r = client.post("/", headers=headers, data=body) + assert r.status_code == 204 + + +def test_structured_request(client): + # This data defines a binary cloudevent + attributes = { + "type": "com.example.sampletype2", + "source": "https://example.com/event-producer", + } + data = {"message": "Hello World!"} + + event = CloudEvent(attributes, data) + headers, body = to_structured_http(event) + + r = client.post("/", headers=headers, data=body) + assert r.status_code == 204 \ No newline at end of file From bf622a3be21887c1c30050760834373107dbfe5a Mon Sep 17 00:00:00 2001 From: Curtis Mason Date: Tue, 28 Jul 2020 12:41:10 -0700 Subject: [PATCH 2/5] removed outdated python-requests sample code Signed-off-by: Curtis Mason --- .../python-requests/cloudevent_to_request.py | 76 ------------------- .../python-requests/request_to_cloudevent.py | 38 ---------- 2 files changed, 114 deletions(-) delete mode 100644 samples/python-requests/cloudevent_to_request.py delete mode 100644 samples/python-requests/request_to_cloudevent.py diff --git a/samples/python-requests/cloudevent_to_request.py b/samples/python-requests/cloudevent_to_request.py deleted file mode 100644 index 3df3f33c..00000000 --- a/samples/python-requests/cloudevent_to_request.py +++ /dev/null @@ -1,76 +0,0 @@ -# All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import json -import sys - -import requests - -from cloudevents.sdk import converters, marshaller -from cloudevents.sdk.event import v1 - - -def run_binary(event, url): - binary_headers, binary_data = http_marshaller.ToRequest( - event, converters.TypeBinary, json.dumps - ) - - print("binary CloudEvent") - for k, v in binary_headers.items(): - print("{0}: {1}\r\n".format(k, v)) - print(binary_data) - response = requests.post(url, headers=binary_headers, data=binary_data) - response.raise_for_status() - - -def run_structured(event, url): - structured_headers, structured_data = http_marshaller.ToRequest( - event, converters.TypeStructured, json.dumps - ) - print("structured CloudEvent") - print(structured_data.getvalue()) - - response = requests.post( - url, headers=structured_headers, data=structured_data.getvalue() - ) - response.raise_for_status() - - -if __name__ == "__main__": - - if len(sys.argv) < 3: - sys.exit( - "Usage: python with_requests.py " - "[binary | structured] " - "" - ) - - fmt = sys.argv[1] - url = sys.argv[2] - - http_marshaller = marshaller.NewDefaultHTTPMarshaller() - event = ( - v1.Event() - .SetContentType("application/json") - .SetData({"name": "denis"}) - .SetEventID("my-id") - .SetSource("") - - url = sys.argv[1] - response = requests.get(url) - response.raise_for_status() - headers = response.headers - data = io.BytesIO(response.content) - event = v1.Event() - http_marshaller = marshaller.NewDefaultHTTPMarshaller() - event = http_marshaller.FromRequest(event, headers, data, json.load) - - print(json.dumps(event.Properties())) From 36b6d7092dd8718817f17f3420078979425533f1 Mon Sep 17 00:00:00 2001 From: Curtis Mason Date: Tue, 28 Jul 2020 12:44:24 -0700 Subject: [PATCH 3/5] lint fix Signed-off-by: Curtis Mason --- samples/http-json-cloudevents/test_verify_sample.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/samples/http-json-cloudevents/test_verify_sample.py b/samples/http-json-cloudevents/test_verify_sample.py index 8d277e50..7f9208ff 100644 --- a/samples/http-json-cloudevents/test_verify_sample.py +++ b/samples/http-json-cloudevents/test_verify_sample.py @@ -1,6 +1,7 @@ -from cloudevents.sdk.http import CloudEvent, to_binary_http, to_structured_http -from server import app import pytest +from server import app + +from cloudevents.sdk.http import CloudEvent, to_binary_http, to_structured_http @pytest.fixture @@ -36,4 +37,4 @@ def test_structured_request(client): headers, body = to_structured_http(event) r = client.post("/", headers=headers, data=body) - assert r.status_code == 204 \ No newline at end of file + assert r.status_code == 204 From 977f32396e83550d2bd4212b4b3b1947cecd2580 Mon Sep 17 00:00:00 2001 From: Curtis Mason Date: Tue, 28 Jul 2020 15:12:24 -0700 Subject: [PATCH 4/5] Added flask to requirements Signed-off-by: Curtis Mason --- requirements/test.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements/test.txt b/requirements/test.txt index 1d5f3087..6aa95bdb 100644 --- a/requirements/test.txt +++ b/requirements/test.txt @@ -10,4 +10,4 @@ sanic aiohttp Pillow requests - +flask From 2fdffc5ed30b3038498553b3fa1b6f04e4e4f5f5 Mon Sep 17 00:00:00 2001 From: Curtis Mason Date: Thu, 30 Jul 2020 16:42:51 -0700 Subject: [PATCH 5/5] lint fix Signed-off-by: Curtis Mason --- samples/http-image-cloudevents/image_sample_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/http-image-cloudevents/image_sample_test.py b/samples/http-image-cloudevents/image_sample_test.py index c5e97ef6..64c0be26 100644 --- a/samples/http-image-cloudevents/image_sample_test.py +++ b/samples/http-image-cloudevents/image_sample_test.py @@ -4,8 +4,8 @@ import pytest from client import image_bytes +from image_sample_server import app from PIL import Image -from image_sample_server import app from cloudevents.http import ( CloudEvent,