Notice
Recent Posts
Recent Comments
Link
«   2025/07   »
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
Tags
more
Archives
Today
Total
관리 메뉴

노우!쑤

[nGrinder]Http POST Call 스크립트 (Groovy) 본문

DevOps

[nGrinder]Http POST Call 스크립트 (Groovy)

no500 2022. 3. 15. 17:20
import static net.grinder.script.Grinder.grinder
import static org.junit.Assert.*
import static org.hamcrest.Matchers.*
import net.grinder.script.GTest
import net.grinder.script.Grinder
import net.grinder.scriptengine.groovy.junit.GrinderRunner
import net.grinder.scriptengine.groovy.junit.annotation.BeforeProcess
import net.grinder.scriptengine.groovy.junit.annotation.BeforeThread
// import static net.grinder.util.GrinderUtils.* // You can use this if you're using nGrinder after 3.2.3
import org.junit.Before
import org.junit.BeforeClass
import org.junit.Test
import org.junit.runner.RunWith
 
import HTTPClient.Codecs
import HTTPClient.NVPair
 
import org.ngrinder.http.HTTPRequest
import org.ngrinder.http.HTTPRequestControl
import org.ngrinder.http.HTTPResponse
import org.ngrinder.http.cookie.Cookie
import org.ngrinder.http.cookie.CookieManager
 
 import java.util.UUID
 
/**
* A simple example using the HTTP plugin that shows the retrieval of a single page via HTTP.
*
* This script is automatically generated by ngrinder.
*
* @author admin
*/
@RunWith(GrinderRunner)
class TestRunner {
 
    public static GTest test
    public static HTTPRequest request
    public static Map<String, String> headers = [:]
    public static Map<String, Object> params = [:]
    public static List<Cookie> cookies = []
 
    @BeforeProcess
    public static void beforeProcess() {
        HTTPRequestControl.setConnectionTimeout(300000)
        test = new GTest(1, "[URL]")
        request = new HTTPRequest()
        grinder.logger.info("before process.")
    }
 
    @BeforeThread
    public void beforeThread() {
        test.record(this, "test")
        grinder.statistics.delayReports = true
        grinder.logger.info("before thread.")
    }
 
    @Before
    public void before() {
        request.setHeaders(headers)
        CookieManager.addCookies(cookies)
        grinder.logger.info("before. init headers and cookies")
    }
     
    @Test
    public void test() {
        //def reqBody = '{"date": "'+new Date().toString().format("yyyyMMdd-HH:mm:ss.SSS", TimeZone.getTimeZone('UTC'))+'"}'
        def reqBody = '{"date": "2022-03-15"}'
        List<NVPair> headerList = new ArrayList<NVPair>()
        headerList.add(new NVPair("Content-Type", "application/json"))
        headerList.add(new NVPair("Authorization", "ApiKey VGVrTmpIOEI0c2MxUWhLd0hZZW46bmFVYXQyTHZUQS1pOXVPNE1pRTZ5Zw=="))
        NVPair[] headers = headerList.toArray()
        request.setHeaders(headers)
 
 	//${UUID.randomUUID()}
        HTTPResponse response = request.POST("[URL]", reqBody.getBytes())
 
        if (response.statusCode == 301 || response.statusCode == 302) {
            grinder.logger.warn("Warning. The response may not be correct. The response code was {}.", response.statusCode)
        } else {
            assertThat(response.statusCode, is(201))
        }
    }
}

'DevOps' 카테고리의 다른 글

[Elastic Agent] Air-Gapped 환경  (0) 2022.03.23
[CentOS]인증서 등록  (0) 2022.03.18
[ElasticSearch]8.0 Metricbeat  (0) 2022.03.11
[ElasticSearch]8.0 키바나 설치  (0) 2022.03.11
[ElasticSearch]8.0  (0) 2022.03.11