Skip to content

Commit

Permalink
add customized JSONP callback name grammer
Browse files Browse the repository at this point in the history
  • Loading branch information
Bosn committed Mar 24, 2014
1 parent ed0856e commit 7cb5f0c
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 40 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,17 @@ RAP.getMode();
```bash
RAP.setMode(1);
```

接口文档进阶
--------------------------------------

### 接口文档请求链接语法

http://www.taobao.com/getItem?[callback]=foo

`[callback]` 表示参数`callback`会用作JSONP的回调key,若实际请求为getItem?callback=foo,则返回结果为:foo({...});

http://www.taobao.com/getREST?{path}=delete

`{path}` 表示参数`path`会在RAP文档和开发环境进行接口对接时,需要考虑path参数,一般REST API会使用比较多。因为REST不同请求是通过参数区分的,比如getRest?path=delete, getRest?path=update, 而{path}表示两者为不同的接口。

1 change: 1 addition & 0 deletions UPDATELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
### rap v0.8.1 ###
* [功能] 请求链接添加新语法url?[callback]=foo,用以支持自定义的JSONP callback key。 2014-03-24
* [功能] 请求链接添加新语法url?{action}=update,用以支持REST_API的带参数查询接口的功能。 2014-03-20
* [BUG] 修复pageTester控制台请求链接带参数时的请求路径错误。 2014-03-20
* [BUG] 修复@mock空标签导致MOCK异常的问题。 2014-03-20
Expand Down
2 changes: 1 addition & 1 deletion WebContent/demo/mock.plugin/index.htm
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<!-- <script type="text/javascript"
src="http://rap.alibaba-inc.com/rap.plugin.js?projectId=79"></script> -->
<script type="text/javascript"
src="http://etaoux-bj.taobao.ali.com:8080/rap.plugin.js?projectId=79&mode=3"></script>
src="http://rap.alibaba-inc.com/rap.plugin.js?projectId=79&mode=0"></script>
<!-- 引入RAP结束 -->
</head>
<body>
Expand Down
36 changes: 24 additions & 12 deletions src/com/taobao/rigel/rap/mock/service/MockMgr.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
package com.taobao.rigel.rap.mock.service;

import java.io.UnsupportedEncodingException;
import java.util.Map;

