How to Create a Mock Echo Soap/REST API using SoapUI?

  • 时间:2020-09-18 17:26:09
  • 分类:网络文摘
  • 阅读:142 次

Modern Applications Development often requires testing APIs. If you don’t have backend API developed, you can create Mock API services using SoapUI. You probably can do the same thing using PostMan, however which may require a premium subscription.

Creating Mocking API services help the Test Driven Development (TDD) where you can build the client first without worrying about the actual implementation of the server API.

Echo APIs are good. They are simple, and easy to implement. They will echo whatever is given (as parameter or the request body). For example, the API: https://helloacm.com/api/echo/?s=Hello! will echo a string Hello.

Creating a Mock Echo Get/Post API using SoapUI

Create a MockSerivce (either Rest of Soap), then add an Action. And then add a Mock Response.

In the response, click the script tab, and put the following to retrieve the querystring parameters of the Mock Request.

1
2
3
def paramValue = mockRequest.getRequest().getParameter("value")
context.paramValue = paramValue
log.info "context.paramValue:" + context.paramValue
def paramValue = mockRequest.getRequest().getParameter("value")
context.paramValue = paramValue
log.info "context.paramValue:" + context.paramValue
soapui-create-an-echo-mock-rest-api-get How to Create a Mock Echo Soap/REST API using SoapUI? API

soapui-create-an-echo-mock-rest-api-get

The POST API allows you to retrieve the request Body. Copy over the below code in the script field of the Post API Response.

1
2
def requestBody = mockRequest.getRequestContent()
context.requestBody = requestBody
def requestBody = mockRequest.getRequestContent()
context.requestBody = requestBody

With requestBody and paramValue variables defined in the script, we can use the dynamic values in the mock responses.

soapui-create-an-echo-mock-rest-api-post-get-requestcontent-requestbody How to Create a Mock Echo Soap/REST API using SoapUI? API

soapui-create-an-echo-mock-rest-api-post-get-requestcontent-requestbody

Once the Echo APIs are defined, we can start the APIs by clicking the green arrow button. The APIs will be running on port 8080.

soapui-start-rest-mock-service-echo How to Create a Mock Echo Soap/REST API using SoapUI? API

soapui-start-rest-mock-service-echo

Then, we can use the PostMan to send requests to test the above two Mock Echo APIs.

postman-sends-echo-request-api-via-post How to Create a Mock Echo Soap/REST API using SoapUI? API

postman-sends-echo-request-api-via-post

postman-sends-echo-request-api How to Create a Mock Echo Soap/REST API using SoapUI? API

postman-sends-echo-request-api

The SoapUI allows you to create the dynamic APIs with dynamic responses. Below are some script to show the scripting capabilities.

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
// Script dispatcher is used to select a response based on the incoming request.
// Here are few examples showing how to match based on path, query param, header and body
 
// Match based on path
def requestPath = mockRequest.getPath()
log.info "Path: "+ requestPath
 
if ( requestPath.contains("json") ) {
    // return the name of the response you want to dispatch
    return "JSON Response"
}
 
// Match based on query parameter
def queryString = mockRequest.getRequest().getQueryString()
log.info "QueryString: " + queryString
 
if ( queryString.contains("stockholm") ) {
    // return the name of the response you want to dispatch
    return "Response Stockholm"
}
else if( queryString.contains("london") ) {
    // return the name of the response you want to dispatch
    return "Response London"
}
 
// Match based on header
def acceptEncodingHeaderList = mockRequest.getRequestHeaders().get("Accept-Encoding")
log.info "AcceptEncoding Header List: " + acceptEncodingHeaderList
 
if ( acceptEncodingHeaderList.contains("gzip,deflate") ) {
    // return the name of the response you want to dispatch
    return "GZiped Response"
}
 
 
// Match based on body
def requestBody = mockRequest.getRequestContent()
log.info "Request body: " + requestBody
 
if ( requestBody.contains("some data") ) {
    // return the name of the response you want to dispatch
    return "Response N"
}
// Script dispatcher is used to select a response based on the incoming request.
// Here are few examples showing how to match based on path, query param, header and body

// Match based on path
def requestPath = mockRequest.getPath()
log.info "Path: "+ requestPath

if ( requestPath.contains("json") ) {
    // return the name of the response you want to dispatch
    return "JSON Response"
}

// Match based on query parameter
def queryString = mockRequest.getRequest().getQueryString()
log.info "QueryString: " + queryString

if ( queryString.contains("stockholm") ) {
    // return the name of the response you want to dispatch
    return "Response Stockholm"
}
else if( queryString.contains("london") ) {
    // return the name of the response you want to dispatch
    return "Response London"
}

// Match based on header
def acceptEncodingHeaderList = mockRequest.getRequestHeaders().get("Accept-Encoding")
log.info "AcceptEncoding Header List: " + acceptEncodingHeaderList

if ( acceptEncodingHeaderList.contains("gzip,deflate") ) {
    // return the name of the response you want to dispatch
    return "GZiped Response"
}


// Match based on body
def requestBody = mockRequest.getRequestContent()
log.info "Request body: " + requestBody

if ( requestBody.contains("some data") ) {
    // return the name of the response you want to dispatch
    return "Response N"
}

API Testing Tools Software Download

PostMan: https://www.getpostman.com
SoapUI: https://www.soapui.org

Example SoapUI Project of Mock Echo APIs

The SoapUI project containing the above Echo APIs: GET and POST can be downloaded here: https://helloacm.com/static/soapui-rest-echo.xml

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
中华人民共和国深海海底区域资源勘探开发法(主席令第四十二号)  全国人民代表大会常务委员会关于修改《中华人民共和国人口与计划生育法》的决定(主席令第四十一号)  全国人大常委会关于修改《中华人民共和国高等教育法》的决定(主席令第四十号)  中华人民共和国反家庭暴力法(主席令第三十七号)  全国人大常委会关于修改《中华人民共和国教育法》的决定(主席令第三十九号)  中华人民共和国国家勋章和国家荣誉称号法(主席令第三十八号)  中华人民共和国反恐怖主义法(主席令第三十六号)  地图管理条例(国务院令第664号)   中华人民共和国宪法  全国社会保障基金条例(国务院令第667号)  
评论列表
添加评论