Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
Merge pull request #28 from BonnierNews/feature/handle-empty-trigger-…
Browse files Browse the repository at this point in the history
…response

handle empty trigger response
  • Loading branch information
ebbalindstrom authored Oct 26, 2023
2 parents da0e6a6 + e3fef31 commit 6e9b89e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
11 changes: 8 additions & 3 deletions lib/message-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,16 @@ export default async function messageHandler(recipeMap, req, res) {
logger.info(`incoming message ${JSON.stringify(messageData)}`);
const parts = key.split(".");
const suffix = parts.pop();
const [ prefix, ...rest ] = parts;
const [ prefix ] = parts;
// Check if it is a trigger message and if so start the sequence at first step
if (prefix === "trigger") {
const { key: triggerKey, triggerMessages, error } = rest.length === 0 && await runTrigger(recipeMap.triggerHandler(key), message, context);

const triggerHandler = recipeMap.triggerHandler(key);
const triggerResult = await runTrigger(triggerHandler, message, context);
if (triggerHandler && !triggerResult) {
logger.info(`Found nothing to trigger for message ${messageId}`);
return res.status(200).send();
}
const { key: triggerKey, triggerMessages, error } = triggerResult;
if (error) {
logger.error(`Got error running trigger for message with correlationId: ${correlationId}. Error: ${error}. Message: ${message}`);
return res.status(400).send();
Expand Down
35 changes: 34 additions & 1 deletion test/feature/http-call-feature.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,40 @@ Feature("Make http call from lambda", () => {
response.statusCode.should.eql(400, response.text);
});

And("we should have published 4 messages", () => {
And("we should have published 0 messages", () => {
fakePubSub.recordedMessages().length.should.eql(0);
});
});

Scenario("Trigger a trigger handler from http, empty result from trigger", () => {
let broker;

Given("broker is initiated with a recipe", () => {
broker = start({
startServer: false,
triggers: {
"trigger.order": () => {
return;
},
},
recipes: [],
});
});

And("we can publish messages", () => {
fakePubSub.enablePublish(broker);
});

let response;
When("a trigger http call is received for an unknown sequence", async () => {
response = await fakePubSub.triggerMessage(broker, triggerMessage, { key: "trigger.order" });
});

Then("the status code should be 200 OK", () => {
response.statusCode.should.eql(200, response.text);
});

And("we should have published 0 messages", () => {
fakePubSub.recordedMessages().length.should.eql(0);
});
});
Expand Down

0 comments on commit 6e9b89e

Please sign in to comment.