diff --git a/requirements/test.txt b/requirements/test.txt index 9972370a..6aa95bdb 100644 --- a/requirements/test.txt +++ b/requirements/test.txt @@ -10,4 +10,4 @@ sanic aiohttp Pillow requests -Flask +flask diff --git a/samples/http-image-cloudevents/README.md b/samples/http-image-cloudevents/README.md index 35e758c0..adec0340 100644 --- a/samples/http-image-cloudevents/README.md +++ b/samples/http-image-cloudevents/README.md @@ -9,7 +9,7 @@ pip3 install -r requirements.txt Start server: ```sh -python3 server.py +python3 image_sample_server.py ``` In a new shell, run the client code which sends a structured and binary diff --git a/samples/http-image-cloudevents/server.py b/samples/http-image-cloudevents/image_sample_server.py similarity index 100% rename from samples/http-image-cloudevents/server.py rename to samples/http-image-cloudevents/image_sample_server.py diff --git a/samples/http-image-cloudevents/test_image_sample.py b/samples/http-image-cloudevents/image_sample_test.py similarity index 99% rename from samples/http-image-cloudevents/test_image_sample.py rename to samples/http-image-cloudevents/image_sample_test.py index 5b1c4030..64c0be26 100644 --- a/samples/http-image-cloudevents/test_image_sample.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 server import app from cloudevents.http import ( CloudEvent, diff --git a/samples/http-json-cloudevents/README.md b/samples/http-json-cloudevents/README.md index a72244dc..38447da0 100644 --- a/samples/http-json-cloudevents/README.md +++ b/samples/http-json-cloudevents/README.md @@ -9,7 +9,7 @@ pip3 install -r requirements.txt Start server: ```sh -python3 server.py +python3 json_sample_server.py ``` In a new shell, run the client code which sends a structured and binary @@ -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/server.py b/samples/http-json-cloudevents/json_sample_server.py similarity index 100% rename from samples/http-json-cloudevents/server.py rename to samples/http-json-cloudevents/json_sample_server.py diff --git a/samples/http-json-cloudevents/json_sample_test.py b/samples/http-json-cloudevents/json_sample_test.py new file mode 100644 index 00000000..4ab9708b --- /dev/null +++ b/samples/http-json-cloudevents/json_sample_test.py @@ -0,0 +1,40 @@ +import pytest +from json_sample_server import app + +from cloudevents.http import CloudEvent, to_binary_http, to_structured_http + + +@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 diff --git a/samples/http-json-cloudevents/test_verify_sample.py b/samples/http-json-cloudevents/test_verify_sample.py deleted file mode 100644 index e69de29b..00000000 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()))