utils.spec.jsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import {
  2. getCurlCommand,
  3. objectToSortedTupleArray,
  4. removeFilterMaskedEntries,
  5. stringifyQueryList,
  6. } from 'sentry/components/events/interfaces/utils';
  7. import {MetaProxy, withMeta} from 'sentry/components/events/meta/metaProxy';
  8. import {FILTER_MASK} from 'sentry/constants';
  9. describe('components/interfaces/utils', function () {
  10. describe('getCurlCommand()', function () {
  11. it('should convert an http request object to an equivalent unix curl command string', function () {
  12. expect(
  13. getCurlCommand({
  14. cookies: [
  15. ['foo', 'bar'],
  16. ['biz', 'baz'],
  17. ],
  18. url: 'http://example.com/foo',
  19. headers: [
  20. ['Referer', 'http://example.com'],
  21. [
  22. 'User-Agent',
  23. 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36',
  24. ],
  25. ['Content-Type', 'application/json'],
  26. ],
  27. env: {
  28. ENV: 'prod',
  29. },
  30. fragment: '',
  31. query: [['foo', 'bar']],
  32. data: '{"hello": "world"}',
  33. method: 'GET',
  34. })
  35. ).toEqual(
  36. 'curl \\\n' +
  37. ' -H "Content-Type: application/json" \\\n' +
  38. ' -H "Referer: http://example.com" \\\n' +
  39. ' -H "User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36" \\\n' +
  40. ' --data "{\\"hello\\": \\"world\\"}" \\\n' +
  41. ' "http://example.com/foo?foo=bar"'
  42. );
  43. // --compressed (because Accept-Encoding: gzip)
  44. expect(
  45. getCurlCommand({
  46. url: 'http://example.com/foo',
  47. headers: [
  48. ['Content-Type', 'application/json'],
  49. ['Referer', 'http://example.com'],
  50. ['Accept-Encoding', 'gzip'],
  51. ],
  52. env: {
  53. ENV: 'prod',
  54. },
  55. fragment: '',
  56. query: [['foo', 'bar']],
  57. data: '{"hello": "world"}',
  58. method: 'GET',
  59. })
  60. ).toEqual(
  61. 'curl \\\n' +
  62. ' --compressed \\\n' +
  63. ' -H "Accept-Encoding: gzip" \\\n' +
  64. ' -H "Content-Type: application/json" \\\n' +
  65. ' -H "Referer: http://example.com" \\\n' +
  66. ' --data "{\\"hello\\": \\"world\\"}" \\\n' +
  67. ' "http://example.com/foo?foo=bar"'
  68. );
  69. // Do not add data if data is empty
  70. expect(
  71. getCurlCommand({
  72. url: 'http://example.com/foo',
  73. headers: [],
  74. env: {
  75. ENV: 'prod',
  76. },
  77. fragment: '',
  78. query: [['foo', 'bar']],
  79. method: 'GET',
  80. })
  81. ).toEqual('curl \\\n "http://example.com/foo?foo=bar"');
  82. // Do not add data if data is empty object
  83. expect(
  84. getCurlCommand({
  85. url: 'http://example.com/foo',
  86. headers: [],
  87. env: {
  88. ENV: 'prod',
  89. },
  90. inferredContentType: null,
  91. fragment: '',
  92. data: {},
  93. method: 'GET',
  94. })
  95. ).toEqual('curl \\\n "http://example.com/foo"');
  96. // Escape escaped strings.
  97. expect(
  98. getCurlCommand({
  99. cookies: [
  100. ['foo', 'bar'],
  101. ['biz', 'baz'],
  102. ],
  103. url: 'http://example.com/foo',
  104. headers: [
  105. ['Referer', 'http://example.com'],
  106. [
  107. 'User-Agent',
  108. 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36',
  109. ],
  110. ['Content-Type', 'application/json'],
  111. ],
  112. env: {
  113. ENV: 'prod',
  114. },
  115. fragment: '',
  116. query: [],
  117. data: '{"a":"b\\"c"}',
  118. method: 'GET',
  119. })
  120. ).toEqual(
  121. 'curl \\\n' +
  122. ' -H "Content-Type: application/json" \\\n' +
  123. ' -H "Referer: http://example.com" \\\n' +
  124. ' -H "User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36" \\\n' +
  125. ' --data "{\\"a\\":\\"b\\\\\\"c\\"}" \\\n' +
  126. ' "http://example.com/foo"'
  127. );
  128. });
  129. it('works with a Proxy', function () {
  130. const spy = jest.spyOn(MetaProxy.prototype, 'get');
  131. const data = {
  132. fragment: '',
  133. cookies: [],
  134. inferredContentType: null,
  135. env: {
  136. SERVER_NAME: 'sentry',
  137. SERVER_PORT: '443',
  138. REMOTE_ADDR: '127.0.0.1',
  139. },
  140. headers: [
  141. ['Accept-Language', 'en'],
  142. ['Referer', 'http://example.com'],
  143. [
  144. 'User-Agent',
  145. 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36',
  146. ],
  147. ['Content-Type', 'application/json'],
  148. ['Referer', 'http://example.com'],
  149. ['Accept-Encoding', 'gzip'],
  150. ],
  151. url: 'https://www.sentry.io',
  152. query: [],
  153. data: null,
  154. method: 'GET',
  155. };
  156. const eventWithProxy = withMeta(data);
  157. getCurlCommand(eventWithProxy);
  158. // This may need to change, but we should aim to keep this low
  159. expect(spy.mock.calls.length).toBeLessThan(200);
  160. });
  161. });
  162. describe('objectToSortedTupleArray()', function () {
  163. it('should convert a key/value object to a sorted array of key/value tuples', function () {
  164. expect(
  165. objectToSortedTupleArray({
  166. foo: ['bar', 'baz'],
  167. })
  168. ).toEqual([
  169. ['foo', 'bar'],
  170. ['foo', 'baz'],
  171. ]);
  172. });
  173. });
  174. describe('removeFilterMaskedEntries()', function () {
  175. const rawData = {
  176. id: '26',
  177. name: FILTER_MASK,
  178. username: 'maiseythedog',
  179. email: FILTER_MASK,
  180. };
  181. it('should remove filtered values', function () {
  182. const result = removeFilterMaskedEntries(rawData);
  183. expect(result).not.toHaveProperty('name');
  184. expect(result).not.toHaveProperty('email');
  185. });
  186. it('should preserve unfiltered values', function () {
  187. const result = removeFilterMaskedEntries(rawData);
  188. expect(result).toHaveProperty('id');
  189. expect(result.id).toEqual('26');
  190. expect(result).toHaveProperty('username');
  191. expect(result.username).toEqual('maiseythedog');
  192. });
  193. });
  194. describe('stringifyQueryList()', function () {
  195. it('should return query if it is a string', function () {
  196. const query = stringifyQueryList('query');
  197. expect(query).toEqual('query');
  198. });
  199. it('should parse query tuples', function () {
  200. const query = stringifyQueryList([
  201. ['field', 'ops.http'],
  202. ['field', 'ops.db'],
  203. ['field', 'total.time'],
  204. ['numBuckets', '100'],
  205. ]);
  206. expect(query).toEqual(
  207. 'field=ops.http&field=ops.db&field=total.time&numBuckets=100'
  208. );
  209. });
  210. });
  211. });