Exemple #1
0
  @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");
    }
  }
Exemple #2
0
  @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\"}");
    }
  }
Exemple #3
0
  @Test
  public void successful() throws MalformedURLException, InterruptedException {
    // 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 true;
          }
        });
    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\"}");
  }
Exemple #4
0
 @BeforeClass
 public static void mockSetup() {
   handler = CurlTestSuite.getHandler();
   factory = CurlTestSuite.getFactory();
   try {
     URL.setURLStreamHandlerFactory(factory);
   } catch (Error e) {
   }
   handler.getConnection().setUseMapped(true);
 }
Exemple #5
0
 @Before
 public void resetMock() {
   handler.getConnection().setHeaders(new HashMap<String, String>());
   handler.getConnection().setOutput(new ByteArrayOutputStream());
 }