728x90
엔그라인더로 파일 전송 테스트를 하거나 multipart-form 형식 요청의 스크립트 작성 방법
script
import groovy.json.JsonBuilder
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.NVPair
import org.ngrinder.http.HTTPRequest
import org.ngrinder.http.HTTPRequestControl
import org.ngrinder.http.HTTPResponse
import HTTPClient.Codecs
import HTTPClient.NVPair
import org.ngrinder.http.cookie.Cookie
import org.ngrinder.http.cookie.CookieManager
/**
* 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 List<Cookie> cookies = []
public static NVPair[] headers = []
@BeforeProcess
public static void beforeProcess() {
HTTPRequestControl.setConnectionTimeout(30000)
test = new GTest(1, "localhost:8810")
request = new HTTPRequest()
headers = [new NVPair("Authorization", "key")]
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: Initialized headers and cookies")
}
@Test
public void test() {
String boundary = "--------------------------" + UUID.randomUUID().toString().replace("-", "")
String stringData =new JsonBuilder([
markerId: "marker01",
content: "경기도 고양시"
]).toString()
def file = new File("/var/local/images/KakaoTalk_20231012_015153064.jpg");
String fileContent = file.bytes.encodeBase64().toString()
String body = "--" + boundary + "\r\n" +
//-----------파일부분
"Content-Disposition: form-data; name=\"images\"; filename=\"${file.name}\"\r\n" +
"Content-Type: image/jpeg\r\n\r\n" +
fileContent + "\r\n" +
"--" + boundary + "\r\n" +
//-----------바디 부분
"Content-Disposition: form-data; name=\"request\"\r\n" +
"Content-Type: application/json\r\n\r\n" +
+ "\r\n" +
"--" + boundary + "--"
headers = [new NVPair("Content-Type", "multipart/form-data; boundary=" + boundary),new NVPair("Authorization", "key")]
request.setHeaders(headers)
HTTPResponse response = request.POST("http://localhost:8080/api/abc",body.bytes)
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)) // 200 상태 코드 확인
}
}
}
header
headers = [new NVPair("Content-Type", "multipart/form-data; boundary=" + boundary),new NVPair("Authorization", "key")]
request.setHeaders(headers)
헤더 순서를 잘 지켜서 넣어준다
boundary 는 각요청마다 다른 요청임을 식별해주기 위해 사용한다
body
String boundary = "--------------------------" + UUID.randomUUID().toString().replace("-", "")
String stringData =new JsonBuilder([
markerId: "marker01",
content: "경기도 고양시"
]).toString()
def file = new File("/var/local/images/KakaoTalk_20231012_015153064.jpg");
String fileContent = file.bytes.encodeBase64().toString()
String body = "--" + boundary + "\r\n" +
//-----------파일부분
"Content-Disposition: form-data; name=\"images\"; filename=\"${file.name}\"\r\n" +
"Content-Type: image/jpeg\r\n\r\n" +
fileContent + "\r\n" +
"--" + boundary + "\r\n" +
//-----------바디 부분
"Content-Disposition: form-data; name=\"request\"\r\n" +
"Content-Type: application/json\r\n\r\n" +
+ "\r\n" +
"--" + boundary + "--"
HTTPResponse response = request.POST("http://localhost:8080/api/abc",body.bytes)
body 는 param 값으로 서버 (Controller)에 전달된다.
서버에서도 param으로 받아야 파싱이 된다.
@PostMapping("/customer/comment", consumes = ["multipart/form-data"])
fun register(
@RequestParam request: RequestDto,
@RequestParam(value = "audio", required = false) audio : MultipartFile?,
): ~~~ {
return service.register(request,audio)
}
728x90
'개-발 > Infra' 카테고리의 다른 글
[nGrinder] 내 서버는 어느정도 까지 버틸까 (stress Test) (0) | 2024.11.21 |
---|---|
[Infra] ngrinder 부하 테스트 도구 (1) | 2024.11.18 |
[Infra] Jmeter 부하 테스트 도구 (1) | 2024.11.18 |
[오류노트] Swagger 포트 바인딩 (with. Nginx) (0) | 2024.10.31 |
[AWS] swap 메모리 설정하기 (0) | 2024.10.21 |