utils.spec.jsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. // Escape strings with special bash characters
  129. expect(
  130. getCurlCommand({
  131. url: 'http://example.com/foo${not_a_variable}',
  132. headers: [
  133. ['Referer', 'http://example.com'],
  134. [
  135. 'User-Agent',
  136. 'Mozilla/5.0 ("Windows" NT 6.2; WOW64) $not_a_variable `test`',
  137. ],
  138. ['Content-Type', 'application/json'],
  139. ],
  140. fragment: '',
  141. query: [],
  142. data: '{"a$TEST":"b\\"c"}',
  143. method: 'GET',
  144. })
  145. ).toEqual(
  146. 'curl \\\n' +
  147. ' -H "Content-Type: application/json" \\\n' +
  148. ' -H "Referer: http://example.com" \\\n' +
  149. ' -H "User-Agent: Mozilla/5.0 (\\"Windows\\" NT 6.2; WOW64) \\$not_a_variable \\`test\\`" \\\n' +
  150. ' --data "{\\"a\\$TEST\\":\\"b\\\\\\"c\\"}" \\\n' +
  151. ' "http://example.com/foo\\${not_a_variable}"'
  152. );
  153. });
  154. it('works with a Proxy', function () {
  155. const spy = jest.spyOn(MetaProxy.prototype, 'get');
  156. const data = {
  157. fragment: '',
  158. cookies: [],
  159. inferredContentType: null,
  160. env: {
  161. SERVER_NAME: 'sentry',
  162. SERVER_PORT: '443',
  163. REMOTE_ADDR: '127.0.0.1',
  164. },
  165. headers: [
  166. ['Accept-Language', 'en'],
  167. ['Referer', 'http://example.com'],
  168. [
  169. 'User-Agent',
  170. 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36',
  171. ],
  172. ['Content-Type', 'application/json'],
  173. ['Referer', 'http://example.com'],
  174. ['Accept-Encoding', 'gzip'],
  175. ],
  176. url: 'https://www.sentry.io',
  177. query: [],
  178. data: null,
  179. method: 'GET',
  180. };
  181. const eventWithProxy = withMeta(data);
  182. getCurlCommand(eventWithProxy);
  183. // This may need to change, but we should aim to keep this low
  184. expect(spy.mock.calls.length).toBeLessThan(200);
  185. });
  186. });
  187. describe('objectToSortedTupleArray()', function () {
  188. it('should convert a key/value object to a sorted array of key/value tuples', function () {
  189. expect(
  190. objectToSortedTupleArray({
  191. foo: ['bar', 'baz'],
  192. })
  193. ).toEqual([
  194. ['foo', 'bar'],
  195. ['foo', 'baz'],
  196. ]);
  197. });
  198. });
  199. describe('removeFilterMaskedEntries()', function () {
  200. const rawData = {
  201. id: '26',
  202. name: FILTER_MASK,
  203. username: 'maiseythedog',
  204. email: FILTER_MASK,
  205. };
  206. it('should remove filtered values', function () {
  207. const result = removeFilterMaskedEntries(rawData);
  208. expect(result).not.toHaveProperty('name');
  209. expect(result).not.toHaveProperty('email');
  210. });
  211. it('should preserve unfiltered values', function () {
  212. const result = removeFilterMaskedEntries(rawData);
  213. expect(result).toHaveProperty('id');
  214. expect(result.id).toEqual('26');
  215. expect(result).toHaveProperty('username');
  216. expect(result.username).toEqual('maiseythedog');
  217. });
  218. });
  219. describe('stringifyQueryList()', function () {
  220. it('should return query if it is a string', function () {
  221. const query = stringifyQueryList('query');
  222. expect(query).toEqual('query');
  223. });
  224. it('should parse query tuples', function () {
  225. const query = stringifyQueryList([
  226. ['field', 'ops.http'],
  227. ['field', 'ops.db'],
  228. ['field', 'total.time'],
  229. ['numBuckets', '100'],
  230. ]);
  231. expect(query).toEqual(
  232. 'field=ops.http&field=ops.db&field=total.time&numBuckets=100'
  233. );
  234. });
  235. });
  236. });