Skip to content

Instantly share code, notes, and snippets.

@zeraf29
Created February 9, 2021 01:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeraf29/9e2eb285817ea2a4fe805c40c9f82aa8 to your computer and use it in GitHub Desktop.
Save zeraf29/9e2eb285817ea2a4fe805c40c9f82aa8 to your computer and use it in GitHub Desktop.
This is Grinder's test script. I edited on basic script for my needs. You can see normal basic script on nGrinder program.
import static net.grinder.script.Grinder.grinder
import static org.junit.Assert.*
import static org.hamcrest.Matchers.*
import net.grinder.plugin.http.HTTPRequest
import net.grinder.plugin.http.HTTPPluginControl
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 java.util.Date
import java.util.List
import java.util.ArrayList
import HTTPClient.Cookie
import HTTPClient.CookieModule
import HTTPClient.HTTPResponse
import HTTPClient.NVPair
/**
* 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 NVPair[] headers = []
public static NVPair[] params = []
public static Cookie[] cookies = []
public static String userValue
//method for making random value
public static String setRandomUserValue(String before="", int size){
def generator = { String alphabet, int n ->
new Random().with {
(1..n).collect { alphabet[ nextInt( alphabet.length() ) ] }.join()
}
}
return generator( (('A'..'Z')+('0'..'9')).join(), size )
}
@BeforeProcess
public static void beforeProcess() {
//HTTPPluginControl.getConnectionDefaults().timeout = 6000
//set timeout value. if you need more wait time from your system, set time here.
HTTPPluginControl.getConnectionDefaults().setTimeout(30000)
test = new GTest(1, "aaa.yoursytem.com")
request = new HTTPRequest()
// Set header datas
List<NVPair> headerList = new ArrayList<NVPair>()
//set request value for Header
headerList.add(new NVPair("Content-Type", "application/x-www-form-urlencoded"))
//headerList.add(new NVPair("token", "aaaaaa"))
headers = headerList.toArray()
// Set param datas
List<NVPair> paramList = new ArrayList<NVPair>()
//paramList.add(new NVPair("aKey", setRandomUserValue("",15)))
//paramList.add(new NVPair("bKey", setRandomUserValue("TEST",8)))
params = paramList.toArray()
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)
cookies.each { CookieModule.addCookie(it, HTTPPluginControl.getThreadHTTPClientContext()) }
grinder.logger.info("before. init headers and cookies");
}
@Test
public void test(){
HTTPResponse result = request.GET("https://aaa.yoursytem.com/bbb", params)
if (result.statusCode == 301 || result.statusCode == 302) {
grinder.logger.warn("Warning. The response may not be correct. The response code was {}.", result.statusCode);
} else {
assertThat(result.statusCode, is(200));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment