Skip to content

Commit

Permalink
fix: for issue #170 gracefully handle pubsub messages without attribu…
Browse files Browse the repository at this point in the history
…tes in them (#187)

* Handle when attributes not present

Co-authored-by: Annie Fu <16651409+anniefu@users.noreply.github.com>
  • Loading branch information
MikeMoore63 and anniefu authored May 18, 2022
1 parent f2285f9 commit a820fd4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
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

0 comments on commit a820fd4

Please sign in to comment.