Play 1.x 기능 테스트 혼란 및 인코딩 문제(Encoded Question In Play 1.x Functional Test)
{"box":[{"entityId":7,"id":7,"idea":"?????","jPAContext":{"insideTransaction":true,"jPAConfig":{"configName":"play","enabled":true,"insideTransaction":true,"jPAContext":{"$ref":".."},"jpql":{}}},"persistent":false,"type":1},{"entityId":8,"id":8,"idea":"???","jPAContext":{"$ref":"$.box[0].jPAContext"},"persistent":false,"type":5},{"idea":"???","jPAContext":{"$ref":"$.box[0].jPAContext"},"persistent":false,"type":3}],"code":"jack","entityId":1003,"id":1003,"ip":"192.168.1.122","jPAContext":{"$ref":"$.box[0].jPAContext"},"nick":"??","passwd":"jack2","persistent":true,"qqmail":"[email protected]"}
이것은 전형적인 인코딩 문제이지만 플레이를 다 찾아봐도 관련 설정을 찾지 못해서 디버깅으로 볼 수밖에 없다.
1.yml 파일에서 데이터를 읽는 과정에서 데이터가 아직 부호화되지 않은 것을 발견했습니다
2. 7장에서 언급한 Action Invoker에 도착을 요청할 때 이미 코드가 엉망이 되었다.
그래서 저는 플레이 프레임워크가 POST 요청을 봉인하는 데 문제가 생겼을 거라고 추측합니다. 모든 기능 테스트의 부류인 Functional Test에서 많은 POST 방법을 다시 불러왔습니다. 그 중에서 근본적으로 이 POST: 플레이입니다.test.FunctionalTest.java
public static Response POST(Request request, Object url, Map parameters, Map files) {
List parts = new ArrayList();
for (String key : parameters.keySet()) {
parts.add(new StringPart(key, parameters.get(key)));
}
for (String key : files.keySet()) {
Part filePart;
try {
filePart = new FilePart(key, files.get(key));
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
parts.add(filePart);
}
MultipartRequestEntity requestEntity = new MultipartRequestEntity(parts.toArray(new Part[]{}), null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
requestEntity.writeRequest(baos);
} catch (IOException e) {
throw new RuntimeException(e);
}
InputStream body = new ByteArrayInputStream(baos.toByteArray());
String contentType = requestEntity.getContentType();
Http.Header header = new Http.Header();
header.name = "content-type";
header.values = Arrays.asList(new String[]{contentType});
request.headers.put("content-type", header);
return POST(request, url, MULTIPART_FORM_DATA, body);
}
이 방법에는 new String Part() 방법이 있습니다.
parts.add(new StringPart(key, parameters.get(key)));
내가 따라들어간 후에 이 녀석도 중재 방법인com을 발견하였다.ning.http.multipart.StringPart.java
public StringPart(String name, String value, String charset)
{
super(name, "text/plain", charset != null ? charset : "US-ASCII", "8bit");
if(value == null)
throw new IllegalArgumentException("Value may not be null");
if(value.indexOf('\0') != -1)
{
throw new IllegalArgumentException("NULs may not be present in string parts");
} else
{
this.value = value;
return;
}
}
public StringPart(String name, String value)
{
this(name, value, null);
}
코드는 일목요연하고,
super(name, "text/plain", charset != null ? charset : "US-ASCII", "8bit");
charset이 없으면 "US-ASCII"를 묵인합니다!문제가 생기면 해결 방법이 많아진다. StringPart에서 기본값을'UTF-8'로 바꾸거나 POST 방법에서'UTF-8'파라미터를 하나 더 전달할 수 있다.그러나 이 두 가지 방법은 모두 그다지 우아하지 않다. 내가 여기에 한 가지 방법을 제공한다.우선 테스트 클래스의 부류를 구성합니다:function.BasisTest.java
public class BasisTest extends FunctionalTest{
public static Response POST(Object url, Map parameters) {
return POST(newLocalRequest(), url, parameters, new HashMap());
}
public static Request newLocalRequest() {
Request request = Request.createRequest(
null,
"GET",
"/",
"",
null,
null,
null,
null,
false,
80,
"localhost",
false,
null,
null
);
request.encoding = "utf-8";
return request;
}
}
FunctionalTest의 POST 클래스를 덮어쓰고 자신의 Request를 구축합니다. 이 곳에서 쿠키를 설정하여 로그인 차단이 있는 상황에서 기능 테스트를 할 수 있습니다.여기는 주로 요청한 encoding을 설정합니다.그리고 FunctionalTest에서 POST 방법에서 StringPart 구조 방법을 세 개의 매개 변수로 하고 Request를 추가합니다.encoding.
parts.add(new StringPart(key, parameters.get(key), request.encoding));
이렇게 인코딩하는 것은 살아있는 것이기 때문에 스스로 설정할 수 있다.
{"box":[{"entityId":7,"id":7,"idea":" ","jPAContext":{"insideTransaction":true,"jPAConfig":{"configName":"play","enabled":true,"insideTransaction":true,"jPAContext":{"$ref":".."},"jpql":{}}},"persistent":false,"type":1},{"entityId":8,"id":8,"idea":" ","jPAContext":{"$ref":"$.box[0].jPAContext"},"persistent":false,"type":5},{"idea":" ","jPAContext":{"$ref":"$.box[0].jPAContext"},"persistent":false,"type":3}],"code":"jack","entityId":1003,"id":1003,"ip":"192.168.1.122","jPAContext":{"$ref":"$.box[0].jPAContext"},"nick":" ","passwd":"jack2","persistent":true,"qqmail":"[email protected]"}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Play 1.x 기능 테스트 혼란 및 인코딩 문제(Encoded Question In Play 1.x Functional Test)모두들 기능 테스트를 할 때 utf-8을 인코딩으로 통일하면 POST 테스트 요청에 한자가 있으면 인코딩이 엉망이 되는 것을 발견했는지 모르겠다. 이것은 전형적인 인코딩 문제이지만 플레이를 다 찾아봐도 관련 설정을 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.