Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
fcf0a44
AMB-1381: Added policy to rewrite response url hostnames
ChewieCB Jan 20, 2023
ec2e4a5
AMB-1381: Removed breakpoint
ChewieCB Jan 20, 2023
6d3a194
AMB-1381: Added host rewrite policy to endpoints
ChewieCB Jan 20, 2023
13d51c1
AMB-1381: Fixed incorrect policy names
ChewieCB Jan 20, 2023
e5ec8b6
AMB-1381: Tweaked placement of js policy
ChewieCB Jan 20, 2023
61ff31a
AMB-1381: Tweaked placement of js policy
ChewieCB Jan 20, 2023
5a22c34
Merge branch 'master' into amb-1381-rewrite-hostnames
ChewieCB Jan 20, 2023
84eb5d0
AMB-1381: Added empty body to url requests
ChewieCB Jan 20, 2023
016f28c
Merge remote-tracking branch 'origin/amb-1381-rewrite-hostnames' into…
ChewieCB Jan 20, 2023
3058d82
AMB-1381: Fixed typo in policy name
ChewieCB Jan 20, 2023
f11a1fa
AMB-1381: Ported host rewrite policies to sandbox
ChewieCB Jan 20, 2023
9768fc5
Updated postcode and place testing
LKFULLER1 Jan 18, 2023
a7a1840
Fix test formatting
LKFULLER1 Jan 18, 2023
035c1a2
fixing linting
LKFULLER1 Jan 18, 2023
7e39029
fixing sandbox test
LKFULLER1 Jan 18, 2023
c85e6c1
fixing sandbox tests
LKFULLER1 Jan 18, 2023
f7edf43
updated postcode check to lowercase
LKFULLER1 Jan 19, 2023
500acc9
spec update
LKFULLER1 Jan 19, 2023
795bade
fixed typo
LKFULLER1 Jan 19, 2023
518467e
gitignore openapitools.json
LKFULLER1 Jan 20, 2023
ae080f8
AMB-1381: Fixed typo in policy name
ChewieCB Jan 20, 2023
fa7f024
AMB-1381: Ported host rewrite policies to sandbox
ChewieCB Jan 20, 2023
a45eeea
Merge remote-tracking branch 'origin/amb-1381-rewrite-hostnames' into…
ChewieCB Jan 20, 2023
06f3a5d
AMB-1381: Removed leftover breakpoint
ChewieCB Jan 20, 2023
d5fa5a0
AMB-1381: Updated spec example to use INT url hostname
ChewieCB Jan 23, 2023
71fd937
AMB-1381: Added regex to more accurately extract and replace hostname…
ChewieCB Jan 23, 2023
8dc23e9
AMB-1381: Ported regex change to sandbox proxy
ChewieCB Jan 23, 2023
36dc42e
AMB-1381: Fixed regex look-behind issue
ChewieCB Jan 23, 2023
36a00f3
AMB-1381: Fixed re-written hostname prefix
ChewieCB Jan 23, 2023
7f8c68b
AMB-1381: Fixed rewritten url
ChewieCB Jan 23, 2023
b96d429
AMB-1381: Fixed regex
ChewieCB Jan 23, 2023
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Javascript async="false" continueOnError="false" enabled="true" timeLimit="200" name="javascript.CacheRequestHost">
<DisplayName>javascript.CacheRequestHost</DisplayName>
<Properties/>
<ResourceURL>jsc://CacheRequestHost.js</ResourceURL>
</Javascript>
6 changes: 6 additions & 0 deletions proxies/live/apiproxy/policies/javascript.RewriteHostName.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Javascript async="false" continueOnError="false" enabled="true" timeLimit="200" name="javascript.RewriteHostName">
<DisplayName>javascript.RewriteHostName</DisplayName>
<Properties/>
<ResourceURL>jsc://RewriteHostName.js</ResourceURL>
</Javascript>
11 changes: 10 additions & 1 deletion proxies/live/apiproxy/proxies/default.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,16 @@
</Condition>
</Flow>
</Flows>
<PreFlow/>
<PreFlow>
<Request>
<Step>
<Name>AssignMessage.SetApimGuids</Name>
</Step>
<Step>
<Name>javascript.CacheRequestHost</Name>
</Step>
</Request>
</PreFlow>
<PostClientFlow name="PostClientFlow">
<Response>
<Step>
Expand Down
4 changes: 4 additions & 0 deletions proxies/live/apiproxy/resources/jsc/CacheRequestHost.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const env_regex = /^([a-zA-Z\-]+)(?=(\.api\.service\.nhs\.uk))/gm;
var hostname_env = request.headers.host;
hostname_env = hostname_env.match(env_regex, request.headers.host)[0];
context.setVariable("request_hostname_env", hostname_env);
19 changes: 19 additions & 0 deletions proxies/live/apiproxy/resources/jsc/RewriteHostName.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var response = context.getVariable("response.content");
var json = JSON.parse(response);

