-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite_post.groovy
92 lines (79 loc) · 3.11 KB
/
write_post.groovy
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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 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 groovy.json.JsonOutput
/**
* 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, "172.16.213.23")
request = new HTTPRequest()
// Set header data
headers.put("Content-Type", "application/json")
headers.put("Authorization", "Bearer eyJ0eXBlIjoiand0IiwiYWxnIjoiSFM1MTIifQ.eyJzdWIiOiJuYW1zaDExMjUxQGdtYWlsLmNvbSIsImF1dGgiOiJST0xFX1VTRVIiLCJtZW1iZXJJZHgiOiI2ZmIxNjNlNS1kNWI5LTQ4OTYtOTA2Mi05NGQ0NGI2ZmFiMGIiLCJpYXQiOjE2OTk2OTIxODgsImV4cCI6MTcwMDI5Njk4OH0.2UJvJ5iAQ41_hVzbPF6EHRLXbm4HisAyC9dINdNXX5CwV3TXscIUgDeRNBHS9jyzvLxHYhz2jF_y9xDygDQ_nw")
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() {
// Set parameters (DTO)
params.put("title", "test title")
params.put("content", "test content")
params.put("summary", "test summary")
params.put("thumbnailLink", "https://d2u3dcdbebyaiu.cloudfront.net/uploads/atch_img/78/82e9c2696d34b752ad2311110afba6ef_res.jpeg")
params.put("isPublic", "true")
params.put("categoryId", "1")
params.put("hashtagList", ["plzplz"])
params.put("topicId", "1")
// Convert params to JSON string
String jsonParams = JsonOutput.toJson(params)
// Convert JSON string to byte array
byte[] byteParams = jsonParams.getBytes("UTF-8")
// Send POST request
HTTPResponse response = request.POST("http://172.16.213.23:8080/posts", byteParams)
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(200))
}
}
}