package org.jboss.resteasy.test.encoding;

import org.jboss.resteasy.client.ClientResponse;
import org.jboss.resteasy.client.ProxyFactory;
import org.jboss.resteasy.plugins.server.tjws.TJWSEmbeddedJaxrsServer;
import org.jboss.resteasy.test.TestPortProvider;
import org.jboss.resteasy.util.Encode;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.nio.charset.CharacterCodingException;

public class EncodingTest
{
   private static TJWSEmbeddedJaxrsServer tjws;
   private static TestClient testClient;

   @BeforeClass
   public static void setupContainer()
   {
      tjws = new TJWSEmbeddedJaxrsServer();
      tjws.setPort(TestPortProvider.getPort());
      tjws.setRootResourcePath("/");
      tjws.start();
      tjws.getDeployment().getDispatcher().getRegistry().addSingletonResource(new MyTestResource());
      String url = "http://localhost:" + TestPortProvider.getPort();
      testClient = ProxyFactory.create(TestClient.class, url);

   }

   @AfterClass
   public static void teardownContainer()
   {
      tjws.stop();
   }

   Character[] RESERVED_CHARACTERS = {
           '?', ':', '@', '&', '=', '+', '$', ','
   };

   //also includes a-zA-Z0-9
   Character[] UNRESERVED_CHARACTERS = {
           '-', '_', '.', '!', '~', '*', '\'', '(', ')'
   };

   //also includes 0x00-0x1F and 0x7F
   Character[] EXCLUDED_CHARACTERS = {
           ' ', '<', '>', '#', '%', '\"'
   };

   Character[] UNWISE_CHARACTERS = {
           '{', '}', '|', '\\', '^', '[', ']', '`'
   };

   /**
    * Tests requesting special characters via a ClientProxy.
    */
   @Test
   public void testEncodingCharacters() throws Exception
   {
      for (Character ch : RESERVED_CHARACTERS)
      {
         encodingCharacter(ch);
      }
      for (Character ch : UNRESERVED_CHARACTERS)
      {
         encodingCharacter(ch);
      }
      for (Character ch : EXCLUDED_CHARACTERS)
      {
         encodingCharacter(ch);
      }
      for (Character ch : UNWISE_CHARACTERS)
      {
         encodingCharacter(ch);
      }
   }

   public void encodingCharacter(Character toTest)
   {
      String paramWithChar = "start" + toTest + "end";
      System.out.println("*** " + paramWithChar);
      ClientResponse<String> returned = testClient.getPathParam(paramWithChar);
      Assert.assertNotNull(returned);
      Assert.assertEquals(returned.getStatus(), HttpURLConnection.HTTP_OK);
      Assert.assertEquals(returned.getEntity(), paramWithChar);
   }

   @Test
   public void testPercent()
   {
      encodingCharacter('\\');
   }

   /**
    * Tests requesting special characters via manual URL construction.
    */
   @Test
   public void testViaDirectURI() throws Exception
   {
      for (Character ch : RESERVED_CHARACTERS)
      {
         viaDirectURI(ch);
      }
      for (Character ch : UNRESERVED_CHARACTERS)
      {
         viaDirectURI(ch);
      }
      for (Character ch : EXCLUDED_CHARACTERS)
      {
         viaDirectURI(ch);
      }
      for (Character ch : UNWISE_CHARACTERS)
      {
         viaDirectURI(ch);
      }

   }

   @Test
   public void decodeChineseInPath() throws Exception
   {
      String path = Encode.decodePath("/%E5%B9%B4%E5%81%87%E6%9C%9F/%E5%B9%B4%E5%81%87%E6%9C%9F");
      System.out.println(path);
      Assert.assertEquals("/\u5E74\u5047\u671F/\u5E74\u5047\u671F", path);
   }

