๐Ÿ“ฆ Kong / httpsnippet

๐Ÿ“„ escape.test.ts ยท 28 lines
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
28import { escapeString } from './escape';

describe('escape methods', () => {
  describe('escapeString', () => {
    it('does nothing to a safe string', () => {
      expect(escapeString('hello world')).toBe('hello world');
    });

    it('escapes double quotes by default', () => {
      expect(escapeString('"hello world"')).toBe('\\"hello world\\"');
    });

    it('escapes newlines by default', () => {
      expect(escapeString('hello\r\nworld')).toBe('hello\\r\\nworld');
    });

    it('escapes backslashes', () => {
      expect(escapeString('hello\\world')).toBe('hello\\\\world');
    });

    it('escapes unrepresentable characters', () => {
      expect(
        escapeString('hello \u0000'), // 0 = ASCII 'null' character
      ).toBe('hello \\u0000');
    });
  });
});