Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: for issue #170 gracefully handle pubsub messages without attributes in them #187

Merged
merged 4 commits into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/functions_framework/event_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ def marshal_background_event_data(request):
"data": {
"@type": _PUBSUB_MESSAGE_TYPE,
"data": request_data["message"]["data"],
"attributes": request_data["message"]["attributes"],
"attributes": request_data["message"].get("attributes", {}),
},
}
except (AttributeError, KeyError, TypeError):
Expand Down
47 changes: 47 additions & 0 deletions tests/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ def raw_pubsub_request():
}


@pytest.fixture
def raw_pubsub_request_noattributes():
return {
"subscription": "projects/sample-project/subscriptions/gcf-test-sub",
"message": {"data": "eyJmb28iOiJiYXIifQ==", "messageId": "1215011316659232"},
}


@pytest.fixture
def marshalled_pubsub_request():
return {
Expand All @@ -121,6 +129,27 @@ def marshalled_pubsub_request():
}


@pytest.fixture
def marshalled_pubsub_request_noattr():
return {
"data": {
"@type": "type.googleapis.com/google.pubsub.v1.PubsubMessage",
"data": "eyJmb28iOiJiYXIifQ==",
"attributes": {},
},
"context": {
"eventId": "1215011316659232",
"eventType": "google.pubsub.topic.publish",
"resource": {
"name": "projects/sample-project/topics/gcf-test",
"service": "pubsub.googleapis.com",
"type": "type.googleapis.com/google.pubsub.v1.PubsubMessage",
},
"timestamp": "2021-04-17T07:21:18.249Z",
},
}


@pytest.fixture
def raw_pubsub_cloud_event_output(marshalled_pubsub_request):
event = PUBSUB_CLOUD_EVENT.copy()
Expand Down Expand Up @@ -343,6 +372,24 @@ def test_marshal_background_event_data_without_topic_in_path(
assert payload == marshalled_pubsub_request


def test_marshal_background_event_data_without_topic_in_path_no_attr(
raw_pubsub_request_noattributes, marshalled_pubsub_request_noattr
):
req = flask.Request.from_values(
json=raw_pubsub_request_noattributes, path="/myfunc/"
)
payload = event_conversion.marshal_background_event_data(req)

# Remove timestamps as they get generates on the fly
del marshalled_pubsub_request_noattr["context"]["timestamp"]
del payload["context"]["timestamp"]

# Resource name is set to empty string when it cannot be parsed from the request path
marshalled_pubsub_request_noattr["context"]["resource"]["name"] = ""

assert payload == marshalled_pubsub_request_noattr


def test_marshal_background_event_data_with_topic_path(
raw_pubsub_request, marshalled_pubsub_request
):
Expand Down