Skip to content

Commit 4bd0a62

Browse files
authored
http-json-cloudevents testing (cloudevents#80)
* Added tests to http-json-cloudevents Signed-off-by: Curtis Mason <cumason@google.com> * removed outdated python-requests sample code Signed-off-by: Curtis Mason <cumason@google.com> * lint fix Signed-off-by: Curtis Mason <cumason@google.com> * Added flask to requirements Signed-off-by: Curtis Mason <cumason@google.com> * lint fix Signed-off-by: Curtis Mason <cumason@google.com>
1 parent ca5c702 commit 4bd0a62

File tree

10 files changed

+50
-118
lines changed

10 files changed

+50
-118
lines changed

requirements/test.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ sanic
1010
aiohttp
1111
Pillow
1212
requests
13-
Flask
13+
flask

samples/http-image-cloudevents/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pip3 install -r requirements.txt
99
Start server:
1010

1111
```sh
12-
python3 server.py
12+
python3 image_sample_server.py
1313
```
1414

1515
In a new shell, run the client code which sends a structured and binary

samples/http-image-cloudevents/test_image_sample.py renamed to samples/http-image-cloudevents/image_sample_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
import pytest
66
from client import image_bytes
7+
from image_sample_server import app
78
from PIL import Image
8-
from server import app
99

1010
from cloudevents.http import (
1111
CloudEvent,

samples/http-json-cloudevents/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pip3 install -r requirements.txt
99
Start server:
1010

1111
```sh
12-
python3 server.py
12+
python3 json_sample_server.py
1313
```
1414

1515
In a new shell, run the client code which sends a structured and binary
@@ -18,3 +18,9 @@ cloudevent to your local server:
1818
```sh
1919
python3 client.py http://localhost:3000/
2020
```
21+
22+
## Test
23+
24+
```sh
25+
pytest
26+
```
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import pytest
2+
from json_sample_server import app
3+
4+
from cloudevents.http import CloudEvent, to_binary_http, to_structured_http
5+
6+
7+
@pytest.fixture
8+
def client():
9+
app.testing = True
10+
return app.test_client()
11+
12+
13+
def test_binary_request(client):
14+
# This data defines a binary cloudevent
15+
attributes = {
16+
"type": "com.example.sampletype1",
17+
"source": "https://example.com/event-producer",
18+
}
19+
data = {"message": "Hello World!"}
20+
21+
event = CloudEvent(attributes, data)
22+
headers, body = to_binary_http(event)
23+
24+
r = client.post("/", headers=headers, data=body)
25+
assert r.status_code == 204
26+
27+
28+
def test_structured_request(client):
29+
# This data defines a binary cloudevent
30+
attributes = {
31+
"type": "com.example.sampletype2",
32+
"source": "https://example.com/event-producer",
33+
}
34+
data = {"message": "Hello World!"}
35+
36+
event = CloudEvent(attributes, data)
37+
headers, body = to_structured_http(event)
38+
39+
r = client.post("/", headers=headers, data=body)
40+
assert r.status_code == 204

samples/http-json-cloudevents/test_verify_sample.py

Whitespace-only changes.

samples/python-requests/cloudevent_to_request.py

Lines changed: 0 additions & 76 deletions
This file was deleted.

samples/python-requests/request_to_cloudevent.py

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)