leotu edited a comment on issue #85: [CALCITE-2704] Avoid use of ISO-8859-1 to 
parse request in JsonHandler
URL: https://github.com/apache/calcite-avatica/pull/85#issuecomment-503959813
 
 
   Hi, here is my test codes for this issue.
   Issue: https://github.com/apache/calcite-avatica/pull/76
   
   ```java
   package org.apache.calcite.avatica.server;
   
   import java.io.ByteArrayInputStream;
   import java.io.IOException;
   import java.io.InputStream;
   import java.nio.charset.StandardCharsets;
   
   import org.apache.calcite.avatica.AvaticaUtils;
   import org.apache.calcite.avatica.util.UnsynchronizedBuffer;
   import org.junit.Assert;
   import org.junit.Test;
   import org.slf4j.Logger;
   import org.slf4j.LoggerFactory;
   
   public class AvaticaJsonHandlerTest {
        private static final Logger LOG = 
LoggerFactory.getLogger(AvaticaJsonHandlerTest.class);
        
        private String requestChineseData = "Hello Word (你好,世界) !";
        
        final ThreadLocal<UnsynchronizedBuffer> threadLocalBuffer;
        
        public AvaticaJsonHandlerTest() {
             this.threadLocalBuffer = new ThreadLocal<UnsynchronizedBuffer>() {
              @Override public UnsynchronizedBuffer initialValue() {
                return new UnsynchronizedBuffer();
              }
            };
        }
            
        @Test
        public void testShapshotCorrect() throws IOException {
                String requestCharacterEncoding = null;
                String requestHeader = null;
                
                InputStream requestInputStream = new 
ByteArrayInputStream(requestChineseData.getBytes(StandardCharsets.UTF_8));
                
                String rawRequest = requestHeader;
           if (rawRequest == null) {
             // Avoid a new buffer creation for every HTTP request
             final UnsynchronizedBuffer buffer = threadLocalBuffer.get();
             try (InputStream inputStream = requestInputStream) {
               byte[] bytes = AvaticaUtils.readFullyToBytes(inputStream, 
buffer);
               String encoding = requestCharacterEncoding;
               if (encoding == null) {
                 encoding = StandardCharsets.UTF_8.name();
               }
               rawRequest = new String(bytes, encoding);
               
             } finally {
               // Reset the offset into the buffer after we're done
               buffer.reset();
             }
           }
           final String jsonRequest = rawRequest;
           LOG.info("Correct decoded request: {}", jsonRequest);
           Assert.assertEquals(requestChineseData, jsonRequest);
        }
   
        @Test
        public void testShapshotError() throws IOException {
                String requestHeader = null;
                
                InputStream requestInputStream = new 
ByteArrayInputStream(requestChineseData.getBytes(StandardCharsets.UTF_8));
                
                String rawRequest = requestHeader;
           if (rawRequest == null) {
             // Avoid a new buffer creation for every HTTP request
             final UnsynchronizedBuffer buffer = threadLocalBuffer.get();
             try (InputStream inputStream = requestInputStream) {
               rawRequest = AvaticaUtils.readFully(inputStream, buffer);
             } finally {
               // Reset the offset into the buffer after we're done
               buffer.reset();
             }
           }
           final String jsonRequest =
                   new String(rawRequest.getBytes("ISO-8859-1"), "UTF-8");
           LOG.info("Error decoded request: {}", jsonRequest);
           Assert.assertEquals(requestChineseData, jsonRequest);
        }
   
   }
   
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to