|
2 | 2 |
|
3 | 3 | import static org.junit.Assert.*; |
4 | 4 |
|
| 5 | +import org.json.JSONArray; |
| 6 | +import org.json.JSONException; |
| 7 | +import org.json.JSONObject; |
5 | 8 | import org.junit.Test; |
6 | 9 |
|
7 | | - |
8 | 10 | public class HelperTest { |
| 11 | + // this is taken from the URL https://www.backblaze.com/b2/docs/string_encoding.html |
| 12 | + |
| 13 | + private static final String ENCODING_TEST_JSON = "[\n" + |
| 14 | + " {\"fullyEncoded\": \"%20\", \"minimallyEncoded\": \"+\", \"string\": \" \"},\n" + |
| 15 | + " {\"fullyEncoded\": \"%21\", \"minimallyEncoded\": \"!\", \"string\": \"!\"},\n" + |
| 16 | + " {\"fullyEncoded\": \"%22\", \"minimallyEncoded\": \"%22\", \"string\": \"\\\"\"},\n" + |
| 17 | + " {\"fullyEncoded\": \"%23\", \"minimallyEncoded\": \"%23\", \"string\": \"#\"},\n" + |
| 18 | + " {\"fullyEncoded\": \"%24\", \"minimallyEncoded\": \"$\", \"string\": \"$\"},\n" + |
| 19 | + " {\"fullyEncoded\": \"%25\", \"minimallyEncoded\": \"%25\", \"string\": \"%\"},\n" + |
| 20 | + " {\"fullyEncoded\": \"%26\", \"minimallyEncoded\": \"%26\", \"string\": \"&\"},\n" + |
| 21 | + " {\"fullyEncoded\": \"%27\", \"minimallyEncoded\": \"'\", \"string\": \"'\"},\n" + |
| 22 | + " {\"fullyEncoded\": \"%28\", \"minimallyEncoded\": \"(\", \"string\": \"(\"},\n" + |
| 23 | + " {\"fullyEncoded\": \"%29\", \"minimallyEncoded\": \")\", \"string\": \")\"},\n" + |
| 24 | + " {\"fullyEncoded\": \"%2A\", \"minimallyEncoded\": \"*\", \"string\": \"*\"},\n" + |
| 25 | + " {\"fullyEncoded\": \"%2B\", \"minimallyEncoded\": \"%2B\", \"string\": \"+\"},\n" + |
| 26 | + " {\"fullyEncoded\": \"%2C\", \"minimallyEncoded\": \"%2C\", \"string\": \",\"},\n" + |
| 27 | + " {\"fullyEncoded\": \"%2D\", \"minimallyEncoded\": \"-\", \"string\": \"-\"},\n" + |
| 28 | + " {\"fullyEncoded\": \"%2E\", \"minimallyEncoded\": \".\", \"string\": \".\"},\n" + |
| 29 | + " {\"fullyEncoded\": \"/\", \"minimallyEncoded\": \"/\", \"string\": \"/\"},\n" + |
| 30 | + " {\"fullyEncoded\": \"%30\", \"minimallyEncoded\": \"0\", \"string\": \"0\"},\n" + |
| 31 | + " {\"fullyEncoded\": \"%31\", \"minimallyEncoded\": \"1\", \"string\": \"1\"},\n" + |
| 32 | + " {\"fullyEncoded\": \"%32\", \"minimallyEncoded\": \"2\", \"string\": \"2\"},\n" + |
| 33 | + " {\"fullyEncoded\": \"%33\", \"minimallyEncoded\": \"3\", \"string\": \"3\"},\n" + |
| 34 | + " {\"fullyEncoded\": \"%34\", \"minimallyEncoded\": \"4\", \"string\": \"4\"},\n" + |
| 35 | + " {\"fullyEncoded\": \"%35\", \"minimallyEncoded\": \"5\", \"string\": \"5\"},\n" + |
| 36 | + " {\"fullyEncoded\": \"%36\", \"minimallyEncoded\": \"6\", \"string\": \"6\"},\n" + |
| 37 | + " {\"fullyEncoded\": \"%37\", \"minimallyEncoded\": \"7\", \"string\": \"7\"},\n" + |
| 38 | + " {\"fullyEncoded\": \"%38\", \"minimallyEncoded\": \"8\", \"string\": \"8\"},\n" + |
| 39 | + " {\"fullyEncoded\": \"%39\", \"minimallyEncoded\": \"9\", \"string\": \"9\"},\n" + |
| 40 | + " {\"fullyEncoded\": \"%3A\", \"minimallyEncoded\": \":\", \"string\": \":\"},\n" + |
| 41 | + " {\"fullyEncoded\": \"%3B\", \"minimallyEncoded\": \";\", \"string\": \";\"},\n" + |
| 42 | + " {\"fullyEncoded\": \"%3C\", \"minimallyEncoded\": \"%3C\", \"string\": \"<\"},\n" + |
| 43 | + " {\"fullyEncoded\": \"%3D\", \"minimallyEncoded\": \"=\", \"string\": \"=\"},\n" + |
| 44 | + " {\"fullyEncoded\": \"%3E\", \"minimallyEncoded\": \"%3E\", \"string\": \">\"},\n" + |
| 45 | + " {\"fullyEncoded\": \"%3F\", \"minimallyEncoded\": \"%3F\", \"string\": \"?\"},\n" + |
| 46 | + " {\"fullyEncoded\": \"%40\", \"minimallyEncoded\": \"@\", \"string\": \"@\"},\n" + |
| 47 | + " {\"fullyEncoded\": \"%41\", \"minimallyEncoded\": \"A\", \"string\": \"A\"},\n" + |
| 48 | + " {\"fullyEncoded\": \"%42\", \"minimallyEncoded\": \"B\", \"string\": \"B\"},\n" + |
| 49 | + " {\"fullyEncoded\": \"%43\", \"minimallyEncoded\": \"C\", \"string\": \"C\"},\n" + |
| 50 | + " {\"fullyEncoded\": \"%44\", \"minimallyEncoded\": \"D\", \"string\": \"D\"},\n" + |
| 51 | + " {\"fullyEncoded\": \"%45\", \"minimallyEncoded\": \"E\", \"string\": \"E\"},\n" + |
| 52 | + " {\"fullyEncoded\": \"%46\", \"minimallyEncoded\": \"F\", \"string\": \"F\"},\n" + |
| 53 | + " {\"fullyEncoded\": \"%47\", \"minimallyEncoded\": \"G\", \"string\": \"G\"},\n" + |
| 54 | + " {\"fullyEncoded\": \"%48\", \"minimallyEncoded\": \"H\", \"string\": \"H\"},\n" + |
| 55 | + " {\"fullyEncoded\": \"%49\", \"minimallyEncoded\": \"I\", \"string\": \"I\"},\n" + |
| 56 | + " {\"fullyEncoded\": \"%4A\", \"minimallyEncoded\": \"J\", \"string\": \"J\"},\n" + |
| 57 | + " {\"fullyEncoded\": \"%4B\", \"minimallyEncoded\": \"K\", \"string\": \"K\"},\n" + |
| 58 | + " {\"fullyEncoded\": \"%4C\", \"minimallyEncoded\": \"L\", \"string\": \"L\"},\n" + |
| 59 | + " {\"fullyEncoded\": \"%4D\", \"minimallyEncoded\": \"M\", \"string\": \"M\"},\n" + |
| 60 | + " {\"fullyEncoded\": \"%4E\", \"minimallyEncoded\": \"N\", \"string\": \"N\"},\n" + |
| 61 | + " {\"fullyEncoded\": \"%4F\", \"minimallyEncoded\": \"O\", \"string\": \"O\"},\n" + |
| 62 | + " {\"fullyEncoded\": \"%50\", \"minimallyEncoded\": \"P\", \"string\": \"P\"},\n" + |
| 63 | + " {\"fullyEncoded\": \"%51\", \"minimallyEncoded\": \"Q\", \"string\": \"Q\"},\n" + |
| 64 | + " {\"fullyEncoded\": \"%52\", \"minimallyEncoded\": \"R\", \"string\": \"R\"},\n" + |
| 65 | + " {\"fullyEncoded\": \"%53\", \"minimallyEncoded\": \"S\", \"string\": \"S\"},\n" + |
| 66 | + " {\"fullyEncoded\": \"%54\", \"minimallyEncoded\": \"T\", \"string\": \"T\"},\n" + |
| 67 | + " {\"fullyEncoded\": \"%55\", \"minimallyEncoded\": \"U\", \"string\": \"U\"},\n" + |
| 68 | + " {\"fullyEncoded\": \"%56\", \"minimallyEncoded\": \"V\", \"string\": \"V\"},\n" + |
| 69 | + " {\"fullyEncoded\": \"%57\", \"minimallyEncoded\": \"W\", \"string\": \"W\"},\n" + |
| 70 | + " {\"fullyEncoded\": \"%58\", \"minimallyEncoded\": \"X\", \"string\": \"X\"},\n" + |
| 71 | + " {\"fullyEncoded\": \"%59\", \"minimallyEncoded\": \"Y\", \"string\": \"Y\"},\n" + |
| 72 | + " {\"fullyEncoded\": \"%5A\", \"minimallyEncoded\": \"Z\", \"string\": \"Z\"},\n" + |
| 73 | + " {\"fullyEncoded\": \"%5B\", \"minimallyEncoded\": \"%5B\", \"string\": \"[\"},\n" + |
| 74 | + " {\"fullyEncoded\": \"%5C\", \"minimallyEncoded\": \"%5C\", \"string\": \"\\\\\"},\n" + |
| 75 | + " {\"fullyEncoded\": \"%5D\", \"minimallyEncoded\": \"%5D\", \"string\": \"]\"},\n" + |
| 76 | + " {\"fullyEncoded\": \"%5E\", \"minimallyEncoded\": \"%5E\", \"string\": \"^\"},\n" + |
| 77 | + " {\"fullyEncoded\": \"%5F\", \"minimallyEncoded\": \"_\", \"string\": \"_\"},\n" + |
| 78 | + " {\"fullyEncoded\": \"%60\", \"minimallyEncoded\": \"%60\", \"string\": \"`\"},\n" + |
| 79 | + " {\"fullyEncoded\": \"%61\", \"minimallyEncoded\": \"a\", \"string\": \"a\"},\n" + |
| 80 | + " {\"fullyEncoded\": \"%62\", \"minimallyEncoded\": \"b\", \"string\": \"b\"},\n" + |
| 81 | + " {\"fullyEncoded\": \"%63\", \"minimallyEncoded\": \"c\", \"string\": \"c\"},\n" + |
| 82 | + " {\"fullyEncoded\": \"%64\", \"minimallyEncoded\": \"d\", \"string\": \"d\"},\n" + |
| 83 | + " {\"fullyEncoded\": \"%65\", \"minimallyEncoded\": \"e\", \"string\": \"e\"},\n" + |
| 84 | + " {\"fullyEncoded\": \"%66\", \"minimallyEncoded\": \"f\", \"string\": \"f\"},\n" + |
| 85 | + " {\"fullyEncoded\": \"%67\", \"minimallyEncoded\": \"g\", \"string\": \"g\"},\n" + |
| 86 | + " {\"fullyEncoded\": \"%68\", \"minimallyEncoded\": \"h\", \"string\": \"h\"},\n" + |
| 87 | + " {\"fullyEncoded\": \"%69\", \"minimallyEncoded\": \"i\", \"string\": \"i\"},\n" + |
| 88 | + " {\"fullyEncoded\": \"%6A\", \"minimallyEncoded\": \"j\", \"string\": \"j\"},\n" + |
| 89 | + " {\"fullyEncoded\": \"%6B\", \"minimallyEncoded\": \"k\", \"string\": \"k\"},\n" + |
| 90 | + " {\"fullyEncoded\": \"%6C\", \"minimallyEncoded\": \"l\", \"string\": \"l\"},\n" + |
| 91 | + " {\"fullyEncoded\": \"%6D\", \"minimallyEncoded\": \"m\", \"string\": \"m\"},\n" + |
| 92 | + " {\"fullyEncoded\": \"%6E\", \"minimallyEncoded\": \"n\", \"string\": \"n\"},\n" + |
| 93 | + " {\"fullyEncoded\": \"%6F\", \"minimallyEncoded\": \"o\", \"string\": \"o\"},\n" + |
| 94 | + " {\"fullyEncoded\": \"%70\", \"minimallyEncoded\": \"p\", \"string\": \"p\"},\n" + |
| 95 | + " {\"fullyEncoded\": \"%71\", \"minimallyEncoded\": \"q\", \"string\": \"q\"},\n" + |
| 96 | + " {\"fullyEncoded\": \"%72\", \"minimallyEncoded\": \"r\", \"string\": \"r\"},\n" + |
| 97 | + " {\"fullyEncoded\": \"%73\", \"minimallyEncoded\": \"s\", \"string\": \"s\"},\n" + |
| 98 | + " {\"fullyEncoded\": \"%74\", \"minimallyEncoded\": \"t\", \"string\": \"t\"},\n" + |
| 99 | + " {\"fullyEncoded\": \"%75\", \"minimallyEncoded\": \"u\", \"string\": \"u\"},\n" + |
| 100 | + " {\"fullyEncoded\": \"%76\", \"minimallyEncoded\": \"v\", \"string\": \"v\"},\n" + |
| 101 | + " {\"fullyEncoded\": \"%77\", \"minimallyEncoded\": \"w\", \"string\": \"w\"},\n" + |
| 102 | + " {\"fullyEncoded\": \"%78\", \"minimallyEncoded\": \"x\", \"string\": \"x\"},\n" + |
| 103 | + " {\"fullyEncoded\": \"%79\", \"minimallyEncoded\": \"y\", \"string\": \"y\"},\n" + |
| 104 | + " {\"fullyEncoded\": \"%7A\", \"minimallyEncoded\": \"z\", \"string\": \"z\"},\n" + |
| 105 | + " {\"fullyEncoded\": \"%7B\", \"minimallyEncoded\": \"%7B\", \"string\": \"{\"},\n" + |
| 106 | + " {\"fullyEncoded\": \"%7C\", \"minimallyEncoded\": \"%7C\", \"string\": \"|\"},\n" + |
| 107 | + " {\"fullyEncoded\": \"%7D\", \"minimallyEncoded\": \"%7D\", \"string\": \"}\"},\n" + |
| 108 | + " {\"fullyEncoded\": \"%7E\", \"minimallyEncoded\": \"~\", \"string\": \"~\"},\n" + |
| 109 | + " {\"fullyEncoded\": \"%7F\", \"minimallyEncoded\": \"%7F\", \"string\": \"\\u007f\"},\n" + |
| 110 | + " {\"fullyEncoded\": \"%E8%87%AA%E7%94%B1\", \"minimallyEncoded\": \"%E8%87%AA%E7%94%B1\", \"string\": \"\\u81ea\\u7531\"},\n" + |
| 111 | + " {\"fullyEncoded\": \"%F0%90%90%80\", \"minimallyEncoded\": \"%F0%90%90%80\", \"string\": \"\\ud801\\udc00\"}\n" + |
| 112 | + "]"; |
9 | 113 |
|
10 | 114 | @Test |
11 | | - public void testEncoding() { |
| 115 | + public void testEncoding() throws JSONException { |
12 | 116 | Helper.urlEncode(""); |
13 | | - assertNull(null); |
| 117 | + JSONArray jsonArray = new JSONArray(ENCODING_TEST_JSON); |
| 118 | + int length = jsonArray.length(); |
| 119 | + for(int i = 0; i< length; i++) { |
| 120 | + JSONObject jsonObject = jsonArray.getJSONObject(i); |
| 121 | + |
| 122 | + String fullyEncoded = jsonObject.getString("fullyEncoded"); |
| 123 | + String minimallyEncoded = jsonObject.getString("minimallyEncoded"); |
| 124 | + String string = jsonObject.getString("string"); |
| 125 | + |
| 126 | + // assert encoding |
| 127 | + String urlEncode = Helper.urlEncode(string); |
| 128 | + if(!(minimallyEncoded.equals(urlEncode) || fullyEncoded.equals(urlEncode))) { |
| 129 | + assertTrue("Invalid encoding, could not encode '" + |
| 130 | + string + |
| 131 | + "' to either '" + |
| 132 | + minimallyEncoded + |
| 133 | + "' or '" + |
| 134 | + fullyEncoded + |
| 135 | + "'. The encoded output was '" + |
| 136 | + "'" + |
| 137 | + urlEncode + |
| 138 | + "'.", |
| 139 | + false); |
| 140 | + } |
| 141 | + |
| 142 | + // assert decoding |
| 143 | + assertEquals(string, Helper.urlDecode(fullyEncoded)); |
| 144 | + assertEquals(string, Helper.urlDecode(minimallyEncoded)); |
| 145 | + |
| 146 | + } |
14 | 147 | } |
15 | 148 |
|
16 | 149 | } |
0 commit comments