Skip to content

Commit

Permalink
modify tests to check that adserver returns CE-compatible responses
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelcheah committed Feb 19, 2024
1 parent 6182072 commit 4c57cc7
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions components/alibi-detect-server/adserver/tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
from typing import List, Dict, Optional, Union
import json
import requests_mock
from cloudevents.sdk import converters
from cloudevents.sdk import marshaller
from cloudevents.sdk.event import v1


class TestProtocol(AsyncHTTPTestCase):
Expand Down Expand Up @@ -74,11 +77,31 @@ def test_basic(self):
)
self.assertEqual(response.code, 200)
expectedResponse = DummyModel.getResponse().data
# assert that the expected response conforms to the CloudEvent spec
event = v1.Event()
http_marshaller = marshaller.NewDefaultHTTPMarshaller()
try:
event = http_marshaller.FromRequest(
event, response.headers, response.body, json.loads
)
except Exception as e:
assert False, f"Failed to unmarshall data with error: {type(e).__name__}('{e}')"

# assert cloud event properties have been set correctly in response
self.assertEqual(event.Data(), expectedResponse)
self.assertEqual(event.Source(), self.eventSource)
self.assertEqual(event.EventType(), self.eventType)
self.assertEqual(event.ContentType(), "application/json")
self.assertEqual(event.EventID(), "1234")
self.assertEqual(event.CloudEventVersion(), "1.0")
self.assertEqual(response.body.decode("utf-8"), json.dumps(expectedResponse))

# assert requests have been made with the correct headers and data
self.assertEqual(m.request_history[0].json(), expectedResponse)
headers: Dict = m.request_history[0]._request.headers
self.assertEqual(headers["ce-source"], self.eventSource)
self.assertEqual(headers["ce-type"], self.eventType)
self.assertNotIn("ce-datacontenttype", headers)


class TestKFservingV2HttpModel(AsyncHTTPTestCase):
Expand Down

0 comments on commit 4c57cc7

Please sign in to comment.