Skip to content

Instantly share code, notes, and snippets.

@mayjungheelee
Created January 21, 2019 07:51
Show Gist options
  • Save mayjungheelee/97c5901b9d1c3fb895abc7dd70f047a4 to your computer and use it in GitHub Desktop.
Save mayjungheelee/97c5901b9d1c3fb895abc7dd70f047a4 to your computer and use it in GitHub Desktop.
import HTTPClient.Cookie
import HTTPClient.CookieModule
import HTTPClient.HTTPResponse
import HTTPClient.NVPair
import net.grinder.plugin.http.HTTPPluginControl
import net.grinder.plugin.http.HTTPRequest
import net.grinder.script.GTest
import net.grinder.scriptengine.groovy.junit.GrinderRunner
import net.grinder.scriptengine.groovy.junit.annotation.BeforeProcess
import net.grinder.scriptengine.groovy.junit.annotation.BeforeThread
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import static net.grinder.script.Grinder.grinder
import static org.hamcrest.Matchers.is
// import static net.grinder.util.GrinderUtils.* // You can use this if you're using nGrinder after 3.2.3
import static org.junit.Assert.assertThat
@RunWith(GrinderRunner)
class TestRunner {
public static GTest test
public static HTTPRequest request
public static NVPair[] headers = []
public static NVPair[] params = []
public static Cookie[] cookies = []
static String protocol = "https://"
static String host = "shortbread.io"
@BeforeProcess
static void beforeProcess() {
HTTPPluginControl.getConnectionDefaults().timeout = 6000
test = new GTest(1, host)
request = new HTTPRequest()
// Set header datas
List<NVPair> headerList = new ArrayList<NVPair>()
headerList.add(new NVPair("Content-Type", "application/json"))
headers = headerList.toArray()
grinder.logger.info("before process.")
}
@BeforeThread
void beforeThread() {
test.record(this, "test")
grinder.statistics.delayReports=true
grinder.logger.info("before thread.")
}
@Before
void before() {
request.setHeaders(headers)
cookies.each { CookieModule.addCookie(it, HTTPPluginControl.getThreadHTTPClientContext()) }
grinder.logger.info("before. init headers and cookies")
}
@Test
void test(){
HTTPResponse result
result = request.GET(protocol + host, 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))
}
result = request.GET(protocol + host + "/coding-expedition.html", 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))
}
result = request.GET(protocol + host + "/experience.html", 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))
}
result = request.GET(protocol + host + "/static/semantic.min.js", 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))
}
result = request.POST(protocol + host + "/static/themes/default/assets/fonts/",
"""{
"outline-icons": "무엇을얼마나 넣어볼까요"
}""".getBytes("UTF-8"))
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