public interface MockMgr {
/**
* automatically generate testing data
*
* @param projectId
* @param pattern
* @param options
* @return JSON String
* @throws UnsupportedEncodingException
* @throws UnsupportedEncodingException
*/
public String generateData(int projectId, String pattern) throws UnsupportedEncodingException;
public String generateData(int projectId, String pattern,
Map<String, Object> options) throws UnsupportedEncodingException;

/**
* modify mock data of parameters based on mockData
* @param actionId identifier of the action to be modified
* @param mockData mock data string
* example: response.param1.subParam=@format=x.xxxx_AND_response.
* param2=@value=1_AND_response.param3.subParam.subSubParam=@length=7
*
* @param actionId
* identifier of the action to be modified
* @param mockData
* mock data string example:
* response.param1.subParam=@format=x.xxxx_AND_response.
* param2=@value=
* 1_AND_response.param3.subParam.subSubParam=@length=7
* @return number of rows affected
*
*/
public int modify(int actionId, String mockData);

/**
* clear all mock data of object in specified projet
* clear all mock data of object in specified project
*
* @param projectId
* @return number of rows affected
*/
Expand All @@ -36,18 +44,22 @@ public interface MockMgr {
*
* @param id
* @param pattern
* @param options
* @return
* @throws UnsupportedEncodingException
* @throws UnsupportedEncodingException
*/
public String generateRule(int id, String pattern) throws UnsupportedEncodingException;

public String generateRule(int id, String pattern,
Map<String, Object> options) throws UnsupportedEncodingException;

/**
* generate mockjs data
*
* @param id
* @param pattern
* @param options
* @return
* @throws UnsupportedEncodingException
* @throws UnsupportedEncodingException
*/
public String generateRuleData(int id, String pattern) throws UnsupportedEncodingException;
public String generateRuleData(int id, String pattern,
Map<String, Object> options) throws UnsupportedEncodingException;
}
32 changes: 21 additions & 11 deletions src/com/taobao/rigel/rap/mock/service/impl/MockMgrImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public void setProjectDao(ProjectDao projectDao) {
}

@Override
public String generateData(int projectId, String pattern) throws UnsupportedEncodingException {
public String generateData(int projectId, String pattern,
Map<String, Object> options) throws UnsupportedEncodingException {
_num = 1;
String originalPattern = pattern;
System.out.println("pattern before processed:" + pattern);
Expand All @@ -69,8 +70,8 @@ public String generateData(int projectId, String pattern) throws UnsupportedEnco
.getMatchedActionList(projectId, pattern);
if (aList.size() == 0)
return "{\"isOk\":false, \"errMsg\":\"no matched action\"}";
Action action = actionPick(aList, originalPattern);

Action action = actionPick(aList, originalPattern, options);

String desc = action.getDescription();
Set<Parameter> pList = action.getResponseParameterList();
Expand Down Expand Up @@ -130,15 +131,15 @@ public String generateData(int projectId, String pattern) throws UnsupportedEnco
}

@Override
public String generateRuleData(int projectId, String pattern)
throws UnsupportedEncodingException {
String result = generateRule(projectId, pattern);
public String generateRuleData(int projectId, String pattern,
Map<String, Object> options) throws UnsupportedEncodingException {
String result = generateRule(projectId, pattern, options);
return MockjsRunner.renderMockjsRule(result);
}

@Override
public String generateRule(int projectId, String pattern)
throws UnsupportedEncodingException {
public String generateRule(int projectId, String pattern,
Map<String, Object> options) throws UnsupportedEncodingException {
String originalPattern = pattern;
_num = 1;
System.out.println("pattern before processed:" + pattern);
Expand All @@ -155,7 +156,7 @@ public String generateRule(int projectId, String pattern)
return "{\"isOk\":false, \"errMsg\":\"no matched action\"}";
}

Action action = actionPick(aList, originalPattern);
Action action = actionPick(aList, originalPattern, options);

String desc = action.getDescription();
Set<Parameter> pList = action.getResponseParameterList();
Expand Down Expand Up @@ -214,8 +215,8 @@ public String generateRule(int projectId, String pattern)
return resultFilter(result);
}

private Action actionPick(List<Action> actionList, String pattern)
throws UnsupportedEncodingException {
private Action actionPick(List<Action> actionList, String pattern,
Map<String, Object> options) throws UnsupportedEncodingException {
Action result = actionList.get(0);
Map<String, List<String>> requestParams = getUrlParameters(pattern);
for (Action action : actionList) {
Expand All @@ -224,6 +225,15 @@ private Action actionPick(List<Action> actionList, String pattern)
boolean hasSchema = false;
boolean isPassed = true;
for (String docParamKey : docActionParams.keySet()) {
if (docParamKey.contains("[") && docParamKey.contains("]")) {
String docParamKeyProcessed = docParamKey.substring(1,
docParamKey.length() - 1);
List<String> list = requestParams.get(docParamKeyProcessed);
if (list != null && list.size() > 0) {
options.put("callback", list.get(0));
}
}

if (docParamKey.contains("{") && docParamKey.contains("}")) {
hasSchema = true;
String docParamKeyProcessed = docParamKey.substring(1,
Expand Down
55 changes: 39 additions & 16 deletions src/com/taobao/rigel/rap/mock/web/action/MockAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.taobao.rigel.rap.common.ActionBase;
import com.taobao.rigel.rap.mock.service.MockMgr;
Expand Down Expand Up @@ -59,7 +61,8 @@ public String get_c() {
}

public void set_c(String _c) {
this._c = _c;;
this._c = _c;
;
}

public String getCallback() {
Expand Down Expand Up @@ -99,22 +102,23 @@ public void setContent(String content) {
public String getPattern() {
return pattern;
}

private String callbackFilter(String cb) {
if (cb == null) {
return "callback";
}
if (cb.contains("_c=")) {
int startIndex = cb.indexOf("_c=") + 3;
int endIndex = cb.indexOf("&", startIndex);
if (endIndex == -1) endIndex = cb.length();
if (endIndex == -1)
endIndex = cb.length();
cb = cb.substring(startIndex, endIndex);
return cb;
}
if (cb.contains("&")) {
cb = cb.substring(0, cb.indexOf("&"));
}

return cb;
}

Expand All @@ -135,25 +139,38 @@ public void setPattern(String pattern) {
}

public String createData() throws UnsupportedEncodingException {
Map<String, Object> options = new HashMap<String, Object>();
String _c = get_c();
String result = mockMgr.generateData(id, pattern, options);
if (options.get("callback") != null) {
_c = (String) options.get("callback");
callback = (String) options.get("callback");
}

if (callback != null && !callback.isEmpty()) {
setContent(callback + "(" + mockMgr.generateData(id, pattern) + ")");
setContent(callback + "(" + result + ")");
} else if (_c != null && !_c.isEmpty()) {
setContent(_c + "(" + mockMgr.generateData(id, pattern) + ")");
setContent(_c + "(" + result + ")");
} else {
setContent(mockMgr.generateData(id, pattern));
setContent(result);
}
return SUCCESS;
}

public String createRule() throws UnsupportedEncodingException {
Map<String, Object> options = new HashMap<String, Object>();
String _c = get_c();
String result = mockMgr.generateRule(id, pattern, options);
if (options.get("callback") != null) {
_c = (String) options.get("callback");
callback = (String) options.get("callback");
}
if (callback != null && !callback.isEmpty()) {
setContent(callback + "(" + mockMgr.generateRule(id, pattern) + ")");
setContent(callback + "(" + result + ")");
} else if (_c != null && !_c.isEmpty()) {
setContent(_c + "(" + mockMgr.generateRule(id, pattern) + ")");
setContent(_c + "(" + result + ")");
} else {
setContent(mockMgr.generateRule(id, pattern));
setContent(result);
}
return SUCCESS;
}
Expand All @@ -167,7 +184,7 @@ public String reset() {
setNum(mockMgr.reset(projectId));
return SUCCESS;
}

public String createPluginScript() {
List<String> list = new ArrayList<String>();
Project p = projectMgr.getProject(projectId);
Expand All @@ -183,15 +200,21 @@ public String createPluginScript() {
urlList = list;
return SUCCESS;
}

public String createMockjsData() throws UnsupportedEncodingException {
String _c = get_c();
Map<String, Object> options = new HashMap<String, Object>();
String result = mockMgr.generateRuleData(id, pattern, options);
if (options.get("callback") != null) {
_c = (String) options.get("callback");
callback = (String) options.get("callback");
}
if (callback != null && !callback.isEmpty()) {
setContent(callback + "(" + mockMgr.generateRuleData(id, pattern) + ")");
setContent(callback + "(" + result + ")");
} else if (_c != null && !_c.isEmpty()) {
setContent(_c + "(" + mockMgr.generateRuleData(id, pattern) + ")");
setContent(_c + "(" + result + ")");
} else {
setContent(mockMgr.generateRuleData(id, pattern));
setContent(result);
}
return SUCCESS;
}
Expand Down

0 comments on commit 7cb5f0c

Please sign in to comment.