Skip to content

Commit

Permalink
Add tests to the addCustomField method and catch error when key conta…
Browse files Browse the repository at this point in the history
…ins a period
  • Loading branch information
matheusbsilva137 committed Jan 13, 2021
1 parent 8073acb commit 2f57f3a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/server/accessors/MessageBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ export class MessageBuilder implements IMessageBuilder {
throw new Error(`The message already contains a custom field by the key: ${ key }`);
}

if (key.includes('.')) {
throw new Error(`The given key contains a period, which is not allowed. Key: ${ key }`);
}

this.msg.customFields[key] = value;
return this;
}
Expand Down
6 changes: 6 additions & 0 deletions tests/server/accessors/MessageBuilder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,11 @@ export class MessageBuilderAccessorTestFixture {
Expect(mb.setParseUrls(false)).toBe(mb);
Expect(msg.parseUrls).toEqual(false);
Expect(mb.getParseUrls()).toEqual(false);

Expect(mb.addCustomField('thing', 'value')).toBe(mb);
Expect(msg.customFields).toBeDefined();
Expect(msg.customFields.thing).toBe('value');
Expect(() => mb.addCustomField('thing', 'second')).toThrowError(Error, 'The message already contains a custom field by the key: thing');
Expect(() => mb.addCustomField('thing.', 'second')).toThrowError(Error, 'The given key contains a period, which is not allowed. Key: thing.');
}
}

0 comments on commit 2f57f3a

Please sign in to comment.