Advertisement
Lulunga

Unit Testing 01. Request Validator

Oct 21st, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(obj) {
  2.     const validMethods = ['GET', 'POST', 'DELETE', 'CONNECT'];
  3.     const uriRegex = /^[A-z0-9.]+$/gm;
  4.     const validVersion = ['HTTP/0.9', 'HTTP/1.0', 'HTTP/1.1', 'HTTP/2.0'];
  5.     const messageRegex = /(^[^<>&\\'"]*$)/gm;
  6.  
  7.     if(!(obj.method && validMethods.includes(obj.method))) {
  8.         throw new Error('Invalid request header: Invalid Method');
  9.     }
  10.  
  11.     if(!(obj.uri && (obj.uri.match(uriRegex) || obj.uri == '*'))) {
  12.         throw new Error('Invalid request header: Invalid URI');
  13.     }
  14.  
  15.     if(!(obj.version && validVersion.includes(obj.version))) {
  16.         throw new Error('Invalid request header: Invalid Version');
  17.     }
  18.  
  19.     if(!(obj.hasOwnProperty('message') && obj.message.match(messageRegex))) {
  20.         throw new Error('Invalid request header: Invalid Message');
  21.     }
  22.  
  23.     return obj;
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement