api.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. import isEqual from 'lodash/isEqual';
  2. import * as ApiNamespace from 'sentry/api';
  3. const RealApi: typeof ApiNamespace = jest.requireActual('sentry/api');
  4. export class Request {}
  5. export const initApiClientErrorHandling = RealApi.initApiClientErrorHandling;
  6. const respond = (isAsync: boolean, fn?: Function, ...args: any[]): void => {
  7. if (!fn) {
  8. return;
  9. }
  10. if (isAsync) {
  11. setTimeout(() => fn(...args), 1);
  12. return;
  13. }
  14. fn(...args);
  15. };
  16. type FunctionCallback<Args extends any[] = any[]> = (...args: Args) => void;
  17. /**
  18. * Callables for matching requests based on arbitrary conditions.
  19. */
  20. interface MatchCallable {
  21. (url: string, options: ApiNamespace.RequestOptions): boolean;
  22. }
  23. type ResponseType = ApiNamespace.ResponseMeta & {
  24. url: string;
  25. statusCode: number;
  26. method: string;
  27. callCount: 0;
  28. match: MatchCallable[];
  29. body: any;
  30. headers: Record<string, string>;
  31. };
  32. type MockResponse = [resp: ResponseType, mock: jest.Mock];
  33. /**
  34. * Compare two records. `want` is all the entries we want to have the same value in `check`
  35. */
  36. function compareRecord(want: Record<string, any>, check: Record<string, any>): boolean {
  37. for (const entry of Object.entries(want)) {
  38. const [key, value] = entry;
  39. if (!isEqual(check[key], value)) {
  40. return false;
  41. }
  42. }
  43. return true;
  44. }
  45. class Client implements ApiNamespace.Client {
  46. static mockResponses: MockResponse[] = [];
  47. static mockAsync = false;
  48. static clearMockResponses() {
  49. Client.mockResponses = [];
  50. }
  51. /**
  52. * Create a query string match callable.
  53. *
  54. * Only keys/values defined in `query` are checked.
  55. */
  56. static matchQuery(query: Record<string, any>): MatchCallable {
  57. const queryMatcher: MatchCallable = (_url, options) => {
  58. return compareRecord(query, options.query ?? {});
  59. };
  60. return queryMatcher;
  61. }
  62. /**
  63. * Create a data match callable.
  64. *
  65. * Only keys/values defined in `data` are checked.
  66. */
  67. static matchData(data: Record<string, any>): MatchCallable {
  68. const dataMatcher: MatchCallable = (_url, options) => {
  69. return compareRecord(data, options.data ?? {});
  70. };
  71. return dataMatcher;
  72. }
  73. // Returns a jest mock that represents Client.request calls
  74. static addMockResponse(response: Partial<ResponseType>) {
  75. const mock = jest.fn();
  76. Client.mockResponses.unshift([
  77. {
  78. url: '',
  79. status: 200,
  80. statusCode: 200,
  81. statusText: 'OK',
  82. responseText: '',
  83. responseJSON: '',
  84. body: '',
  85. method: 'GET',
  86. callCount: 0,
  87. match: [],
  88. ...response,
  89. headers: response.headers ?? {},
  90. getResponseHeader: (key: string) => response.headers?.[key] ?? null,
  91. },
  92. mock,
  93. ]);
  94. return mock;
  95. }
  96. static findMockResponse(url: string, options: Readonly<ApiNamespace.RequestOptions>) {
  97. return Client.mockResponses.find(([response]) => {
  98. if (url !== response.url) {
  99. return false;
  100. }
  101. if ((options.method || 'GET') !== response.method) {
  102. return false;
  103. }
  104. return response.match.every(matcher => matcher(url, options));
  105. });
  106. }
  107. activeRequests: Record<string, ApiNamespace.Request> = {};
  108. baseUrl = '';
  109. uniqueId() {
  110. return '123';
  111. }
  112. /**
  113. * In the real client, this clears in-flight responses. It's NOT
  114. * clearMockResponses. You probably don't want to call this from a test.
  115. */
  116. clear() {}
  117. wrapCallback<T extends any[]>(
  118. _id: string,
  119. func: FunctionCallback<T> | undefined,
  120. _cleanup: boolean = false
  121. ) {
  122. return (...args: T) => {
  123. // @ts-expect-error
  124. if (RealApi.hasProjectBeenRenamed(...args)) {
  125. return;
  126. }
  127. respond(Client.mockAsync, func, ...args);
  128. };
  129. }
  130. requestPromise(
  131. path: string,
  132. {
  133. includeAllArgs,
  134. ...options
  135. }: {includeAllArgs?: boolean} & Readonly<ApiNamespace.RequestOptions> = {}
  136. ): any {
  137. return new Promise((resolve, reject) => {
  138. this.request(path, {
  139. ...options,
  140. success: (data, ...args) => {
  141. includeAllArgs ? resolve([data, ...args]) : resolve(data);
  142. },
  143. error: (error, ..._args) => {
  144. reject(error);
  145. },
  146. });
  147. });
  148. }
  149. // XXX(ts): We type the return type for requestPromise and request as `any`. Typically these woul
  150. request(url: string, options: Readonly<ApiNamespace.RequestOptions> = {}): any {
  151. const [response, mock] = Client.findMockResponse(url, options) || [
  152. undefined,
  153. undefined,
  154. ];
  155. if (!response || !mock) {
  156. // Endpoints need to be mocked
  157. const err = new Error(
  158. `No mocked response found for request: ${options.method || 'GET'} ${url}`
  159. );
  160. // Mutate stack to drop frames since test file so that we know where in the test
  161. // this needs to be mocked
  162. const lines = err.stack?.split('\n');
  163. const startIndex = lines?.findIndex(line => line.includes('tests/js/spec'));
  164. err.stack = ['\n', lines?.[0], ...(lines?.slice(startIndex) ?? [])].join('\n');
  165. // Throwing an error here does not do what we want it to do....
  166. // Because we are mocking an API client, we generally catch errors to show
  167. // user-friendly error messages, this means in tests this error gets gobbled
  168. // up and developer frustration ensues. Use `setTimeout` to get around this
  169. setTimeout(() => {
  170. throw err;
  171. });
  172. } else {
  173. // has mocked response
  174. // mock gets returned when we add a mock response, will represent calls to api.request
  175. mock(url, options);
  176. const body =
  177. typeof response.body === 'function' ? response.body(url, options) : response.body;
  178. if (![200, 202].includes(response.statusCode)) {
  179. response.callCount++;
  180. const errorResponse = Object.assign(
  181. {
  182. status: response.statusCode,
  183. responseText: JSON.stringify(body),
  184. responseJSON: body,
  185. },
  186. {
  187. overrideMimeType: () => {},
  188. abort: () => {},
  189. then: () => {},
  190. error: () => {},
  191. },
  192. new XMLHttpRequest()
  193. );
  194. this.handleRequestError(
  195. {
  196. id: '1234',
  197. path: url,
  198. requestOptions: options,
  199. },
  200. errorResponse as any,
  201. 'error',
  202. 'error'
  203. );
  204. } else {
  205. response.callCount++;
  206. respond(
  207. Client.mockAsync,
  208. options.success,
  209. body,
  210. {},
  211. {
  212. getResponseHeader: (key: string) => response.headers[key],
  213. statusCode: response.statusCode,
  214. status: response.statusCode,
  215. }
  216. );
  217. }
  218. }
  219. respond(Client.mockAsync, options.complete);
  220. }
  221. handleRequestError = RealApi.Client.prototype.handleRequestError;
  222. }
  223. export {Client};