Ejemplo n.º 1
0
  /**
   * Tests that a PageHeaderIterator correctly retrieves ETag headers for 3 pages from a mocked
   * server that conform to GitHub API's pagination specifications and terminates afterwards.
   *
   * @throws NoSuchElementException
   */
  @Test
  public void testHeaderIterator() throws NoSuchElementException, IOException {
    MockServerClient mockServer = ClientAndServer.startClientAndServer(8888);

    HttpRequest page1Request = createMockServerPagedHeaderRequest(1);
    List<Header> page1Headers =
        TestUtils.parseHeaderRecord(
            TestUtils.readFileFromResource(this, "tests/PagedHeadersSample/page1-header.txt"));
    String page1Etag = "aaf65fc6b10d5afbdc9cd0aa6e6ada4c";

    HttpRequest page2Request = createMockServerPagedHeaderRequest(2);
    List<Header> page2Headers =
        TestUtils.parseHeaderRecord(
            TestUtils.readFileFromResource(this, "tests/PagedHeadersSample/page2-header.txt"));
    String page2Etag = "731501e0f7d9816305782bc4c3f70d9f";

    HttpRequest page3Request = createMockServerPagedHeaderRequest(3);
    List<Header> page3Headers =
        TestUtils.parseHeaderRecord(
            TestUtils.readFileFromResource(this, "tests/PagedHeadersSample/page3-header.txt"));
    String page3Etag = "a6f367d674155d6fbbacbc2fca04917b";

    setUpHeadRequestOnMockServer(mockServer, page1Request, page1Headers);
    setUpHeadRequestOnMockServer(mockServer, page2Request, page2Headers);
    setUpHeadRequestOnMockServer(mockServer, page3Request, page3Headers);

    PagedRequest<Milestone> request = new PagedRequest<>();
    Map<String, String> params = new HashMap<>();
    params.put("state", "all");

    GitHubClientEx client = new GitHubClientEx("localhost", 8888, "http");

    String path = SEGMENT_REPOS + "/hubturbo/hubturbo" + SEGMENT_PULLS;
    request.setUri(path);
    request.setResponseContentType(CONTENT_TYPE_JSON);
    request.setParams(params);

    PageHeaderIterator iter = new PageHeaderIterator(request, client, "ETag");
    assertEquals(page1Etag, Utility.stripQuotes(iter.next()));
    assertEquals(page2Etag, Utility.stripQuotes(iter.next()));
    assertEquals(page3Etag, Utility.stripQuotes(iter.next()));
    assertFalse(iter.hasNext());

    mockServer.stop();
  }
Ejemplo n.º 2
0
 private void setUpHeadRequestOnMockServer(
     MockServerClient mockServer, HttpRequest request, List<Header> headers) {
   mockServer.when(request).respond(response().withHeaders(headers));
 }