Skip to content

Commit 559de86

Browse files
committed
Add tests for request_json and delete_json
1 parent 4e7784a commit 559de86

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

tests/server_test.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import tempfile
66
import unittest
77

8+
import requests
9+
import responses
10+
811
from fhirclient import server
912

1013

@@ -17,6 +20,24 @@ def copy_metadata(filename: str, tmpdir: str) -> None:
1720
os.path.join(tmpdir, 'metadata')
1821
)
1922

23+
@staticmethod
24+
def create_server() -> server.FHIRServer:
25+
return server.FHIRServer(None, state={
26+
'base_uri': "https://example.invalid/",
27+
"auth_type": "oauth2",
28+
"auth": {
29+
"aud": "https://example.invalid/",
30+
"registration_uri": "https://example.invalid/o2/registration",
31+
"authorize_uri": "https://example.invalid/o2/authorize",
32+
"redirect_uri": "https://example.invalid/o2/redirect",
33+
"token_uri": "https://example.invalid/o2/token",
34+
"auth_state": "931f4c31-73e2-4c04-bf6b-b7c9800312ea",
35+
"app_secret": "my-secret",
36+
"access_token": "my-access-token",
37+
"refresh_token": "my-refresh-token",
38+
},
39+
})
40+
2041
def testValidCapabilityStatement(self):
2142
with tempfile.TemporaryDirectory() as tmpdir:
2243
self.copy_metadata('test_metadata_valid.json', tmpdir)
@@ -59,6 +80,49 @@ def testInvalidCapabilityStatement(self):
5980
self.assertEqual("Superfluous entry \"systems\"", str(e.errors[2].errors[1].errors[0].errors[0].errors[0])[:27])
6081
self.assertEqual("Superfluous entry \"formats\"", str(e.errors[3])[:27])
6182

83+
@responses.activate
84+
def testRequestJson(self):
85+
fhir = self.create_server()
86+
fhir.prepare()
87+
88+
bin1 = {"resourceType": "Binary", "id": "bin1"}
89+
mock = responses.add("GET", f"{fhir.base_uri}Binary/bin1", json=bin1)
90+
91+
resp = fhir.request_json("Binary/bin1")
92+
self.assertEqual(resp, bin1)
93+
self.assertEqual(mock.calls[0].request.headers["Accept"], "application/fhir+json")
94+
self.assertEqual(mock.calls[0].request.headers["Accept-Charset"], "UTF-8")
95+
self.assertEqual(mock.calls[0].request.headers["Authorization"], "Bearer my-access-token")
96+
97+
resp = fhir.request_json("Binary/bin1", nosign=True)
98+
self.assertEqual(resp, bin1)
99+
self.assertEqual(mock.calls[1].request.headers["Accept"], "application/fhir+json")
100+
self.assertEqual(mock.calls[1].request.headers["Accept-Charset"], "UTF-8")
101+
self.assertNotIn("Authorization", mock.calls[1].request.headers)
102+
103+
self.assertEqual(mock.call_count, 2)
104+
105+
@responses.activate
106+
def testDeleteJson(self):
107+
fhir = self.create_server()
108+
fhir.prepare()
109+
110+
mock = responses.add("DELETE", f"{fhir.base_uri}Binary/bin1")
111+
112+
resp = fhir.delete_json("Binary/bin1")
113+
self.assertIsInstance(resp, requests.Response)
114+
self.assertEqual(mock.calls[0].request.headers["Accept"], "application/fhir+json")
115+
self.assertEqual(mock.calls[0].request.headers["Accept-Charset"], "UTF-8")
116+
self.assertEqual(mock.calls[0].request.headers["Authorization"], "Bearer my-access-token")
117+
118+
resp = fhir.delete_json("Binary/bin1", nosign=True)
119+
self.assertIsInstance(resp, requests.Response)
120+
self.assertEqual(mock.calls[1].request.headers["Accept"], "application/fhir+json")
121+
self.assertEqual(mock.calls[1].request.headers["Accept-Charset"], "UTF-8")
122+
self.assertNotIn("Authorization", mock.calls[1].request.headers)
123+
124+
self.assertEqual(mock.call_count, 2)
125+
62126

63127
class MockServer(server.FHIRServer):
64128
""" Reads local files.

0 commit comments

Comments
 (0)