Skip to content

Commit

Permalink
Merge 063d510 into 142d69f
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenao committed Sep 10, 2021
2 parents 142d69f + 063d510 commit 8a5acc4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/eventBody.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
const stringIsValidUrl = (s, protocols) => {
try {
const url = new URL(s);
return protocols
? url.protocol
? protocols.map(x => `${x.toLowerCase()}:`).includes(url.protocol)
: false
: true;
} catch (err) {
return false;
}
};

export class EventBody {
constructor() {
this._eventTypeGuid = 'd6b021b6-ffa2-4ee8-8206-8710e04396ce';
Expand Down Expand Up @@ -33,6 +46,9 @@ export class EventBody {
this._context.Type = type;
}
if (url) {
if (!stringIsValidUrl(url, ['http', 'https'])) {
throw new Error('url is not in absolute format');
}
this._context.Url = url;
}
if (value) {
Expand Down
5 changes: 5 additions & 0 deletions test/eventBody.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ describe('EventBody', () => {
}
});
});

it('should reject invalid url', () =>{
expect(()=>eventBody.setContext('1', 'Course Offering', '/context', 'contextValue')).to.throw();
});

it('should generate JSON body (using chaining)', () => {
eventBody.setDate(new Date(Date.UTC(96, 1, 2, 3, 4, 5)))
.setAction('MyAction')
Expand Down

0 comments on commit 8a5acc4

Please sign in to comment.