multipart/form-data 의 필드 데이터인 한글이 깨집니다.

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

multipart/form-data 의 필드 데이터인 한글이 깨집니다.

rasol
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 groovy.json.JsonOutput;
import java.io.*;

// 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.After
import org.junit.BeforeClass
import net.grinder.scriptengine.groovy.junit.annotation.AfterProcess
import org.junit.Test
import org.junit.runner.RunWith

import java.util.Date
import java.util.List
import java.util.ArrayList
import java.lang.String

import HTTPClient.Cookie
import HTTPClient.CookieModule
import HTTPClient.HTTPResponse
import HTTPClient.NVPair
import HTTPClient.Codecs


/**
* 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 int tc_size = 0
    public static int tc_counter = 0
    //public static String filename = "./resources/nlu.20200620_all.csv"
    public static String filename = "./resources/grxml_file_names.txt"
    public static String inputFilename = "./resources/TC_base_all.txt"
    public static BufferedReader reader;
    public static BufferedReader inputReader;


    @BeforeProcess
    public static void beforeProcess() {
        HTTPPluginControl.getConnectionDefaults().timeout = 6000
        test = new GTest(1, "172.27.107.213")
        request = new HTTPRequest()
        // Set header datas
        List<NVPair> headerList = new ArrayList<NVPair>()
        //headerList.add(new NVPair("Content-Type", "application/json"))
        headerList.add(new NVPair("Content-Type", "application/json;charset=UTF-8"))

        headerList.add(new NVPair("Charset", "UTF-8"))
        headers = headerList.toArray()
        grinder.logger.info("before process.");
       
        reader = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
        inputReader = new BufferedReader(new InputStreamReader(new FileInputStream(inputFilename)));
    }

    @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 thread. init headers and cookies");        
    }
   
    @After
    public void after() {
        tc_counter += 1
    }
   
    @AfterProcess
    public static void afterProcess() {
                grinder.logger.info("afterProcess");
        closeQuietly(reader);
    }

    @Test
    public void test(){
        grinder.logger.info("tc_counter: " + tc_counter);
       
        try {
                        String grxmlFileName = reader.readLine();
                        String inputText = inputReader.readLine();
                        grinder.logger.info("inputText : " + inputText);
                        grinder.logger.info("inputText : " + Codecs.URLEncode(inputText));
                        grinder.logger.info("inputText : " + Codecs.URLDecode(Codecs.URLEncode(inputText)));

                        if (null == grxmlFileName) {
                                grinder.logger.info("readLine is null: reopen file");
                                closeQuietly(reader);
                                reader = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
                                grxmlFileName = reader.readLine();
                        }
                        if (null == inputText) {
                                grinder.logger.info("readLine is null: reopen file");
                                closeQuietly(inputReader);
                                inputReader = new BufferedReader(new InputStreamReader(new FileInputStream(inputFilename)));
                                inputText = inputReader.readLine();
                        }
                       
                        //def reqBody = '{"text":"yes","meta":{"transaction_id":"20200429_dummy_transaction_id",        "enable_kvfarm":true    }}'
                        //grinder.logger.info("reqBody: " + reqBody)
                        // HTTPResponse result = request.POST("http://172.27.107.213:51000/predictor/predict/entry/v1.0", reqBody.getBytes(), headers)
                        //NVParams.append(NVPair(String(k, "utf-8"), String(v, "utf-8")))
                        NVPair[] params = [new NVPair("text", Codecs.URLEncode("가")), new NVPair("meta", "testmeta"), new NVPair("run number", "$grinder.runNumber")]
                        //NVPair[] params = [new NVPair("text", new String("가".getBytes("8859_1"),"utf-8")), new NVPair("meta", "testmeta"), new NVPair("run number", "$grinder.runNumber")]
                        NVPair[] files = [new NVPair("grxmlFile", "./resources/grxml_files/" + grxmlFileName), new NVPair("grxmlFile", "./resources/grxml_files/asr_hes_post.grxml")]

                        def data = Codecs.mpFormDataEncode(params, files, headers)
                        //grinder.logger.info("data : " + data);
                       
                        HTTPResponse result = request.POST("서버주소/predictor/predict/grxml/v1.0", data, headers);
                       
                        if (result.statusCode != 200) {
                                grinder.logger.warn("Warning. The response may not be correct. The response code was {}.", result.statusCode);
                        } else {
                                assertThat(result.statusCode, is(200));
                        }
                } catch (IOException e) {
                        grinder.logger.info("IOException: reopen file", e);
                        closeQuietly(reader);
                        reader = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
                }
    }
   
        public static void closeQuietly(Closeable... closeables) {
                grinder.logger.info("closeQuietly");
                for (Closeable c : closeables) {
                        if (c != null) {
                                try {
                                        c.close();
                                } catch (IOException e) {
                                        e.printStackTrace();
                                }
                        }
                }
    }
}

검색하다가 나온것들 죄다 해보느라 주석이 좀 난잡한데요;
이것저것 다해봐도 저 "가" 라는 한글이 %EA%B0%80 서버에서는 이렇게 보입니다.
postman으로 쏴보면 "가" 그대로 잘 보이구요..

4개의 필드를 쏘는데, 2개는 파일 2개는 텍스트 데이터입니다.

중간에 찍은 로그에서는 잘 찍히는거보면 Codecs.mpFormDataEncode 문제인것같은데..
어떻게 방법이 없을까요?