【问题标题】:Healthcheck for endpoints - quick and dirty version端点的健康检查 - 快速而肮脏的版本
【发布时间】:2016-02-03 21:35:08
【问题描述】:

我有几个 REST 端点和几个 [asmx/svc] 端点。 其中一些是 GET,另一些是 POST 操作。

我正在尝试组合一个快速、肮脏、可重复的健康检查序列,以查找所有端点是否都响应或是否有任何关闭。 基本上要么得到 200 或 201,否则报告错误。

最简单的方法是什么?

【问题讨论】:

    标签: rest soap groovy soapui postman


    【解决方案1】:

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

    在你的 testCase 中添加一个 groovy 脚本 testStep 并在里面使用下面的代码;它基本上尝试对 URL 列表执行 GET,如果它返回 http-status 200201 它认为它正在工作,如果返回 http-status 405(方法不允许)然后它尝试使用 POST 和执行相同的状态码检查,否则视为关闭。

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

    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]
    

    希望对你有帮助,

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-08
      • 1970-01-01
      • 1970-01-01
      • 2012-12-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多