utils.spec.jsx 6.2 KB

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