openapi.test.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import tcb from '../../src/index'
  2. import assert from 'assert'
  3. import config from '../config.local'
  4. import { ERROR } from '../../lib/const/code'
  5. // TODO 删除前先创建
  6. describe('wx.openApi: 微信openapi', () => {
  7. const app = tcb.init(config)
  8. it('传参JSON.stringify报错', async () => {
  9. let a: any = {}
  10. let b: any = {}
  11. a.c = b
  12. b.c = a
  13. let result
  14. try {
  15. result = await app.callWxOpenApi({
  16. apiName: '/inner/svrkitclientcall',
  17. requestData: a
  18. })
  19. } catch (e) {
  20. assert(e.code === ERROR.INVALID_PARAM.code)
  21. }
  22. })
  23. it('微信openapi', async () => {
  24. try {
  25. let result = await app.callWxOpenApi({
  26. apiName: '/inner/svrkitclientcall',
  27. requestData: { name: 'jamespeng' }
  28. })
  29. } catch (e) {
  30. assert(e.code === ERROR.INVALID_PARAM.code)
  31. }
  32. // assert(result.result, '微信openapi失败');
  33. }, 30000)
  34. it('微信new openapi', async () => {
  35. try {
  36. let result = await app.callCompatibleWxOpenApi({
  37. apiName: '/AAA/BBB/sample',
  38. requestData: Buffer.from(JSON.stringify({ name: 'jamespeng' }))
  39. })
  40. } catch (e) {
  41. assert(e.code === ERROR.INVALID_PARAM.code)
  42. }
  43. // console.log(result)
  44. // assert(result.result, '微信openapi失败');
  45. }, 30000)
  46. it('mock callCompatibleWxOpenApi return', async () => {
  47. jest.resetModules()
  48. jest.mock('request', () => {
  49. return jest.fn().mockImplementation((params, callback) => {
  50. const body = { data: { name: 'luke' } }
  51. process.nextTick(() => {
  52. callback(null, { req: {reusedSocket: false}, statusCode: 200, body })
  53. })
  54. })
  55. })
  56. const tcb1 = require('../../src/index')
  57. const app1 = tcb1.init(config)
  58. const res = await app1.callCompatibleWxOpenApi({
  59. apiName: '/AAA/BBB/sample',
  60. requestData: Buffer.from(JSON.stringify({ name: 'jamespeng' }))
  61. })
  62. assert.ok(res.data.name === 'luke')
  63. })
  64. // mock callWxOpenApi 回包为string
  65. it('微信openapi mock回包为string', async () => {
  66. jest.resetModules()
  67. jest.mock('request', () => {
  68. return jest.fn().mockImplementation((params, callback) => {
  69. const body = { data: { responseData: 'test' } }
  70. process.nextTick(() => {
  71. callback(null, { req: {reusedSocket: false}, statusCode: 200, body })
  72. })
  73. })
  74. })
  75. const tcb1 = require('../../src/index')
  76. const app1 = tcb1.init(config)
  77. try {
  78. let result = await app1.callWxOpenApi({
  79. apiName: '/inner/svrkitclientcall',
  80. requestData: { name: 'jamespeng' }
  81. })
  82. // console.log(result)
  83. assert(typeof result.result === 'string')
  84. } catch (err) {
  85. // assert(err.code === 'STORAGE_REQUEST_FAIL')
  86. console.log(err)
  87. }
  88. })
  89. it('微信 wxPayApi', async () => {
  90. try {
  91. let result = await app.callWxPayApi({
  92. apiName: 'cloudPay.getRefundStatus',
  93. requestData: Buffer.from(JSON.stringify({ api: 'getRefundStatus', data: {} }))
  94. })
  95. } catch (e) {
  96. assert(e.code === ERROR.INVALID_PARAM.code)
  97. }
  98. // console.log(result)
  99. // assert(result.result, '微信openapi失败');
  100. }, 30000)
  101. it('mock callWxOpenApi code', async () => {
  102. jest.resetModules()
  103. jest.mock('request', () => {
  104. return jest.fn().mockImplementation((params, callback) => {
  105. const body = { code: 'mockCode', message: 'mockMessage' }
  106. process.nextTick(() => {
  107. callback(null, { req: {reusedSocket: false}, statusCode: 200, body })
  108. })
  109. })
  110. })
  111. const tcb1 = require('../../src/index')
  112. const app1 = tcb1.init(config)
  113. expect(
  114. app1.callWxOpenApi({
  115. apiName: 'cloudPay.getRefundStatus',
  116. requestData: { name: 'luke' }
  117. })
  118. ).rejects.toThrow(new Error('mockMessage'))
  119. const app2 = tcb1.init({
  120. ...config,
  121. throwOnCode: false
  122. })
  123. const res = await app2.callWxOpenApi({
  124. apiName: 'cloudPay.getRefundStatus',
  125. requestData: { name: 'luke' }
  126. })
  127. assert(res.code === 'mockCode')
  128. })
  129. it('getCrossAccountInfo err', async () => {
  130. expect(
  131. app.callWxOpenApi(
  132. {
  133. apiName: 'cloudPay.getRefundStatus',
  134. requestData: { name: 'luke' }
  135. },
  136. {
  137. getCrossAccountInfo: 'test'
  138. }
  139. )
  140. ).rejects.toThrow(new Error('invalid config: getCrossAccountInfo'))
  141. })
  142. })