if (json.place) {
// Loop over each item in the search result array, re-writing the hostname to that of the initial request
json.place.forEach(value => {
var request_hostname = context.getVariable("request_hostname_env");
// Annoyingly, Apigee's version of JS doesn't support regex look-behinds,
// so we grab the env string including https:// prefix
var hostname_regex = /(^https:\/\/([a-zA-Z\-]+\.)+)nhs\.uk/gm;
var rewritten_url = value.url.replace(hostname_regex, "https://" + request_hostname + ".api.service.nhs.uk");
value.url = rewritten_url;
value.url = value.url.replace("api-version=1", "api-version=2");
value.url = value.url.replace("service-search", "service-search-api");
});

// Update the response payload with the corrected urls
context.setVariable("message.content", JSON.stringify(json));
}
8 changes: 8 additions & 0 deletions proxies/live/apiproxy/targets/target.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
</Step>
</Request>
</PreFlow>
<PostFlow>
<Request/>
<Response>
<Step>
<Name>javascript.RewriteHostName</Name>
</Step>
</Response>
</PostFlow>
<FaultRules>
<FaultRule name="api_key_invalid">
<Step>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Javascript async="false" continueOnError="false" enabled="true" timeLimit="200" name="javascript.CacheRequestHost">
<DisplayName>javascript.CacheRequestHost</DisplayName>
<Properties/>
<ResourceURL>jsc://CacheRequestHost.js</ResourceURL>
</Javascript>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Javascript async="false" continueOnError="false" enabled="true" timeLimit="200" name="javascript.RewriteHostName">
<DisplayName>javascript.RewriteHostName</DisplayName>
<Properties/>
<ResourceURL>jsc://RewriteHostName.js</ResourceURL>
</Javascript>
11 changes: 10 additions & 1 deletion proxies/sandbox/apiproxy/proxies/default.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,16 @@
</Condition>
</Flow>
</Flows>
<PreFlow/>
<PreFlow>
<Request>
<Step>
<Name>AssignMessage.SetApimGuids</Name>
</Step>
<Step>
<Name>javascript.CacheRequestHost</Name>
</Step>
</Request>
</PreFlow>
<PostClientFlow name="PostClientFlow">
<Response>
<Step>
Expand Down
4 changes: 4 additions & 0 deletions proxies/sandbox/apiproxy/resources/jsc/CacheRequestHost.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const env_regex = /^([a-zA-Z\-]+)(?=(\.api\.service\.nhs\.uk))/gm;
var hostname_env = request.headers.host;
hostname_env = hostname_env.match(env_regex, request.headers.host)[0];
context.setVariable("request_hostname_env", hostname_env);
19 changes: 19 additions & 0 deletions proxies/sandbox/apiproxy/resources/jsc/RewriteHostName.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var response = context.getVariable("response.content");
var json = JSON.parse(response);

if (json.place) {
// Loop over each item in the search result array, re-writing the hostname to that of the initial request
json.place.forEach(value => {
var request_hostname = context.getVariable("request_hostname_env");
// Annoyingly, Apigee's version of JS doesn't support regex look-behinds,
// so we grab the env string including https:// prefix
var hostname_regex = /(^https:\/\/([a-zA-Z\-]+\.)+)nhs\.uk/gm;
var rewritten_url = value.url.replace(hostname_regex, "https://" + request_hostname + ".api.service.nhs.uk");
value.url = rewritten_url;
value.url = value.url.replace("api-version=1", "api-version=2");
value.url = value.url.replace("service-search", "service-search-api");
});

// Update the response payload with the corrected urls
context.setVariable("message.content", JSON.stringify(json));
}
8 changes: 8 additions & 0 deletions proxies/sandbox/apiproxy/targets/sandbox.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
</Step>
</Response>
</PreFlow>
<PostFlow>
<Request/>
<Response>
<Step>
<Name>javascript.RewriteHostName</Name>
</Step>
</Response>
</PostFlow>
<DefaultFaultRule name="DefaultFaultRule">
<Step>
<Name>AssignMessage.AddCors</Name>
Expand Down
6 changes: 3 additions & 3 deletions specification/examples/search-place_v2.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
"Latitude": 53.478941671902611,
"Longitude": -2.245277998298477,
"text": "Manchester, North West",
"url": "https://api.nhs.uk/service-search/search-postcode-or-place?api-version=1&search=manchester&latitude=53.4789416719026&longitude=-2.24527799829848"
"url": "https://int.api.service.nhs.uk/service-search/search-postcode-or-place?api-version=1&search=manchester&latitude=53.4789416719026&longitude=-2.24527799829848"
},
{
"Latitude": 53.514265448236706,
"Longitude": -2.4199003923191209,
"text": "New Manchester, North West",
"url": "https://api.nhs.uk/service-search/search-postcode-or-place?api-version=1&search=manchester&latitude=53.5142654482367&longitude=-2.41990039231912"
"url": "https://int.api.service.nhs.uk/service-search/search-postcode-or-place?api-version=1&search=manchester&latitude=53.5142654482367&longitude=-2.41990039231912"
}
]
}
}
9 changes: 4 additions & 5 deletions tests/test_search_postcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ def test_invalid_api_version(self, get_api_key):
assert_that(response.status_code).is_equal_to(expected_status_code)
assert_that(response.json()).is_equal_to(expected_body)

@pytest.mark.skip
@pytest.mark.integration
def test_response_payload_urls_are_corrected(self, get_api_key):
"""
Expand All @@ -172,11 +171,11 @@ def test_response_payload_urls_are_corrected(self, get_api_key):
json={},
)

results = response.json()["place"]
results = response.json()['place']
for item in results:
url_response = requests.get(
url=item["url"],
params={"apikey": api_key},
url_response = requests.post(
url=item['url'],
headers=make_headers(api_key),
json={},
)
assert_that(url_response.status_code).is_equal_to(expected_status_code)