端点的健康检查 - 快速和脏版本

时间:2015-11-03 16:21:43

标签: rest soap groovy soapui postman

我有一些REST端点和少量[asmx / svc]端点。 其中一些是GET,其他是POST操作。

我正在尝试整理一个快速,脏,可重复的健康检查序列,以查找所有端点是否响应或是否有任何故障。 基本上要么获得200或201,否则报告错误。

最简单的方法是什么?

1 个答案:

答案 0 :(得分:0)

SOAPUI使用内部apache http-client 4.1.1版本,您可以在groovy脚本testStep中使用它来执行检查。

将一个groovy脚本testStep添加到你的testCase里面,使用以下代码;它基本上尝试对URL列表执行GET,如果它返回http-status 200201它被认为是有效的,如果返回http-status 405(Method not allowed)那么它尝试使用POST并执行相同的状态代码检查,否则将其视为已关闭。

请注意,某些服务可以正在运行但是如果请求不正确则可以返回例如400(BAD请求),因此请考虑是否需要重新考虑执行检查的方式或添加一些其他状态代码以考虑服务器是否正常运行。

import org.apache.http.HttpEntity
import org.apache.http.HttpResponse
import org.apache.http.client.methods.HttpGet
import org.apache.http.client.methods.HttpPost
import org.apache.http.impl.client.DefaultHttpClient

// urls to check
def urls = ['http://google.es','http://stackoverflow.com']

// apache http-client to use in closue
DefaultHttpClient httpclient = new DefaultHttpClient()

// util function to get the response
def getStatus = {  httpMethod ->
    HttpResponse response = httpclient.execute(httpMethod)
    // consume the entity to avoid error with http-client
    if(response.getEntity() != null) {
        response.getEntity().consumeContent();
    }

    return response.getStatusLine().getStatusCode()
}

HttpGet httpget;
HttpPost httppost;

// finAll urls that are working
def urlsWorking =  urls.findAll { url ->
    log.info "try GET for $url"
    httpget = new HttpGet(url)
    def status = getStatus(httpget) 

    // if status are 200 or 201 it's correct
    if(status in [200,201]){
        log.info "$url is OK"
        return true
     // if GET is not allowed try with POST
    }else if(status == 405){
        log.info "try POST for $url"
        httppost = new HttpPost(url)
        status = getStatus(httpget) 

        // if status are 200 or 201 it's correct
        if(status in [200,201]){
            log.info "$url is OK"
            return true
        }

        log.info "$url is NOT working status code: $status"
        return false
    }else{
        log.info "$url is NOT working status code: $status"
        return false
    }
}

// close connection to release resources
httpclient.getConnectionManager().shutdown()

log.info "URLS WORKING:" + urlsWorking

此脚本记录:

Tue Nov 03 22:37:59 CET 2015:INFO:try GET for http://google.es
Tue Nov 03 22:38:00 CET 2015:INFO:http://google.es is OK
Tue Nov 03 22:38:00 CET 2015:INFO:try GET for http://stackoverflow.com
Tue Nov 03 22:38:03 CET 2015:INFO:http://stackoverflow.com is OK
Tue Nov 03 22:38:03 CET 2015:INFO:URLS WORKING:[http://google.es, http://stackoverflow.com]

希望它有所帮助,

相关问题