Skip to content

Commit fa48380

Browse files
authored
Merge pull request #61 from NHSDigital/mm-set-content-length-0-on-initial-request-timeout
apm: set content-length=0 for errors with no response stream
2 parents ec13640 + 284e76b commit fa48380

5 files changed

Lines changed: 60 additions & 4 deletions

File tree

docker/async-slowapp/src/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ const handlers = require("./handlers");
120120
app.use(before_request);
121121
app.get("/_ping", handlers.ping);
122122
app.get("/_status", handlers.ping);
123-
app.get("/slow", handlers.slow);
123+
app.all("/slow", handlers.slow);
124124
app.delete("/poll", handlers.delete_poll);
125125
app.get("/poll", handlers.poll);
126126
app.get("/sub/_ping", handlers.ping);

docker/async-slowapp/src/app.spec.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,45 @@ describe("express with async-slowapp with /sub", function () {
213213
});
214214
});
215215

216+
it("responds to post /slow with a content-location and poll count increments", (done) => {
217+
let content_location;
218+
request(server)
219+
.post("/slow?complete_in=0.01&final_status=418")
220+
.send({test: "bob"})
221+
.expect("Content-Type", /json/)
222+
.expect(res => {
223+
let headers = res.res.rawHeaders.asMultiValue();
224+
assert.isTrue(headers.has('content-location'));
225+
content_location = headers['content-location'];
226+
let cookies = headers.cookies("set-cookie");
227+
assert.isDefined(cookies["poll-count"]);
228+
let poll_count = parseInt(cookies["poll-count"].split(";")[0].split("=")[1]);
229+
assert.equal(poll_count, 0);
230+
})
231+
.expect(202)
232+
.end(async (err)=>{
233+
if (err) {
234+
return done(err);
235+
}
236+
await sleep(500);
237+
let url = new URL(content_location);
238+
request(server)
239+
.get(url.pathname + url.search)
240+
.set("Cookie", "poll-count=0")
241+
.expect(res => {
242+
if (err) {
243+
return done(err);
244+
}
245+
let headers = res.res.rawHeaders.asMultiValue();
246+
assert.isFalse(headers.has('content-location'));
247+
let cookies = headers.cookies("set-cookie");
248+
assert.isDefined(cookies["poll-count"]);
249+
let poll_count = parseInt(cookies["poll-count"].split(";")[0].split("=")[1]);
250+
assert.equal(poll_count, 1);
251+
})
252+
.expect(418, done)
253+
});
254+
});
216255

217256
it("responds to /sub/poll continues with correct location", (done) => {
218257
let content_location;

docker/sync-wrap/src/app.slowapp.spec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,21 @@ describe("express with slowap", function () {
109109
.get("/slow?delay=5")
110110
.set("x-sync-wait", "0.25")
111111
.set("Accept", "application/json")
112+
.expect(res => {
113+
assert.equal(res.header['content-length'], "0");
114+
})
115+
.expect(504, done);
116+
}).timeout(10000);
117+
118+
it("when posting it times out if x-sync-wait shorter than initial response", (done) => {
119+
request(server)
120+
.post("/slow?delay=5")
121+
.send({test: 'data'})
122+
.set("x-sync-wait", "0.25")
123+
.set("Accept", "application/json")
124+
.expect(res => {
125+
assert.equal(res.header['content-length'], "0");
126+
})
112127
.expect(504, done);
113128
}).timeout(10000);
114129

docker/sync-wrap/src/handlers.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ async function proxy(req, res, next) {
302302
}
303303

304304
if (response_stream === undefined) {
305+
res.set('content-length', 0)
305306
res.end();
306307
next(error);
307308
return
@@ -518,7 +519,7 @@ async function proxy(req, res, next) {
518519
await send_response(response.statusCode, {headers: headers, response: response});
519520
})
520521
.catch(async (fin) => {
521-
await send_response(fin.error === "timeout" ? 504 : 502, {headers: headers, error: fin.error || fin});
522+
await send_response(fin.error === "timeout" ? 504 : 502, {headers: undefined, error: fin.error || fin});
522523
});
523524

524525
}

e2e/tests/api_tests.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ async def is_deployed(resp: ClientResponse):
6565
return False
6666

6767
backend = dict_path(body, ["checks", "healthcheck", "outcome", "version"])
68-
if not backend:
69-
return True
68+
69+
if type(backend) != dict:
70+
return False
7071

7172
return backend.get("commitId") == api_test_config.commit_id
7273

0 commit comments

Comments
 (0)