mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 20:33:53 +01:00
Fix implementation of getUploadCount() and its test
This commit is contained in:
parent
d1e53db73a
commit
946f4f2ac2
2 changed files with 19 additions and 4 deletions
|
|
@ -3,6 +3,7 @@ package fr.free.nrw.commons.mwapi;
|
|||
import android.os.Build;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
|
|
@ -41,6 +42,8 @@ import timber.log.Timber;
|
|||
* @author Addshore
|
||||
*/
|
||||
public class ApacheHttpClientMediaWikiApi implements MediaWikiApi {
|
||||
private String wikiMediaToolforgeUrl = "https://tools.wmflabs.org/";
|
||||
|
||||
private static final String THUMB_SIZE = "640";
|
||||
private AbstractHttpClient httpClient;
|
||||
private MWApi api;
|
||||
|
|
@ -57,6 +60,11 @@ public class ApacheHttpClientMediaWikiApi implements MediaWikiApi {
|
|||
api = new MWApi(apiURL, httpClient);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public void setWikiMediaToolforgeUrl(String wikiMediaToolforgeUrl) {
|
||||
this.wikiMediaToolforgeUrl = wikiMediaToolforgeUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param username String
|
||||
* @param password String
|
||||
|
|
@ -376,14 +384,16 @@ public class ApacheHttpClientMediaWikiApi implements MediaWikiApi {
|
|||
@NonNull
|
||||
public Single<Integer> getUploadCount(String userName) {
|
||||
final String uploadCountUrlTemplate =
|
||||
"https://tools.wmflabs.org/urbanecmbot/uploadsbyuser/uploadsbyuser.py?user=%s";
|
||||
wikiMediaToolforgeUrl + "urbanecmbot/uploadsbyuser/uploadsbyuser.py";
|
||||
|
||||
return Single.fromCallable(() -> {
|
||||
String url = String.format(
|
||||
Locale.ENGLISH,
|
||||
uploadCountUrlTemplate,
|
||||
new PageTitle(userName).getText());
|
||||
HttpResponse response = Http.get(url).use(httpClient).asResponse();
|
||||
HttpResponse response = Http.get(url).use(httpClient)
|
||||
.data("user", userName)
|
||||
.asResponse();
|
||||
String uploadCount = EntityUtils.toString(response.getEntity()).trim();
|
||||
return Integer.parseInt(uploadCount);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -41,6 +41,7 @@ public class ApacheHttpClientMediaWikiApiTest {
|
|||
public void setUp() throws Exception {
|
||||
server = new MockWebServer();
|
||||
testObject = new ApacheHttpClientMediaWikiApi("http://" + server.getHostName() + ":" + server.getPort() + "/");
|
||||
testObject.setWikiMediaToolforgeUrl("http://" + server.getHostName() + ":" + server.getPort() + "/");
|
||||
}
|
||||
|
||||
@After
|
||||
|
|
@ -198,10 +199,14 @@ public class ApacheHttpClientMediaWikiApiTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void getUploadCount() {
|
||||
public void getUploadCount() throws InterruptedException {
|
||||
server.enqueue(new MockResponse().setBody("23\n"));
|
||||
|
||||
TestObserver<Integer> testObserver = testObject.getUploadCount("username").test();
|
||||
TestObserver<Integer> testObserver = testObject.getUploadCount("testUsername").test();
|
||||
|
||||
RecordedRequest request = server.takeRequest();
|
||||
Map<String, String> params = parseQueryParams(request);
|
||||
assertEquals("testUsername", params.get("user"));
|
||||
|
||||
assertEquals(1, testObserver.valueCount());
|
||||
assertEquals(23, (int)testObserver.values().get(0));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue