通过JSON替换的方式实现app返回数据MOCK

来源:互联网 发布:科多大数据上海 编辑:程序博客网 时间:2024/06/10 19:28

本地MOCK

背景

在app的测试时,经常需要对返回的字段做替换。通常我们采用的方案是通过fiddler/charles抓包,然后替换请求。但是,由于修改了网络库,无法再继续抓包,所以产生了这种需求:对收到的请求进行替换。

方案

在收取请求后,在解析JSONObject 之前,进行数据的修改。修改完成后再进行正常的数据处理。


方案评估

方案 优点 缺点使用切面 程序结构和逻辑更加清晰. 可以深入到更为底层的模块, 所以扩展性更好 技术可行性待评估, 会引入额外jar包, 如果遇到未知问题难以调试直接实现 实现相对简明直接. 满足现在的测试需要. 代码比较丑陋,需要借助宏. 只能在baseresponse这一层拦截.动态代理 http://www.cnblogs.com/android-html5/archive/2012/03/11/2533680.html 

从实现的简单程度上,直接实现是最省事的。so,先做。。。

数据替换

定义规则:

key = value 表示将某个key的值替换成value.其中key可能是个简单的key,也可能是个JSONObject/JSONArray key = DEL 表示删除某个key key = ADD(value) 表示在key的对象内部增加某个object.

具体实现 替换和删除

添加内容还没做,用key=value就能实现,不做了

public class SingleRule {    public String key;    public String value;    public int status = -1;//0, change,1 del,2,add; -1 means not match    Pattern change = Pattern.compile("^[\\s]*([^=]+)=[\\s]*((?!DEL|ADD)[^\\s]+)[\\s]*");    Pattern del = Pattern.compile("^[\\s]*([^=]+)=[\\s]*DEL[\\s]*");    public SingleRule(String schema) {        Matcher matcherChange = change.matcher(schema);        Matcher matcherDel = del.matcher(schema);        if(matcherChange.matches()){            key = matcherChange.group(1).trim();            value = matcherChange.group(2).trim();            status = 0;        }        if(matcherDel.matches()){            key = matcherDel.group(1).trim();            status = 1;        }    }    public static String changeString(String orig,String targetKey,String value,boolean change){        String desc = orig;        try {            JSONObject obj = JSON.parseObject(orig);            desc = getValue(obj, "", targetKey, value, change).toString();        } catch(Exception e){            e.printStackTrace();        }        return desc;    }    public static Object getValue(Object objValue,String curKey,String targetKey,String value,boolean change) {        if(objValue instanceof JSONArray){            if(curKey.equals(targetKey)){                if(change) {                    return JSON.parseArray(value);                } else                    return null;            }            int size = ((List<Object>)objValue).size();            for(int i=size-1;i>=0;i--){                Object obj = ((List<Object>)objValue).get(i);                Object resp = getValue(obj, curKey, targetKey, value, change);                if(resp == null)                    ((List<Object>)objValue).remove(i);                else                    ((List<Object>)objValue).set(i, resp);            }        } else if(objValue instanceof JSONObject) {            if(curKey.equals(targetKey)){                if(change) {                    return JSON.parseObject(value);                } else                    return null;            }            Set<String> keys = new HashSet<String>(((JSONObject)objValue).keySet());            for(String key: keys){                Object obj = ((Map)objValue).get(key);                Object resp = getValue(obj, key, targetKey, value, change);                ((Map)objValue).remove(key);                if(resp != null) {                    ((Map) objValue).put(key,resp);                }            }        } else {            if(curKey.equals(targetKey)){                if(change) {                    return value;                } else                    return null;            }        }        return objValue;    }    @Override    public String toString() {        return "SingleRule{" +            "key='" + key + '\'' +            ", value='" + value + '\'' +            ", status=" + status +            '}';    }    static String PATH = "/Users/army/Documents/tmall/tem.txt";    public static void main( String[] args ) throws Exception    {    String str = readfile(PATH);    System.out.println("orig:" + str);    SingleRule rule = new SingleRule("suggestList=[\"a]");    System.out.println(rule);    String newstr =SingleRule.changeString(str,rule.key,rule.value,true);    System.out.println("r:" + newstr);    }    public static String readfile(String path) throws IOException {        BufferedReader br = new BufferedReader(new FileReader(path));        String data = br.readLine();//一次读入一行,直到读入null为文件结束        StringBuffer strBuf = new StringBuffer();        while( data!=null){            strBuf.append(data);            data = br.readLine(); //接着读下一行        }        return strBuf.toString();    }}

0 0
原创粉丝点击