@Ignore @Test public void responseCodesIn400RangeAreConsideredBad() throws IOException, InterruptedException { final String url = "http://github.com/TantalumMobile"; /* * Setup test */ // Return a unauthorized response code, no response body when(platformUtils.getHttpGetConn(eq(url), any(Vector.class), any(Vector.class))) .thenReturn(httpConn); when(httpConn.getResponseCode()).thenReturn(HttpGetter.HTTP_401_UNAUTHORIZED); when(httpConn.getLength()).thenReturn(0L); /* * Execute */ final Object returnValue = getter.exec(url); /* * Assert */ verify(httpConn).close(); assertEquals("Return null when get is unsuccessful", null, returnValue); assertEquals(HttpGetter.HTTP_401_UNAUTHORIZED, getter.getResponseCode()); assertTrue("Task was not correctly cancelled after error", cancelCalled); }
/** * @return pair<uuid of server, last updated time on server - i.e. FROM time for uploading local * tasks> * @throws Exception */ private Pair<String, Long> getRemoteInfo() throws Exception { String uuid; URI uri = new URI("http", null, host, port, "/get_uuid", null, null); HashMap<String, String> headers = new HashMap<String, String>(); headers.put("Uuid", localUuid); String content = httpGetter.getResponse(uri, headers, null); // Drill into the JSON response to find the content body JSONObject resp = (JSONObject) new JSONTokener(content).nextValue(); uuid = resp.getString("uuid"); Long lastUpdated = resp.getLong("lastUpdated"); inform("Server UUID:last updated" + uuid + ":" + lastUpdated.toString(), Events.TEXT); Pair<String, Long> result = new Pair<String, Long>(uuid, lastUpdated); return result; }
private void processRemoteUpdates(Long fromTime) throws Exception { URI uri = new URI("http", null, host, port, "/get_updates", "fromTime=" + fromTime.toString(), null); String content = httpGetter.getResponse(uri, null, null); // Drill into the JSON response to find the content body JSONObject updates = (JSONObject) new JSONTokener(content).nextValue(); Log.d(TAG, "Got JSON from server:"); Log.d(TAG, updates.toString()); if (updates.has("tasks")) { JSONArray tasks = updates.getJSONArray("tasks"); for (int i = 0; i < tasks.length(); ++i) { JSONObject json = tasks.getJSONObject(i); Log.d(TAG, "get task JSON"); Log.d(TAG, json.toString()); Task task = new Task(); task.fromJson(json); updateTask(task); } } }
/** * It's valid to call exec with a non-string key, but we'd better blow up already before casting a * random object to String, or converting it using toString() */ @Test public void canceledWhenNonStringKey() throws InterruptedException { getter.exec(new Object()); assertEquals( "Getter was not correctly cancelled for non-String url", getter.getStatus(), Task.CANCELED); }