Soap UIでREST APIのモックを作成した時の、Soap UIプロジェクトの内容です。
/apiNameSample01/getqueryservice
Dispatch: SCRIPT
Default Response: default
def projectDir = context.expand('${projectDir}');
log.info "projectDir: "+ projectDir
def workspaceDir = context.expand('${workspaceDir}');
log.info "workspaceDir: "+ workspaceDir
def requestPath = mockRequest.getPath()
log.info "Path: "+ requestPath
def queryString = mockRequest.getRequest().getQueryString()
log.info "QueryString: " + queryString
if( queryString.contains("city=") )
{
def cityCode = queryString.tokenize('=')[-1]
log.info "cityCode: " + cityCode
return cityCode
}
else // default
{
return "default"
}
MockResponse
default , 130010 , 270000, 471010
Http Status Code: 200 – OK
{
"forecasts": [
{
"telop": "晴れ"
}
]
}
888404
Http Status Code: 200 – OK
{
"error": [
{
"message": "都市が存在しません"
}
]
}
999404
Http Status Code: 404 – Not Found
/apiNameSample02/postservice
Dispatch: SCRIPT
Default Response: default
import groovy.json.JsonSlurper;
def projectDir = context.expand('${projectDir}');
log.info "projectDir: "+ projectDir
def workspaceDir = context.expand('${workspaceDir}');
log.info "workspaceDir: "+ workspaceDir
def requestPath = mockRequest.getPath()
log.info "Path: "+ requestPath
def queryString = mockRequest.getRequest().getQueryString()
log.info "QueryString: " + queryString
// Match based on body
def requestBody = mockRequest.getRequestContent()
log.info "Request body: " + requestBody
if( requestBody.contains("city") )
{
// Request message
def reqMsg = new String(requestBody);
def jSlurper = new groovy.json.JsonSlurper().parseText(reqMsg);
def cityCode = "${jSlurper.city}";
return cityCode
}
else // default
{
return "default"
}
MockResponse
default , 130010 , 270000, 471010
Http Status Code: 200 – OK
{
"forecasts": [
{
"telop": "晴れ"
}
]
}
888404
Http Status Code: 200 – OK
{
"error": [
{
"message": "都市が存在しません"
}
]
}
999404
Http Status Code: 404 – Not Found
/apiNameSample03/getpathservice/130010
Dispatch: SEQUENCE
MockResponse
Response
Http Status Code: 200 – OK
{
"forecasts": [
{
"telop": "晴れ"
}
]
}
※以下の mock action についても同様の内容となります。
- /apiNameSample03/getpathservice/270000
- /apiNameSample03/getpathservice/471010
/apiNameSample03/getpathservice/888404
Dispatch: SEQUENCE
MockResponse
Response
Http Status Code: 200 – OK
{
"error": [
{
"message": "都市が存在しません"
}
]
}