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