Skip to content

Commit 2294e0f

Browse files
author
jonschmidt
committed
Merge remote-tracking branch 'origin' into feature/client-id-for-unauthorized-requests
Conflicts: src/main/java/com/soundcloud/api/CloudAPI.java
2 parents 63ebcc2 + a00f8d0 commit 2294e0f

5 files changed

Lines changed: 27 additions & 12 deletions

File tree

src/main/java/com/soundcloud/api/CloudAPI.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public interface CloudAPI {
3434
// custom
3535
String OAUTH1_TOKEN_GRANT_TYPE = "oauth1_token"; // soundcloud
3636
String FACEBOOK_GRANT_TYPE = "urn:soundcloud:oauth2:grant-type:facebook&access_token="; // oauth2 extension
37+
String GOOGLE_PLUS_GRANT_TYPE = "urn:soundcloud:oauth2:grant-type:google_plus&access_token=";
3738

3839
// other constants
3940
String REALM = "SoundCloud";

src/main/java/com/soundcloud/api/Request.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
* </code>
4848
*/
4949
public class Request implements Iterable<NameValuePair> {
50+
public static final String UTF_8 = "UTF-8";
51+
5052
private List<NameValuePair> mParams = new ArrayList<NameValuePair>(); // XXX should probably be lazy
5153
private Map<String, Attachment> mFiles;
5254

@@ -82,10 +84,10 @@ public Request(String resource) {
8284
try {
8385
if (kv.length == 2) {
8486
mParams.add(new BasicNameValuePair(
85-
URLDecoder.decode(kv[0], "UTF-8"),
86-
URLDecoder.decode(kv[1], "UTF-8")));
87+
URLDecoder.decode(kv[0], UTF_8),
88+
URLDecoder.decode(kv[1], UTF_8)));
8789
} else if (kv.length == 1) {
88-
mParams.add(new BasicNameValuePair(URLDecoder.decode(kv[0], "UTF-8"), null));
90+
mParams.add(new BasicNameValuePair(URLDecoder.decode(kv[0], UTF_8), null));
8991
}
9092
} catch (UnsupportedEncodingException ignored) {
9193
}
@@ -215,7 +217,7 @@ public int size() {
215217
* list of parameters in an HTTP PUT or HTTP POST.
216218
*/
217219
public String queryString() {
218-
return format(mParams, "UTF-8");
220+
return format(mParams, UTF_8);
219221
}
220222

221223
/**
@@ -317,7 +319,7 @@ public Request withEntity(HttpEntity entity) {
317319
*/
318320
public Request withContent(String content, String contentType) {
319321
try {
320-
StringEntity stringEntity = new StringEntity(content);
322+
StringEntity stringEntity = new StringEntity(content, UTF_8);
321323
if (contentType != null) {
322324
stringEntity.setContentType(contentType);
323325
}
@@ -377,7 +379,7 @@ public <T extends HttpRequestBase> T buildRequest(Class<T> method) {
377379
HttpEntityEnclosingRequestBase enclosingRequest =
378380
(HttpEntityEnclosingRequestBase) request;
379381

380-
final Charset charSet = java.nio.charset.Charset.forName("UTF-8");
382+
final Charset charSet = java.nio.charset.Charset.forName(UTF_8);
381383
if (isMultipart()) {
382384
MultipartEntity multiPart = new MultipartEntity(
383385
HttpMultipartMode.BROWSER_COMPATIBLE, // XXX change this to STRICT once rack on server is upgraded

src/main/java/com/soundcloud/api/Stream.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ private static long getExpires(String resource) {
107107
String[] kv = s.split("=", 2);
108108
if (kv != null && kv.length == 2) {
109109
try {
110-
String name = URLDecoder.decode(kv[0], "UTF-8");
110+
String name = URLDecoder.decode(kv[0], Request.UTF_8);
111111
if (EXPIRES.equalsIgnoreCase(name)) {
112-
String value = URLDecoder.decode(kv[1], "UTF-8");
112+
String value = URLDecoder.decode(kv[1], Request.UTF_8);
113113
try {
114114
return Long.parseLong(value) * 1000L;
115115
} catch (NumberFormatException ignored) {

src/test/java/com/soundcloud/api/CloudAPIIntegrationTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public void shouldCreateAPlaylistAndAddTracksToItWithJSON() throws Exception {
142142
String playlistUrl = location.getValue();
143143
assertNotNull(playlistUrl);
144144

145-
String title = "a new title:" + System.currentTimeMillis();
145+
String title = "a new tîtle:" + System.currentTimeMillis();
146146
JSONObject json = createJSONPlaylist(title, CHE_FLUTE_TRACK_ID, FLICKERMOOD_TRACK_ID);
147147

148148
resp = api.put(Request.to(playlistUrl)
@@ -184,9 +184,8 @@ public void shouldNotGetASignupTokenWhenInofficialApp() throws Exception {
184184

185185
@Test(expected = CloudAPI.InvalidTokenException.class)
186186
public void shouldGetATokenUsingExtensionGrantTypes() throws Exception {
187-
// TODO
188-
String fbToken = "fbToken";
189-
api.extensionGrantType(CloudAPI.FACEBOOK_GRANT_TYPE +fbToken);
187+
// TODO ?
188+
api.extensionGrantType(CloudAPI.FACEBOOK_GRANT_TYPE + "fbToken");
190189
}
191190

192191
@Test

src/test/java/com/soundcloud/api/RequestTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,19 @@ public void shouldIncludeContentInRequest() throws Exception {
276276
assertThat("<foo><baz>content</baz></foo>", equalTo(body));
277277
}
278278

279+
@Test
280+
public void shouldUseUTF8AsDefaultEncodingForStringPayloads() throws Exception {
281+
HttpPost request = Request.to("/too")
282+
.withContent("{ string:\"îøüöéí\" }", "application/json")
283+
.buildRequest(HttpPost.class);
284+
285+
ByteArrayOutputStream os = new ByteArrayOutputStream();
286+
request.getEntity().writeTo(os);
287+
288+
String decoded = os.toString("UTF-8");
289+
assertThat("{ string:\"îøüöéí\" }", equalTo(decoded));
290+
}
291+
279292
@Test
280293
public void shouldBuildARequestWithContentAndPreserveQueryParameters() throws Exception {
281294
HttpPost post = Request

0 commit comments

Comments
 (0)