mirror of
https://github.com/YMFE/yapi.git
synced 2025-03-01 14:05:44 +08:00
fix: 高级mock 设置多个 cookie ,后面覆盖前面的bug
This commit is contained in:
parent
60a2e3f623
commit
70abd4ec7e
@ -9,9 +9,11 @@ const lib = require(path.resolve(yapi.WEBROOT, 'common/lib.js' ));
|
||||
const Mock = require('mockjs');
|
||||
|
||||
function arrToObj(arr){
|
||||
let obj = {};
|
||||
let obj = {'Set-Cookie': []};
|
||||
arr.forEach(item=>{
|
||||
obj[item.name] = item.value;
|
||||
if(item.name === 'Set-Cookie'){
|
||||
obj['Set-Cookie'].push(item.value);
|
||||
}else obj[item.name] = item.value;
|
||||
})
|
||||
return obj;
|
||||
}
|
||||
@ -59,7 +61,6 @@ module.exports = function(){
|
||||
}).select('_id params')
|
||||
list.forEach(item=>{
|
||||
let params = item.params;
|
||||
console.log(reqParams, item.params)
|
||||
if(lib.isDeepMatch(reqParams, item.params)){
|
||||
matchList.push(item);
|
||||
}
|
||||
|
@ -26,6 +26,17 @@ function matchApi(apiPath, apiRule) {
|
||||
return true;
|
||||
}
|
||||
|
||||
function parseCookie(str){
|
||||
if(!str || typeof str !== 'string'){
|
||||
return str;
|
||||
}
|
||||
if(str.split(';')[0]){
|
||||
let c = str.split(';')[0].split('=');
|
||||
return {name: c[0], value: c[1] || ''}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function handleCorsRequest(ctx) {
|
||||
let header = ctx.request.header;
|
||||
ctx.set('Access-Control-Allow-Origin', header.origin);
|
||||
@ -169,7 +180,26 @@ module.exports = async (ctx, next) => {
|
||||
await handleMock;
|
||||
if (context.resHeader && typeof context.resHeader === 'object') {
|
||||
for (let i in context.resHeader) {
|
||||
ctx.set(i, context.resHeader[i]);
|
||||
let cookie;
|
||||
if(i === 'Set-Cookie'){
|
||||
if(context.resHeader[i] && typeof context.resHeader[i] === 'string'){
|
||||
cookie = parseCookie(context.resHeader[i]);
|
||||
if(cookie && typeof cookie === 'object'){
|
||||
ctx.cookies.set(cookie.name, cookie.value, {
|
||||
maxAge: 864000000
|
||||
});
|
||||
}
|
||||
}else if(context.resHeader[i] && Array.isArray(context.resHeader[i])){
|
||||
context.resHeader[i].forEach(item=>{
|
||||
cookie = parseCookie(item);
|
||||
if(cookie && typeof cookie === 'object'){
|
||||
ctx.cookies.set(cookie.name, cookie.value, {
|
||||
maxAge: 864000000
|
||||
});
|
||||
}
|
||||
})
|
||||
}
|
||||
}else ctx.set(i, context.resHeader[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user