   @Test
   public void decodeWesternEuropeanCharsInPath() throws Exception
   {
      Assert.assertEquals("Gr\u00FC\u00DF Gott", Encode.decodePath("Gr%C3%BC%C3%9F%20Gott"));
      Assert.assertEquals("D\u00E6lenenga Gr\u00FCnerl\u00F8kka", Encode.decodePath("D%C3%A6lenenga%20Gr%C3%BCnerl%C3%B8kka"));
      Assert.assertEquals("\u00C4lv\u00E5s V\u00E4stra G\u00F6taland", Encode.decodePath("%C3%84lv%C3%A5s%20V%C3%A4stra%20G%C3%B6taland"));
   }

   public void viaDirectURI(Character toTest) throws Exception
   {
      System.out.println("*** testing character '" + toTest + "'");
      String uriBase = "http://localhost:" + TestPortProvider.getPort() + "/test/path-param/";
      String expected = "start" + toTest + "end";
      String encoded = "start%" + Integer.toHexString(toTest).toUpperCase() + "end";
      URI uri = URI.create(uriBase + encoded);

      HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection();
      connection.setRequestMethod("GET");
      connection.setRequestProperty("accept", "text/plain");
      InputStream is = connection.getInputStream();
      Reader r = new InputStreamReader(is, "UTF-8");
      StringBuffer buf = new StringBuffer();
      char[] chars = new char[1024];
      int charsRead;
      while ((charsRead = r.read(chars)) != -1)
      {
         buf.append(chars, 0, charsRead);
      }
      r.close();
      is.close();

      Assert.assertEquals(buf.toString(), expected);
   }

   /**
    * Test method for
    * {@link org.jboss.resteasy.util.Encode#encodeQueryParam(java.lang.String)}
    * .
    */
   @Test
   public void testEncodeQueryParam()
   {
      Assert.assertEquals("Test", Encode.encodeQueryParam("Test"));
   }

   @Test
   public void testEncodeQueryParam_special()
   {
      Assert.assertEquals(".-%2B%2F%7C", Encode.encodeQueryParam(".-+/|"));
   }

   @Test
   public void testEncodeQueryParam_scands() throws UnsupportedEncodingException
   {
      String input = "\u00E5\u00E4\u00F6\u00C5\u00C4\u00D6";
      String expected = "%C3%A5%C3%A4%C3%B6%C3%85%C3%84%C3%96";
      Assert.assertEquals(expected, Encode.encodeQueryParam(input));
   }

   @Test
   public void testEncodeQueryParam_utf8_2byte() throws CharacterCodingException, UnsupportedEncodingException
   {
      StringBuilder sb = new StringBuilder();
      for (int n = 0; n < 2047; n++)
      {
         sb.append((char) n);
      }
      String characters = sb.toString();
      String encodedQueryParam = Encode.encodeQueryParam(characters);
      String expected = "%00%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F+%21%22%23%24%25%26%27%28%29*%2B%2C-.%2F0123456789%3A%3B%3C%3D%3E?%40ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz{|}~%7F%C2%80%C2%81%C2%82%C2%83%C2%84%C2%85%C2%86%C2%87%C2%88%C2%89%C2%8A%C2%8B%C2%8C%C2%8D%C2%8E%C2%8F%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99%DE%9A%DE%9B%DE%9C%DE%9D%DE%9E%DE%9F%DE%A0%DE%A1%DE%A2%DE%A3%DE%A4%DE%A5%DE%A6%DE%A7%DE%A8%DE%A9%DE%AA%DE%AB%DE%AC%DE%AD%DE%AE%DE%AF%DE%B0%DE%B1%DE%B2%DE%B3%DE%B4%DE%B5%DE%B6%DE%B7%DE%B8%DE%B9%DE%BA%DE%BB%DE%BC%DE%BD%DE%BE%DE%BF%DF%80%DF%81%DF%82%DF%83%DF%84%DF%85%DF%86%DF%87%DF%88%DF%89%DF%8A%DF%8B%DF%8C%DF%8D%DF%8E%DF%8F%DF%90%DF%91%DF%92%DF%93%DF%94%DF%95%DF%96%DF%97%DF%98%DF%99%DF%9A%DF%9B%DF%9C%DF%9D%DF%9E%DF%9F%DF%A0%DF%A1%DF%A2%DF%A3%DF%A4%DF%A5%DF%A6%DF%A7%DF%A8%DF%A9%DF%AA%DF%AB%DF%AC%DF%AD%DF%AE%DF%AF%DF%B0%DF%B1%DF%B2%DF%B3%DF%B4%DF%B5%DF%B6%DF%B7%DF%B8%DF%B9%DF%BA%DF%BB%DF%BC%DF%BD%DF%BE";
      Assert.assertEquals(expected, encodedQueryParam);
   }

