You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2014/05/26 12:23:41 UTC

svn commit: r1597551 - in /httpcomponents/httpclient/trunk/fluent-hc/src: main/java/org/apache/http/client/fluent/Content.java test/java/org/apache/http/client/fluent/TestFluent.java

Author: olegk
Date: Mon May 26 10:23:40 2014
New Revision: 1597551

URL: http://svn.apache.org/r1597551
Log:
HTTPCLIENT-1512: Option to override content charset for fluent response objects
Contributed by Jochen Kemnade <jochen.kemnade at eddyson.de>

Modified:
    httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Content.java
    httpcomponents/httpclient/trunk/fluent-hc/src/test/java/org/apache/http/client/fluent/TestFluent.java

Modified: httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Content.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Content.java?rev=1597551&r1=1597550&r2=1597551&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Content.java (original)
+++ httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Content.java Mon May 26 10:23:40 2014
@@ -28,7 +28,6 @@ package org.apache.http.client.fluent;
 
 import java.io.ByteArrayInputStream;
 import java.io.InputStream;
-import java.io.UnsupportedEncodingException;
 import java.nio.charset.Charset;
 
 import org.apache.http.Consts;
@@ -60,11 +59,11 @@ public class Content {
         if (charset == null) {
             charset = Consts.ISO_8859_1;
         }
-        try {
-            return new String(this.raw, charset.name());
-        } catch (final UnsupportedEncodingException ex) {
-            return new String(this.raw);
-        }
+        return asString(charset);
+    }
+
+    public String asString(final Charset charset) {
+        return new String(this.raw, charset);
     }
 
     public InputStream asStream() {

Modified: httpcomponents/httpclient/trunk/fluent-hc/src/test/java/org/apache/http/client/fluent/TestFluent.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/fluent-hc/src/test/java/org/apache/http/client/fluent/TestFluent.java?rev=1597551&r1=1597550&r2=1597551&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/fluent-hc/src/test/java/org/apache/http/client/fluent/TestFluent.java (original)
+++ httpcomponents/httpclient/trunk/fluent-hc/src/test/java/org/apache/http/client/fluent/TestFluent.java Mon May 26 10:23:40 2014
@@ -29,6 +29,7 @@ package org.apache.http.client.fluent;
 import java.io.File;
 import java.io.IOException;
 import java.net.InetSocketAddress;
+import java.nio.charset.Charset;
 
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpEntityEnclosingRequest;
@@ -122,6 +123,17 @@ public class TestFluent extends LocalSer
     }
 
     @Test
+    public void testContentAsStringWithCharset() throws Exception {
+        final InetSocketAddress serviceAddress = this.localServer.getServiceAddress();
+        final String baseURL = "http://localhost:" + serviceAddress.getPort();
+        final Content content = Request.Post(baseURL + "/echo").bodyByteArray("Ü".getBytes("utf-8")).execute()
+                .returnContent();
+        Assert.assertEquals((byte)-61, content.asBytes()[0]);
+        Assert.assertEquals((byte)-100, content.asBytes()[1]);
+        Assert.assertEquals("Ü", content.asString(Charset.forName("utf-8")));
+    }
+
+    @Test
     public void testConnectionRelease() throws Exception {
         final InetSocketAddress serviceAddress = this.localServer.getServiceAddress();
         final String baseURL = "http://localhost:" + serviceAddress.getPort();