Advertisement
Guest User

Untitled

a guest
Jun 21st, 2022
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export async function script2({url, expectStatus, expectText}: {
  2.   url: string;
  3.   expectStatus?: number;
  4.   expectText?: string;
  5. }): Promise<{
  6.   pass: boolean;
  7.   error?: string;
  8. }> {
  9.   try {
  10.     const response = await fetch(url, {});
  11.     if (expectStatus && response.status !== expectStatus) {
  12.       return {
  13.         pass: false,
  14.         error: `Server returned status ${response.status} where ${expectStatus} was expected`
  15.       };
  16.     }
  17.     if (expectText) {
  18.       const body = await response.text();
  19.       if (body.indexOf(expectText) < 0) {
  20.         return {
  21.           pass: false,
  22.           error: `Response did not contain the expected text "${expectText}"`
  23.         };
  24.       }
  25.     }
  26.     return {
  27.       pass: true
  28.     };
  29.   } catch (error) {
  30.     return {
  31.       pass: false,
  32.       error: error.message
  33.     };
  34.   }
  35. }
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement