mock平台

test.js 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. const yapi = require('../yapi.js');
  2. const baseController = require('./base.js');
  3. const fs = require('fs'); //引入文件模块
  4. const path = require('path');
  5. class interfaceColController extends baseController {
  6. constructor(ctx) {
  7. super(ctx);
  8. }
  9. /**
  10. * 测试 get
  11. * @interface /test/get
  12. * @method GET
  13. * @returns {Object}
  14. * @example
  15. */
  16. async testGet(ctx) {
  17. try {
  18. let query = ctx.query;
  19. // cookie 检测
  20. ctx.cookies.set('_uid', 12, {
  21. expires: yapi.commons.expireDate(7),
  22. httpOnly: true
  23. });
  24. ctx.body = yapi.commons.resReturn(query);
  25. } catch (e) {
  26. ctx.body = yapi.commons.resReturn(null, 402, e.message);
  27. }
  28. }
  29. /**
  30. * 测试 code
  31. * @interface /http/code
  32. * @method GET
  33. * @returns {Object}
  34. * @example
  35. */
  36. async testHttpCode(ctx) {
  37. try {
  38. let params = ctx.request.body;
  39. ctx.status = +ctx.query.code || 200;
  40. ctx.body = yapi.commons.resReturn(params);
  41. } catch(e) {
  42. ctx.body = yapi.commons.resReturn(null, 402, e.message);
  43. }
  44. }
  45. /**
  46. * 测试 post
  47. * @interface /test/post
  48. * @method POST
  49. * @returns {Object}
  50. * @example
  51. */
  52. async testPost(ctx) {
  53. try {
  54. let params = ctx.request.body;
  55. ctx.body = yapi.commons.resReturn(params);
  56. } catch (e) {
  57. ctx.body = yapi.commons.resReturn(null, 402, e.message);
  58. }
  59. }
  60. /**
  61. * 测试 单文件上传
  62. * @interface /test/single/upload
  63. * @method POST
  64. * @returns {Object}
  65. * @example
  66. */
  67. async testSingleUpload(ctx) {
  68. try {
  69. // let params = ctx.request.body;
  70. let req = ctx.req;
  71. let chunks = [],
  72. size = 0;
  73. req.on('data', function(chunk) {
  74. chunks.push(chunk);
  75. size += chunk.length;
  76. });
  77. req.on('finish', function() {
  78. console.log(34343);
  79. });
  80. req.on('end', function() {
  81. let data = new Buffer(size);
  82. for (let i = 0, pos = 0, l = chunks.length; i < l; i++) {
  83. let chunk = chunks[i];
  84. chunk.copy(data, pos);
  85. pos += chunk.length;
  86. }
  87. fs.writeFileSync(path.join(yapi.WEBROOT_RUNTIME, 'test.text'), data, function(err) {
  88. return (ctx.body = yapi.commons.resReturn(null, 402, '写入失败'));
  89. });
  90. });
  91. ctx.body = yapi.commons.resReturn({ res: '上传成功' });
  92. } catch (e) {
  93. ctx.body = yapi.commons.resReturn(null, 402, e.message);
  94. }
  95. }
  96. /**
  97. * 测试 文件上传
  98. * @interface /test/files/upload
  99. * @method POST
  100. * @returns {Object}
  101. * @example
  102. */
  103. async testFilesUpload(ctx) {
  104. try {
  105. let file = ctx.request.body.files.file;
  106. let newPath = path.join(yapi.WEBROOT_RUNTIME, 'test.text');
  107. fs.renameSync(file.path, newPath);
  108. ctx.body = yapi.commons.resReturn({ res: '上传成功' });
  109. } catch (e) {
  110. ctx.body = yapi.commons.resReturn(null, 402, e.message);
  111. }
  112. }
  113. /**
  114. * 测试 put
  115. * @interface /test/put
  116. * @method PUT
  117. * @returns {Object}
  118. * @example
  119. */
  120. async testPut(ctx) {
  121. try {
  122. let params = ctx.request.body;
  123. ctx.body = yapi.commons.resReturn(params);
  124. } catch (e) {
  125. ctx.body = yapi.commons.resReturn(null, 402, e.message);
  126. }
  127. }
  128. /**
  129. * 测试 delete
  130. * @interface /test/delete
  131. * @method DELETE
  132. * @returns {Object}
  133. * @example
  134. */
  135. async testDelete(ctx) {
  136. try {
  137. let body = ctx.request.body;
  138. ctx.body = yapi.commons.resReturn(body);
  139. } catch (e) {
  140. ctx.body = yapi.commons.resReturn(null, 402, e.message);
  141. }
  142. }
  143. /**
  144. * 测试 head
  145. * @interface /test/head
  146. * @method HEAD
  147. * @returns {Object}
  148. * @example
  149. */
  150. async testHead(ctx) {
  151. try {
  152. let query = ctx.query;
  153. ctx.body = yapi.commons.resReturn(query);
  154. } catch (e) {
  155. ctx.body = yapi.commons.resReturn(null, 402, e.message);
  156. }
  157. }
  158. /**
  159. * 测试 options
  160. * @interface /test/options
  161. * @method OPTIONS
  162. * @returns {Object}
  163. * @example
  164. */
  165. async testOptions(ctx) {
  166. try {
  167. let query = ctx.query;
  168. ctx.body = yapi.commons.resReturn(query);
  169. } catch (e) {
  170. ctx.body = yapi.commons.resReturn(null, 402, e.message);
  171. }
  172. }
  173. /**
  174. * 测试 patch
  175. * @interface /test/patch
  176. * @method PATCH
  177. * @returns {Object}
  178. * @example
  179. */
  180. async testPatch(ctx) {
  181. try {
  182. let params = ctx.request.body;
  183. ctx.body = yapi.commons.resReturn(params);
  184. } catch (e) {
  185. ctx.body = yapi.commons.resReturn(null, 402, e.message);
  186. }
  187. }
  188. /**
  189. * 测试 raw
  190. * @interface /test/raw
  191. * @method POST
  192. * @return {Object}
  193. * @example
  194. */
  195. async testRaw(ctx) {
  196. try {
  197. let params = ctx.request.body;
  198. ctx.body = yapi.commons.resReturn(params);
  199. } catch (e) {
  200. ctx.body = yapi.commons.resReturn(null, 402, e.message);
  201. }
  202. }
  203. /**
  204. * 测试返回值
  205. * @interface /test/response
  206. * @method get
  207. * @return {Object}
  208. * @example
  209. */
  210. async testResponse(ctx) {
  211. try {
  212. // let result = `<div><h2>12222222</h2></div>`;
  213. // let result = `wieieieieiieieie`
  214. let result = { b: '12', c: '23' };
  215. ctx.set('Access-Control-Allow-Origin', '*');
  216. ctx.set('Content-Type', 'text');
  217. console.log(ctx.response);
  218. ctx.body = result;
  219. } catch (e) {
  220. ctx.body = yapi.commons.resReturn(null, 402, e.message);
  221. }
  222. }
  223. }
  224. module.exports = interfaceColController;