Skip to content
This repository was archived by the owner on Feb 8, 2023. It is now read-only.

Commit a5c455d

Browse files
Fix the retrieval of UTF-8 characters from the server
With this all encoding tests pass for js-cookie, ;D
1 parent 0f8a8f4 commit a5c455d

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/test/java/org/jscookie/test/integration/encoding/EncodingServlet.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package org.jscookie.test.integration.encoding;
22

33
import java.io.IOException;
4+
import java.io.UnsupportedEncodingException;
5+
import java.net.URLDecoder;
6+
import java.nio.charset.StandardCharsets;
47

58
import javax.servlet.ServletException;
69
import javax.servlet.annotation.WebServlet;
@@ -18,7 +21,7 @@ public class EncodingServlet extends HttpServlet {
1821
@Override
1922
public void doGet( HttpServletRequest request, HttpServletResponse response )
2023
throws ServletException, IOException {
21-
String name = request.getParameter( "name" );
24+
String name = getUTF8Param( "name", request );
2225

2326
System.out.println( "--------------------" );
2427
System.out.println( "Testing: " + name );
@@ -39,6 +42,19 @@ public void doGet( HttpServletRequest request, HttpServletResponse response )
3942
new ObjectMapper()
4043
.writeValue( response.getOutputStream(), new Result( name, value ) );
4144
}
45+
46+
/**
47+
* Retrieves the parameter using UTF-8 charset since the server default is ISO-8859-1
48+
*/
49+
private String getUTF8Param( String name, HttpServletRequest request ) throws UnsupportedEncodingException {
50+
String query = request.getQueryString();
51+
for ( String pair : query.split( "&" ) ) {
52+
if ( name.equals( pair.split( "=" )[ 0 ] ) ) {
53+
return URLDecoder.decode( pair.split( "=" )[ 1 ], StandardCharsets.UTF_8.name() );
54+
}
55+
}
56+
return null;
57+
}
4258
}
4359

4460
class Result {

0 commit comments

Comments
 (0)