|
1 | 1 | import { describe, expect, test as it } from 'vitest' |
2 | 2 |
|
3 | | -import buildQuery, {Expand, OrderBy, alias, json, ITEM_ROOT, decimal, TypedFilter} from '../src/index'; |
| 3 | +import buildQuery, {Expand, OrderBy, alias, json, ITEM_ROOT, decimal, TypedFilter, escapeODataStringLiteral, escapeIllegalChars} from '../src/index'; |
4 | 4 | import { Square } from './test-interface'; |
5 | 5 |
|
6 | 6 | it('should return an empty string by default', () => { |
@@ -1881,3 +1881,77 @@ describe('format', () => { |
1881 | 1881 | expect(actual).toEqual(expected); |
1882 | 1882 | }); |
1883 | 1883 | }); |
| 1884 | + |
| 1885 | +describe('escapeODataStringLiteral', () => { |
| 1886 | + it('should return plain strings unchanged', () => { |
| 1887 | + const value = 'Hello'; |
| 1888 | + const expected = 'Hello'; |
| 1889 | + const actual = escapeODataStringLiteral(value); |
| 1890 | + expect(actual).toEqual(expected); |
| 1891 | + }); |
| 1892 | + |
| 1893 | + it('should return an empty string unchanged', () => { |
| 1894 | + const value = ''; |
| 1895 | + const expected = ''; |
| 1896 | + const actual = escapeODataStringLiteral(value); |
| 1897 | + expect(actual).toEqual(expected); |
| 1898 | + }); |
| 1899 | + |
| 1900 | + it('should double a single quote', () => { |
| 1901 | + const value = "O'Brien"; |
| 1902 | + const expected = "O''Brien"; |
| 1903 | + const actual = escapeODataStringLiteral(value); |
| 1904 | + expect(actual).toEqual(expected); |
| 1905 | + }); |
| 1906 | + |
| 1907 | + it('should double every quote when multiple are present', () => { |
| 1908 | + const value = "'a'b'"; |
| 1909 | + const expected = "''a''b''"; |
| 1910 | + const actual = escapeODataStringLiteral(value); |
| 1911 | + expect(actual).toEqual(expected); |
| 1912 | + }); |
| 1913 | + |
| 1914 | + it('should not URL-encode reserved characters', () => { |
| 1915 | + const value = 'a&b#c d'; |
| 1916 | + const expected = 'a&b#c d'; |
| 1917 | + const actual = escapeODataStringLiteral(value); |
| 1918 | + expect(actual).toEqual(expected); |
| 1919 | + }); |
| 1920 | + |
| 1921 | + it('should preserve unicode characters as-is', () => { |
| 1922 | + const value = 'café'; |
| 1923 | + const expected = 'café'; |
| 1924 | + const actual = escapeODataStringLiteral(value); |
| 1925 | + expect(actual).toEqual(expected); |
| 1926 | + }); |
| 1927 | +}); |
| 1928 | + |
| 1929 | +describe('escapeIllegalChars', () => { |
| 1930 | + it('should not URL-encode single quotes', () => { |
| 1931 | + const value = "O'Brien"; |
| 1932 | + const expected = "O''Brien"; |
| 1933 | + const actual = escapeIllegalChars(value); |
| 1934 | + expect(actual).toEqual(expected); |
| 1935 | + }); |
| 1936 | + |
| 1937 | + it('should URL-encode reserved characters', () => { |
| 1938 | + const value = 'a&b#c d'; |
| 1939 | + const expected = 'a%26b%23c%20d'; |
| 1940 | + const actual = escapeIllegalChars(value); |
| 1941 | + expect(actual).toEqual(expected); |
| 1942 | + }); |
| 1943 | + |
| 1944 | + it('should double quotes and URL-encode reserved characters', () => { |
| 1945 | + const value = "O'Brien & co"; |
| 1946 | + const expected = "O''Brien%20%26%20co"; |
| 1947 | + const actual = escapeIllegalChars(value); |
| 1948 | + expect(actual).toEqual(expected); |
| 1949 | + }); |
| 1950 | + |
| 1951 | + it('should equal encodeURIComponent of escapeODataStringLiteral', () => { |
| 1952 | + const value = "O'Brien & co"; |
| 1953 | + const expected = encodeURIComponent(escapeODataStringLiteral(value)); |
| 1954 | + const actual = escapeIllegalChars(value); |
| 1955 | + expect(actual).toEqual(expected); |
| 1956 | + }); |
| 1957 | +}); |
0 commit comments