AxiosStrategy-Proxy.spec.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import axios from "axios"
  2. import axiosStrategy, {
  3. testables,
  4. cancelRunningAxiosRequest,
  5. } from "../AxiosStrategy"
  6. jest.mock("../../utils/b64", () => ({
  7. __esModule: true,
  8. decodeB64StringToArrayBuffer: jest.fn((data) => `${data}-converted`),
  9. }))
  10. jest.mock("~/newstore/settings", () => {
  11. return {
  12. __esModule: true,
  13. settingsStore: {
  14. value: {
  15. PROXY_ENABLED: true,
  16. PROXY_URL: "test",
  17. },
  18. },
  19. }
  20. })
  21. describe("cancelRunningAxiosRequest", () => {
  22. test("cancels axios request and does that only 1 time", () => {
  23. const cancelFunc = jest.spyOn(testables.cancelSource, "cancel")
  24. cancelRunningAxiosRequest()
  25. expect(cancelFunc).toHaveBeenCalledTimes(1)
  26. })
  27. })
  28. describe("axiosStrategy", () => {
  29. describe("Proxy Requests", () => {
  30. test("sends POST request to proxy if proxy is enabled", async () => {
  31. let passedURL
  32. jest.spyOn(axios, "post").mockImplementation((url) => {
  33. passedURL = url
  34. return Promise.resolve({ data: { success: true, isBinary: false } })
  35. })
  36. await axiosStrategy({})
  37. expect(passedURL).toEqual("test")
  38. })
  39. test("passes request fields to axios properly", async () => {
  40. const reqFields = {
  41. testA: "testA",
  42. testB: "testB",
  43. testC: "testC",
  44. }
  45. let passedFields
  46. jest.spyOn(axios, "post").mockImplementation((_url, req) => {
  47. passedFields = req
  48. return Promise.resolve({ data: { success: true, isBinary: false } })
  49. })
  50. await axiosStrategy(reqFields)
  51. expect(passedFields).toMatchObject(reqFields)
  52. })
  53. test("passes wantsBinary field", async () => {
  54. let passedFields
  55. jest.spyOn(axios, "post").mockImplementation((_url, req) => {
  56. passedFields = req
  57. return Promise.resolve({ data: { success: true, isBinary: false } })
  58. })
  59. await axiosStrategy({})
  60. expect(passedFields).toHaveProperty("wantsBinary")
  61. })
  62. test("checks for proxy response success field and throws error message for non-success", async () => {
  63. jest.spyOn(axios, "post").mockResolvedValue({
  64. data: {
  65. success: false,
  66. data: {
  67. message: "test message",
  68. },
  69. },
  70. })
  71. await expect(axiosStrategy({})).rejects.toThrow("test message")
  72. })
  73. test("checks for proxy response success field and throws error 'Proxy Error' for non-success", async () => {
  74. jest.spyOn(axios, "post").mockResolvedValue({
  75. data: {
  76. success: false,
  77. data: {},
  78. },
  79. })
  80. await expect(axiosStrategy({})).rejects.toThrow("Proxy Error")
  81. })
  82. test("checks for proxy response success and doesn't throw for success", async () => {
  83. jest.spyOn(axios, "post").mockResolvedValue({
  84. data: {
  85. success: true,
  86. data: {},
  87. },
  88. })
  89. await expect(axiosStrategy({})).resolves.toBeDefined()
  90. })
  91. test("checks isBinary response field and resolve with the converted value if so", async () => {
  92. jest.spyOn(axios, "post").mockResolvedValue({
  93. data: {
  94. success: true,
  95. isBinary: true,
  96. data: "testdata",
  97. },
  98. })
  99. await expect(axiosStrategy({})).resolves.toMatchObject({
  100. data: "testdata-converted",
  101. })
  102. })
  103. test("checks isBinary response field and resolve with the actual value if not so", async () => {
  104. jest.spyOn(axios, "post").mockResolvedValue({
  105. data: {
  106. success: true,
  107. isBinary: false,
  108. data: "testdata",
  109. },
  110. })
  111. await expect(axiosStrategy({})).resolves.toMatchObject({
  112. data: "testdata",
  113. })
  114. })
  115. test("cancel errors are thrown with the string 'cancellation'", async () => {
  116. jest.spyOn(axios, "post").mockRejectedValue("errr")
  117. jest.spyOn(axios, "isCancel").mockReturnValueOnce(true)
  118. await expect(axiosStrategy({})).rejects.toBe("cancellation")
  119. })
  120. test("non-cancellation errors are thrown", async () => {
  121. jest.spyOn(axios, "post").mockRejectedValue("errr")
  122. jest.spyOn(axios, "isCancel").mockReturnValueOnce(false)
  123. await expect(axiosStrategy({})).rejects.toBe("errr")
  124. })
  125. })
  126. })