Last active
August 29, 2015 14:13
-
-
Save shamoh/692ff96e86f5ce775d51 to your computer and use it in GitHub Desktop.
Jersey unit Test to reproduce JERSEY-2713.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.glassfish.jersey.client.ClientConfig; | |
import org.glassfish.jersey.message.internal.ReaderWriter; | |
import org.glassfish.jersey.server.ResourceConfig; | |
import org.glassfish.jersey.test.JerseyTest; | |
import org.junit.Test; | |
import javax.ws.rs.POST; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.Produces; | |
import javax.ws.rs.WebApplicationException; | |
import javax.ws.rs.client.Entity; | |
import javax.ws.rs.client.WebTarget; | |
import javax.ws.rs.core.Application; | |
import javax.ws.rs.core.Response; | |
import javax.ws.rs.ext.WriterInterceptor; | |
import javax.ws.rs.ext.WriterInterceptorContext; | |
import java.io.ByteArrayOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.io.UnsupportedEncodingException; | |
import static org.junit.Assert.assertEquals; | |
public class Jersey2713Test extends JerseyTest { | |
@Override | |
protected Application configure() { | |
return new ResourceConfig() {{ | |
register(StringCharsetResource.class); | |
}}; | |
} | |
@Override | |
protected void configureClient(ClientConfig config) { | |
config.register(RequestEntityInterceptor.class); | |
} | |
@Test | |
public void testIso_8859_9() { | |
String in = "GÖRÜNTÜ İŞLEME GENEL"; | |
WebTarget t = target().path("StringCharsetResource"); | |
String charset = "ISO-8859-9"; | |
Response r = t.path(charset).request().post(Entity.entity(in, "text/plain;charset=" + charset)); | |
byte[] inBytes = requestEntity; | |
byte[] outBytes = getEntityAsByteArray(r); | |
System.out.println(">>>" + in); | |
try { | |
System.out.println("<<<" + (new String(outBytes, charset))); | |
} catch (UnsupportedEncodingException e) { | |
e.printStackTrace(); | |
} | |
_verify(inBytes, outBytes); | |
} | |
protected static byte[] requestEntity; | |
protected static void _verify(byte[] in, byte[] out) { | |
assertEquals(in.length, out.length); | |
for (int i = 0; i < in.length; i++) { | |
if (in[i] != out[i]) { | |
assertEquals("Index: " + i, in[i], out[i]); | |
} | |
} | |
} | |
protected static byte[] getEntityAsByteArray(Response r) { | |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
try { | |
ReaderWriter.writeTo(r.readEntity(InputStream.class), baos); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return baos.toByteArray(); | |
} | |
@Path("/StringCharsetResource") | |
public static class StringCharsetResource { | |
@Path("ISO-8859-9") | |
@POST | |
@Produces("text/plain;charset=ISO-8859-9") | |
public String postIso_8859_9(String t) { | |
return t; | |
} | |
} | |
public static class RequestEntityInterceptor implements WriterInterceptor { | |
@Override | |
public void aroundWriteTo(WriterInterceptorContext writerInterceptorContext) throws IOException, WebApplicationException { | |
OutputStream original = writerInterceptorContext.getOutputStream(); | |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
writerInterceptorContext.setOutputStream(baos); | |
writerInterceptorContext.proceed(); | |
requestEntity = baos.toByteArray(); | |
original.write(requestEntity); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment