All files / src/__tests__/connectors http.ts

100% Statements 64/64
100% Branches 0/0
100% Functions 12/12
100% Lines 58/58
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 1171x       1x 1x 1x 1x 1x       1x 1x 1x         1x 1x     1x 1x     1x 1x 1x     1x 1x     1x 1x     1x 1x     1x 1x     1x 1x   1x     1x 1x   1x     1x 1x         1x     1x   1x 1x 1x 1x 1x       1x   1x 1x 1x 1x 1x       1x 1x         1x     1x   1x 1x 1x 1x       1x   1x 1x 1x 1x        
import express from 'express';
import { Server } from 'http';
import { HttpConnector } from '../..';
 
describe('Http connector', () => {
  const PORT = 5001;
  const URL = 'http://localhost:' + PORT;
  const NETWORK_ERROR_CODE = 'network_error';
  const NETWORK_ERROR_MESSAGE = 'A network error has occurred. Please retry';
 
  let server: Server;
 
  const accessToken = '12345';
  const app = express();
  const connector = new HttpConnector({
    createAccessTokenUrl: URL + '/accessToken',
    logoutUrl: URL + '/logout'
  });
 
  app.get('/accessToken', (_req, res) => {
    res.json({ accessToken });
  });
 
  app.get('/logout', (_req, res) => {
    res.json({ done: true });
  });
 
  app.get('/error', (_req, res) => {
    res.statusMessage = 'failed';
    res.status(400).end();
  });
 
  app.get('/customErrorMsg', (_req, res) => {
    res.status(400).json({ message: 'failed' });
  });
 
  app.get('/customErrorCode', (_req, res) => {
    res.status(400).json({ code: 'it_failed' });
  });
 
  beforeAll(() => {
    server = app.listen(PORT);
  });
 
  afterAll(() => {
    server.close();
  });
 
  it('Creates an accessToken', async () => {
    const data = await connector.createAccessToken({});
 
    expect(data).toEqual({ accessToken });
  });
 
  it('Logouts', async () => {
    const data = await connector.logout({});
 
    expect(data).toEqual({ done: true });
  });
 
  it('Throws a FetchError with a custom error object', async () => {
    const c = new HttpConnector({
      createAccessTokenUrl: URL + '/customErrorMsg',
      logoutUrl: URL + '/customErrorCode'
    });
 
    expect.assertions(10);
 
    try {
      await c.createAccessToken({});
    } catch (e) {
      expect(e.name).toBe('FetchError');
      expect(e.status).toBe(400);
      expect(e.code).toBe(400);
      expect(e.message).toBe('failed');
      expect(e.res).toBeDefined();
    }
 
    try {
      await c.logout({});
    } catch (e) {
      expect(e.name).toBe('FetchError');
      expect(e.status).toBe(400);
      expect(e.code).toBe('it_failed');
      expect(typeof e.message).toBe('string');
      expect(e.res).toBeDefined();
    }
  });
 
  it('Throws a NetworkError for invalid requests', async () => {
    const c = new HttpConnector({
      createAccessTokenUrl: URL + '/error',
      logoutUrl: 'invalid url'
    });
 
    expect.assertions(8);
 
    try {
      await c.createAccessToken({});
    } catch (e) {
      expect(e.name).toBe('NetworkError');
      expect(e.code).toBe(NETWORK_ERROR_CODE);
      expect(e.message).toBe(NETWORK_ERROR_MESSAGE);
      expect(e.res).toBeDefined();
    }
 
    try {
      await c.logout({});
    } catch (e) {
      expect(e.name).toBe('NetworkError');
      expect(e.code).toBe(NETWORK_ERROR_CODE);
      expect(e.message).toBe(NETWORK_ERROR_MESSAGE);
      expect(e.res).toBeUndefined();
    }
  });
});