Skip to content

Commit ca1b5e1

Browse files
committed
Add the ability to return an HTTP 422 error from the sandbox
1 parent 3ea3558 commit ca1b5e1

4 files changed

Lines changed: 53 additions & 1 deletion

File tree

sandbox/__test__/messages.spec.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import request from "supertest";
22
import { assert } from "chai";
33
import * as uuid from "uuid";
44
import { setup } from "./helpers.js";
5+
import { duplicateRequestCorrelationId } from "../handlers/config.js";
56

67
describe("/api/v1/messages", () => {
78
let env;
@@ -169,6 +170,30 @@ describe("/api/v1/messages", () => {
169170
.expect("X-Correlation-Id", correlationId, done);
170171
});
171172

173+
it("responds with a 422 when provided with the correct correlation ID", (done) => {
174+
request(server)
175+
.post("/api/v1/messages")
176+
.send({
177+
data: {
178+
type: "Message",
179+
attributes: {
180+
routingPlanId: "b838b13c-f98c-4def-93f0-515d4e4f4ee1",
181+
messageReference: "b5bb84b9-a522-41e9-aa8b-ad1b6a454243",
182+
recipient: {
183+
nhsNumber: "1",
184+
},
185+
originator: {
186+
odsCode: "X123",
187+
},
188+
personalisation: {},
189+
},
190+
},
191+
})
192+
.set("X-Correlation-Id", duplicateRequestCorrelationId)
193+
.expect(422)
194+
.expect("Content-Type", /json/, done);
195+
});
196+
172197
it("responds with a 201 for a valid global NHS app routing plan", (done) => {
173198
request(server)
174199
.post("/api/v1/messages")

sandbox/handlers/config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ const noDefaultOdsClientAuth = "noDefaultOds";
5252
const noOdsChangeClientAuth = "noOdsChange";
5353
const notAllowedContactDetailOverride = "notAllowedContactDetailOverride"
5454

55+
const duplicateRequestCorrelationId = "cb8c06ea-505b-4c4b-a4c4-317178ecb5b5"
56+
5557
export {
5658
invalidRoutingPlanId,
5759
trigger500SendingGroupId,
@@ -66,5 +68,6 @@ export {
6668
globalFreeTextSmsSendingGroupId,
6769
noDefaultOdsClientAuth,
6870
noOdsChangeClientAuth,
69-
notAllowedContactDetailOverride
71+
notAllowedContactDetailOverride,
72+
duplicateRequestCorrelationId
7073
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {
2+
duplicateRequestCorrelationId
3+
} from "../config.js";
4+
5+
export function getCorrelationIdError(correlationId) {
6+
if (correlationId === duplicateRequestCorrelationId) {
7+
return [
8+
422,
9+
"Request exists with identical messageReference",
10+
];
11+
}
12+
13+
return null;
14+
}

sandbox/handlers/messages.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { getOdsCodeError } from "./error_scenarios/ods_code.js";
66
import { mandatorySingleMessageFieldValidation } from "./validation/mandatory_single_message_fields.js";
77
import { getAlternateContactDetailsError } from "./error_scenarios/override_contact_details.js";
88
import { getGlobalFreeTextError } from "./error_scenarios/global_free_text.js";
9+
import { getCorrelationIdError } from "./error_scenarios/correlation_id.js";
910

1011
export async function messages(req, res, next) {
1112
if (req.headers.authorization === "banned") {
@@ -71,6 +72,15 @@ export async function messages(req, res, next) {
7172
return;
7273
}
7374

75+
const correlationIdError = getCorrelationIdError(req.headers["x-correlation-id"]);
76+
if (correlationIdError !== null) {
77+
const [errorCode, errorMessage] = correlationIdError;
78+
sendError(res, errorCode, errorMessage);
79+
next();
80+
return;
81+
82+
}
83+
7484
writeLog(res, "warn", {
7585
message: "/api/v1/messages",
7686
req: {

0 commit comments

Comments
 (0)