PageRenderTime 106ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/java/org/scribe/utils/OAuthEncoderTest.java

http://github.com/fernandezpablo85/scribe-java
Java | 70 lines | 57 code | 9 blank | 4 comment | 1 complexity | 521094852a327272aa5decd553e24dda MD5 | raw file
  1. package org.scribe.utils;
  2. import org.junit.*;
  3. import static org.junit.Assert.*;
  4. /**
  5. * @author: Pablo Fernandez
  6. */
  7. public class OAuthEncoderTest
  8. {
  9. @Test
  10. public void shouldPercentEncodeString()
  11. {
  12. String plain = "this is a test &^";
  13. String encoded = "this%20is%20a%20test%20%26%5E";
  14. assertEquals(encoded, OAuthEncoder.encode(plain));
  15. }
  16. @Test
  17. public void shouldFormURLDecodeString()
  18. {
  19. String encoded = "this+is+a+test+%26%5E";
  20. String plain = "this is a test &^";
  21. assertEquals(plain, OAuthEncoder.decode(encoded));
  22. }
  23. @Test
  24. public void shouldPercentEncodeAllSpecialCharacters()
  25. {
  26. String plain = "!*'();:@&=+$,/?#[]";
  27. String encoded = "%21%2A%27%28%29%3B%3A%40%26%3D%2B%24%2C%2F%3F%23%5B%5D";
  28. assertEquals(encoded, OAuthEncoder.encode(plain));
  29. assertEquals(plain, OAuthEncoder.decode(encoded));
  30. }
  31. @Test
  32. public void shouldNotPercentEncodeReservedCharacters()
  33. {
  34. String plain = "abcde123456-._~";
  35. String encoded = plain;
  36. assertEquals(encoded, OAuthEncoder.encode(plain));
  37. }
  38. @Test(expected = IllegalArgumentException.class)
  39. public void shouldThrowExceptionIfStringToEncodeIsNull()
  40. {
  41. String toEncode = null;
  42. OAuthEncoder.encode(toEncode);
  43. }
  44. @Test(expected = IllegalArgumentException.class)
  45. public void shouldThrowExceptionIfStringToDecodeIsNull()
  46. {
  47. String toDecode = null;
  48. OAuthEncoder.decode(toDecode);
  49. }
  50. @Test
  51. public void shouldPercentEncodeCorrectlyTwitterCodingExamples()
  52. {
  53. // These tests are part of the Twitter dev examples here -> https://dev.twitter.com/docs/auth/percent-encoding-parameters
  54. String sources[] = {"Ladies + Gentlemen", "An encoded string!", "Dogs, Cats & Mice"};
  55. String encoded[] = {"Ladies%20%2B%20Gentlemen", "An%20encoded%20string%21", "Dogs%2C%20Cats%20%26%20Mice"};
  56. for(int i = 0; i < sources.length; i++)
  57. {
  58. Assert.assertEquals(encoded[i], OAuthEncoder.encode(sources[i]));
  59. }
  60. }
  61. }