/scribejava-core/src/test/java/com/github/scribejava/core/utils/OAuthEncoderTest.java

http://github.com/fernandezpablo85/scribe-java · Java · 69 lines · 57 code · 10 blank · 2 comment · 1 complexity · 1493e191d8240f2aee82898a142ef432 MD5 · raw file

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