   public void testPathParamWithDoublePercent() {
       String paramWithDoublePercent = "start%%end";
       System.out.println("*** " + paramWithDoublePercent);
       ClientResponse<String> returned = testClient.getPathParam(paramWithDoublePercent);
       Assert.assertNotNull(returned);
       Assert.assertEquals(HttpURLConnection.HTTP_OK, returned.getStatus());
       Assert.assertEquals(paramWithDoublePercent, returned.getEntity());
   }

   @Test
   public void testPathParamWithBraces() {
       String paramWithBraces = "start{param}end";
       System.out.println("*** " + paramWithBraces);
       ClientResponse<String> returned = testClient.getPathParam(paramWithBraces);
       Assert.assertNotNull(returned);
       Assert.assertEquals(HttpURLConnection.HTTP_OK, returned.getStatus());
       Assert.assertEquals(paramWithBraces, returned.getEntity());
   }

   @Test
   public void testPathParamWithLifePercentDeath() {
       String paramWithLifePercentDeath = "life%death";
       System.out.println("*** " + paramWithLifePercentDeath);
       ClientResponse<String> returned = testClient.getPathParam(paramWithLifePercentDeath);
       Assert.assertNotNull(returned);
       Assert.assertEquals(HttpURLConnection.HTTP_OK, returned.getStatus());
       Assert.assertEquals(paramWithLifePercentDeath, returned.getEntity());
   }

   @Test
   public void testQueryParamWithDoublePercent() {
      String paramWithDoublePercent = "start%%end";
       System.out.println("*** " + paramWithDoublePercent);
       ClientResponse<String> returned = testClient.getQueryParam(paramWithDoublePercent);
       Assert.assertNotNull(returned);
       Assert.assertEquals(HttpURLConnection.HTTP_OK, returned.getStatus());
       Assert.assertEquals(paramWithDoublePercent, returned.getEntity());
   }

   @Test
   public void testQueryParamWithBraces() {
       String paramWithBraces = "start{param}end";
       System.out.println("*** " + paramWithBraces);
       ClientResponse<String> returned = testClient.getQueryParam(paramWithBraces);
       Assert.assertNotNull(returned);
       Assert.assertEquals(HttpURLConnection.HTTP_OK, returned.getStatus());
       Assert.assertEquals(paramWithBraces, returned.getEntity());
   }

   @Test
   public void testQueryParamWithLifePercentDeath() {
       String paramWithLifePercentDeath = "life%death";
       System.out.println("*** " + paramWithLifePercentDeath);
       ClientResponse<String> returned = testClient.getQueryParam(paramWithLifePercentDeath);
       Assert.assertNotNull(returned);
       Assert.assertEquals(HttpURLConnection.HTTP_OK, returned.getStatus());
       Assert.assertEquals(paramWithLifePercentDeath, returned.getEntity());
   }

   @Test
   public void testEncodeNonCodes() {
       Assert.assertEquals("", Encode.encodeNonCodes(""));
       Assert.assertEquals("a simple string", Encode.encodeNonCodes("a simple string"));
       Assert.assertEquals("%25", Encode.encodeNonCodes("%"));
       Assert.assertEquals("%25%25%25%25", Encode.encodeNonCodes("%%%%"));
       Assert.assertEquals("%25%25", Encode.encodeNonCodes("%%25"));
       Assert.assertEquals("%25a%25", Encode.encodeNonCodes("%a%25"));
       Assert.assertEquals("a%25b", Encode.encodeNonCodes("a%b"));
       Assert.assertEquals("a%25b", Encode.encodeNonCodes("a%25b"));
       Assert.assertEquals("a%25%25%25%25b%25%25%25%25c", Encode.encodeNonCodes("a%%%%b%%25%%c"));
   }

}
