@Test public void failedLogin() throws MalformedURLException { // login response handler .getConnection() .setMappedData("/login/api", "{\"error\":\"login\", \"reason\":\"test\"}"); try { Sprint s = new Sprint("user", "public-key", "localhost", 9295); s.setUrl(new URL("http://example.com")); s.addListener( new ISprintListener() { public boolean onData(SprintResult result) { // fail if we get a ok message assertFalse(true); return true; } }); s.execute(); } catch (AuthenticationException ex) { assertNotNull(ex); assertEquals("login", ex.getError()); assertEquals("test", ex.getReason()); } finally { assertEquals(handler.getConnection().getHeaders().get("X-API-Key"), "public-key"); } }
@Test public void failedToQueue() throws MalformedURLException { // login response handler .getConnection() .setMappedData("/login/api", "{\"ok\":true, \"api_key\":\"private-key\"}"); // execute response handler .getConnection() .setMappedData( "/api/1/curl/execute", "{\"error\":\"throttle\", \"reason\":\"Slow down please!\"}"); try { Sprint s = new Sprint("user", "public-key", "localhost", 9295); s.setUrl(new URL("http://example.com")); s.addListener( new ISprintListener() { public boolean onData(SprintResult result) { // fail if we get a ok message assertFalse(true); return true; } }); s.execute(); } catch (BlitzException ex) { assertNotNull(ex); assertEquals("throttle", ex.getError()); assertEquals("Slow down please!", ex.getReason()); } finally { assertEquals(handler.getConnection().getHeaders().get("X-API-Key"), "private-key"); String output = handler.getConnection().getOutputStreamAsString("UTF-8"); assertEquals(output, "{\"url\":\"http://example.com\"}"); } }
@Test public void abort() throws MalformedURLException, InterruptedException { // abort response handler .getConnection() .setMappedData("/api/1/jobs/a123/abort", "{\"_id\":\"a123\",\"ok\":true}"); // login response handler .getConnection() .setMappedData("/login/api", "{\"ok\":true, \"api_key\":\"private-key\"}"); // execute response handler .getConnection() .setMappedData( "/api/1/curl/execute", "{\"ok\":true, \"status\":\"queued\", " + "\"region\":\"california\", \"job_id\":\"a123\"}"); // job_status response handler .getConnection() .setMappedData( "/api/1/jobs/a123/status", "{\"_id\":\"a123\",\"ok\":true, \"status\":\"completed\"," + "\"result\":{\"region\":\"california\",\"duration\":10,\"connect\":1," + "\"request\":{\"line\":\"GET / HTTP/1.1\",\"method\":\"GET\"," + "\"url\":\"http://localhost:9295\",\"headers\":{\"a\":\"b\"},\"content\":\"abc\"}," + "\"response\":{\"line\":\"GET / HTTP/1.1\",\"message\":\"message\"," + "\"status\":200,\"headers\":{\"c\":\"d\"},\"content\":\"abd\"}}}"); Sprint s = new Sprint("user", "public-key", "localhost", 9295); s.setUrl(new URL("http://example.com")); s.addListener( new ISprintListener() { public boolean onData(SprintResult result) { assertNotNull(result); assertNotNull(result.getRequest()); assertNotNull(result.getResponse()); assertEquals("california", result.getRegion()); assertEquals("GET", result.getRequest().getMethod()); assertEquals(new Integer(200), result.getResponse().getStatus()); assertNotNull(result.getRequest().getHeaders()); assertEquals(1, result.getRequest().getHeaders().size()); assertNotNull(result.getRequest().getHeaders().get("a")); assertEquals("b", result.getRequest().getHeaders().get("a")); return false; } }); s.execute(); assertEquals(handler.getConnection().getHeaders().get("X-API-Key"), "private-key"); String output = handler.getConnection().getOutputStreamAsString("UTF-8"); assertEquals(output, "{\"url\":\"http://example.com\"}"); }
@Test public void failedUrlValidation() { try { Sprint s = new Sprint("user", "public-key", "localhost", 9295); s.addListener( new ISprintListener() { public boolean onData(SprintResult result) { // fail if we get a ok message assertFalse(true); return true; } }); s.execute(); } catch (ValidationException ex) { assertNotNull(ex); assertEquals("validation", ex.getError()); assertEquals("Url is required", ex.getReason()); } }