Search in sources :

Example 1 with MkContainer

use of com.jcabi.http.mock.MkContainer 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();
}
Also used : MkGrizzlyContainer(com.jcabi.http.mock.MkGrizzlyContainer) JdkRequest(com.jcabi.http.request.JdkRequest) RestResponse(com.jcabi.http.response.RestResponse) MkAnswer(com.jcabi.http.mock.MkAnswer) MkContainer(com.jcabi.http.mock.MkContainer) Test(org.junit.Test)

Example 2 with MkContainer

use of com.jcabi.http.mock.MkContainer 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();
}
Also used : MkGrizzlyContainer(com.jcabi.http.mock.MkGrizzlyContainer) ApacheRequest(com.jcabi.http.request.ApacheRequest) MkContainer(com.jcabi.http.mock.MkContainer) Test(org.junit.Test)

Example 3 with MkContainer

use of com.jcabi.http.mock.MkContainer 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();
}
Also used : MkGrizzlyContainer(com.jcabi.http.mock.MkGrizzlyContainer) ApacheRequest(com.jcabi.http.request.ApacheRequest) MkContainer(com.jcabi.http.mock.MkContainer) Test(org.junit.Test)

Example 4 with MkContainer

use of com.jcabi.http.mock.MkContainer 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();
    }
}
Also used : MkGrizzlyContainer(com.jcabi.http.mock.MkGrizzlyContainer) ApacheRequest(com.jcabi.http.request.ApacheRequest) MkQuery(com.jcabi.http.mock.MkQuery) MkContainer(com.jcabi.http.mock.MkContainer) Test(org.junit.Test)

Example 5 with MkContainer

use of com.jcabi.http.mock.MkContainer 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();
    }
}
Also used : MkGrizzlyContainer(com.jcabi.http.mock.MkGrizzlyContainer) ApacheRequest(com.jcabi.http.request.ApacheRequest) MkQuery(com.jcabi.http.mock.MkQuery) MkContainer(com.jcabi.http.mock.MkContainer) Test(org.junit.Test)

Aggregations

MkContainer (com.jcabi.http.mock.MkContainer)136 MkGrizzlyContainer (com.jcabi.http.mock.MkGrizzlyContainer)136 Test (org.junit.Test)136 ApacheRequest (com.jcabi.http.request.ApacheRequest)105 MkGithub (com.jcabi.github.mock.MkGithub)40 MkQuery (com.jcabi.http.mock.MkQuery)34 MkAnswer (com.jcabi.http.mock.MkAnswer)32 JdkRequest (com.jcabi.http.request.JdkRequest)31 JsonObject (javax.json.JsonObject)19 Request (com.jcabi.http.Request)5 JsonArray (javax.json.JsonArray)3 FakeRequest (com.jcabi.http.request.FakeRequest)2 ArrayMap (com.jcabi.immutable.ArrayMap)2 InputStream (java.io.InputStream)2 RestResponse (com.jcabi.http.response.RestResponse)1 StringReader (java.io.StringReader)1 EnumMap (java.util.EnumMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1