use of com.jcabi.http.mock.MkGrizzlyContainer in project jcabi-github by jcabi.
the class RetryCarefulWireTest method makesMultipleRequestsAndWaitUntilReset.
/**
* RetryCarefulWire can make a few requests before giving up and
* can wait until the rate limit resets.
* @throws Exception If something goes wrong inside
*/
@Test
public void makesMultipleRequestsAndWaitUntilReset() throws Exception {
final int threshold = 10;
// @checkstyle MagicNumber (2 lines)
final long reset = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()) + 5L;
final MkContainer container = new MkGrizzlyContainer().next(new MkAnswer.Simple(HttpURLConnection.HTTP_INTERNAL_ERROR)).next(new MkAnswer.Simple(HttpURLConnection.HTTP_INTERNAL_ERROR)).next(new MkAnswer.Simple(HttpURLConnection.HTTP_OK).withHeader(REMAINING_HEADER, "9").withHeader("X-RateLimit-Reset", String.valueOf(reset))).start();
new JdkRequest(container.home()).through(RetryCarefulWire.class, threshold).fetch().as(RestResponse.class).assertStatus(HttpURLConnection.HTTP_OK);
final long now = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
MatcherAssert.assertThat(now, Matchers.greaterThanOrEqualTo(reset));
container.stop();
}
use of com.jcabi.http.mock.MkGrizzlyContainer in project jcabi-github by jcabi.
the class RtUsersTest method iterateUsers.
/**
* RtUsers can iterate users.
* @throws Exception if there is any error
*/
@Test
public void iterateUsers() throws Exception {
final String identifier = "1";
final MkContainer container = new MkGrizzlyContainer().next(new MkAnswer.Simple(HttpURLConnection.HTTP_OK, Json.createArrayBuilder().add(RtUsersTest.json("octocat", identifier)).add(RtUsersTest.json("dummy", "2")).build().toString())).start();
final Users users = new RtUsers(Mockito.mock(Github.class), new ApacheRequest(container.home()));
MatcherAssert.assertThat(users.iterate(identifier), Matchers.<User>iterableWithSize(2));
container.stop();
}
use of com.jcabi.http.mock.MkGrizzlyContainer in project jcabi-github by jcabi.
the class RtUsersTest method getCurrentUser.
/**
* RtUsers can get a current user.
*
* @throws Exception if there is any error
*/
@Test
public void getCurrentUser() throws Exception {
final String login = "kendy";
final MkContainer container = new MkGrizzlyContainer().next(new MkAnswer.Simple(HttpURLConnection.HTTP_OK, RtUsersTest.json(login, "4").toString())).start();
final Users users = new RtUsers(Mockito.mock(Github.class), new ApacheRequest(container.home()));
MatcherAssert.assertThat(users.self().login(), Matchers.equalTo(login));
container.stop();
}
use of com.jcabi.http.mock.MkGrizzlyContainer in project jcabi-github by jcabi.
the class RtPublicKeysTest method canRemoveKey.
/**
* RtPublicKeys should be able to remove a key.
*
* @throws Exception if a problem occurs.
*/
@Test
public void canRemoveKey() throws Exception {
final MkContainer container = new MkGrizzlyContainer().next(new MkAnswer.Simple(HttpURLConnection.HTTP_NO_CONTENT, "")).start();
final RtPublicKeys keys = new RtPublicKeys(new ApacheRequest(container.home()), Mockito.mock(User.class));
try {
keys.remove(1);
final MkQuery query = container.take();
MatcherAssert.assertThat(query.uri().toString(), Matchers.endsWith("/user/keys/1"));
MatcherAssert.assertThat(query.method(), Matchers.equalTo(Request.DELETE));
} finally {
container.stop();
}
}
use of com.jcabi.http.mock.MkGrizzlyContainer in project jcabi-github by jcabi.
the class RtPublicKeysTest method canCreatePublicKey.
/**
* RtPublicKeys can create a key.
* @throws IOException If some problem inside.
*/
@Test
public void canCreatePublicKey() throws IOException {
final MkContainer container = new MkGrizzlyContainer().next(new MkAnswer.Simple(HttpURLConnection.HTTP_CREATED, key(1).toString())).start();
try {
final RtPublicKeys keys = new RtPublicKeys(new ApacheRequest(container.home()), Mockito.mock(User.class));
MatcherAssert.assertThat(keys.create("theTitle", "theKey").number(), Matchers.is(1));
final MkQuery query = container.take();
MatcherAssert.assertThat(query.uri().toString(), Matchers.endsWith("/user/keys"));
MatcherAssert.assertThat(query.body(), Matchers.equalTo("{\"title\":\"theTitle\",\"key\":\"theKey\"}"));
} finally {
container.stop();
}
}
Aggregations