@Test
  public void testExtractHeadersAllCommonHeaders() throws Exception {

    final RequestType request1 = m_httpRecording.addRequest(m_connectionDetails1, "GET", "/path");

    request1.setHeaders(
        createHeaders(
            new NVPair("foo", "bah"), new NVPair("User-Agent", "blah"), new NVPair("Accept", "x")));
    request1.addNewResponse();

    final RequestType request2 = m_httpRecording.addRequest(m_connectionDetails1, "GET", "/path");

    request2.setHeaders(
        createHeaders(
            new NVPair("foo", "bah"), new NVPair("User-Agent", "blah"), new NVPair("Accept", "x")));
    request2.addNewResponse();

    m_httpRecording.dispose();

    verify(m_resultProcessor).process(m_recordingCaptor.capture());

    final HTTPRecordingType recording = m_recordingCaptor.getValue().getHttpRecording();

    assertEquals(1, recording.getCommonHeadersArray().length);

    final RequestType request = recording.getPageArray(0).getRequestArray(0);
    final HeadersType headers = request.getHeaders();
    assertEquals("headers0", headers.getExtends());
    assertEquals(1, headers.sizeOfHeaderArray());
  }
  @Test
  public void testAddRequestWithComplexPaths() throws Exception {
    // Request 1
    final RequestType request1 =
        m_httpRecording.addRequest(
            m_connectionDetails1, "GET", "/path;name=value/blah;dah/foo?foo=y");

    assertEquals("GET", request1.getMethod().toString());
    assertEquals("GET foo", request1.getDescription());
    assertEquals("/path;", request1.getUri().getPath().getTextArray(0));
    assertEquals("token_name", request1.getUri().getPath().getTokenReferenceArray(0).getTokenId());
    assertEquals("/blah;dah/foo", request1.getUri().getPath().getTextArray(1));
    assertEquals(0, request1.getUri().getQueryString().getTextArray().length);
    assertEquals("y", request1.getUri().getQueryString().getTokenReferenceArray(0).getNewValue());
    assertFalse(request1.getUri().isSetFragment());

    final RequestType request2 =
        m_httpRecording.addRequest(m_connectionDetails1, "POST", "/?x=y&fo--o=bah#lah?;blah");

    assertEquals("POST", request2.getMethod().toString());
    assertEquals("POST /", request2.getDescription());
    assertEquals("/", request2.getUri().getPath().getTextArray(0));
    assertEquals(0, request2.getUri().getPath().getTokenReferenceArray().length);
    assertArrayEquals(new String[] {"&"}, request2.getUri().getQueryString().getTextArray());
    assertEquals(
        "token_foo2", request2.getUri().getQueryString().getTokenReferenceArray(1).getTokenId());
    assertEquals("bah", request2.getUri().getQueryString().getTokenReferenceArray(1).getNewValue());
    assertEquals("lah?;blah", request2.getUri().getFragment());

    final RequestType request3 =
        m_httpRecording.addRequest(m_connectionDetails1, "POST", "/?x=y&fo--o=bah#lah?;blah");

    assertArrayEquals(new String[] {"&"}, request3.getUri().getQueryString().getTextArray());
    assertEquals(
        "token_foo2", request3.getUri().getQueryString().getTokenReferenceArray(1).getTokenId());
    assertFalse(request3.getUri().getQueryString().getTokenReferenceArray(1).isSetNewValue());
    assertEquals("lah?;blah", request3.getUri().getFragment());
  }
  @Test
  public void testAddRequestWithHeaders() throws Exception {

    final RequestType request1 = m_httpRecording.addRequest(m_connectionDetails1, "GET", "/path");

    request1.setHeaders(
        createHeaders(
            new NVPair("foo", "bah"), new NVPair("User-Agent", "blah"), new NVPair("Accept", "x")));
    request1.addNewResponse();

    final RequestType request2 = m_httpRecording.addRequest(m_connectionDetails1, "GET", "/path");

    request2.setHeaders(
        createHeaders(
            new NVPair("fu", "bar"), new NVPair("User-Agent", "blah"), new NVPair("Accept", "y")));
    request2.addNewResponse();

    final RequestType request3 = m_httpRecording.addRequest(m_connectionDetails1, "GET", "/path");

    request3.setHeaders(
        createHeaders(
            new NVPair("fu", "bar"), new NVPair("User-Agent", "blah"), new NVPair("Accept", "y")));
    request3.addNewResponse();

    final RequestType request4 = m_httpRecording.addRequest(m_connectionDetails1, "GET", "/path");

    request4.setHeaders(createHeaders(new NVPair("User-Agent", "blah"), new NVPair("Accept", "z")));

    final BasicAuthorizationHeaderType basicAuthorizationHeaderType =
        request4.getHeaders().addNewAuthorization().addNewBasic();
    basicAuthorizationHeaderType.setUserid("phil");
    basicAuthorizationHeaderType.setPassword("abracaduh");
    request4.addNewResponse();

    // The next two requests trigger the case where there is
    // common header set that matches the default headers.
    final RequestType request5 = m_httpRecording.addRequest(m_connectionDetails1, "GET", "/path");

    request5.setHeaders(createHeaders(new NVPair("User-Agent", "blah")));
    request5.addNewResponse();

    final RequestType request6 = m_httpRecording.addRequest(m_connectionDetails1, "GET", "/path");

    request6.setHeaders(createHeaders(new NVPair("User-Agent", "blah")));
    request6.addNewResponse();

    // Request with no response.
    final RequestType request7 = m_httpRecording.addRequest(m_connectionDetails1, "GET", "/path");

    request7.setHeaders(createHeaders(new NVPair("User-Agent", "blah"), new NVPair("Accept", "z")));

    final RequestType request8 = m_httpRecording.addRequest(m_connectionDetails1, "GET", "/path");

    request8.setHeaders(
        createHeaders(new NVPair("User-Agent", "blah"), new NVPair("Accept", "zz")));
    request8.addNewResponse();

    final RequestType request9 = m_httpRecording.addRequest(m_connectionDetails1, "GET", "/path");

    request9.setHeaders(
        createHeaders(new NVPair("User-Agent", "blah"), new NVPair("Accept", "zz")));
    request9.addNewResponse();

    m_httpRecording.dispose();

    verify(m_resultProcessor).process(m_recordingCaptor.capture());

    final HTTPRecordingType recording = m_recordingCaptor.getValue().getHttpRecording();

    // Default, plus 2 sets.
    assertEquals(3, recording.getCommonHeadersArray().length);

    final CommonHeadersType defaultHeaders = recording.getCommonHeadersArray(0);
    assertEquals(0, defaultHeaders.getAuthorizationArray().length);
    assertEquals(1, defaultHeaders.getHeaderArray().length);
    assertEquals("User-Agent", defaultHeaders.getHeaderArray(0).getName());

    final CommonHeadersType commonHeaders1 = recording.getCommonHeadersArray(1);
    assertEquals(defaultHeaders.getHeadersId(), commonHeaders1.getExtends());
    assertEquals(1, commonHeaders1.getHeaderArray().length);
    assertEquals(0, commonHeaders1.getAuthorizationArray().length);

    assertEquals(
        "defaultHeaders", recording.getPageArray(0).getRequestArray(0).getHeaders().getExtends());

    final CommonHeadersType commonHeaders2 = recording.getCommonHeadersArray(2);
    assertEquals(defaultHeaders.getHeadersId(), commonHeaders2.getExtends());
    assertEquals(1, commonHeaders2.getHeaderArray().length);
    assertEquals("zz", commonHeaders2.getHeaderArray(0).getValue());
    assertEquals(0, commonHeaders2.getAuthorizationArray().length);

    final HeadersType headers = recording.getPageArray(3).getRequestArray(0).getHeaders();
    assertEquals(1, headers.getHeaderArray().length);
    assertEquals(1, headers.getAuthorizationArray().length);
    assertEquals("phil", headers.getAuthorizationArray(0).getBasic().getUserid());
  }
  @Test
  public void testAddRequest() throws Exception {
    final EndPoint endPoint3 = new EndPoint("hostC", 80);
    final String[] userComments =
        new String[] {
          "BEGIN ENTER gmail homepage",
          "END ENTER gmail homepage",
          "BEGIN CLICK Sign In",
          "END CLICK Sign In",
        };

    // Request 1
    final RequestType request1 = m_httpRecording.addRequest(m_connectionDetails1, "GET", "/");
    for (final String userComment : userComments) {
      request1.addComment(userComment);
    }
    assertEquals("/", request1.getUri().getUnparsed());
    assertEquals("GET", request1.getMethod().toString());
    assertEquals("GET /", request1.getDescription());
    assertArrayEquals(userComments, request1.getCommentArray());
    assertEquals("END CLICK Sign In", request1.getCommentArray(3));
    assertFalse(request1.isSetSleepTime());
    request1.addNewResponse();
    m_httpRecording.markLastResponseTime();

    // Request 2
    final ConnectionDetails connectionDetails2 = new ConnectionDetails(endPoint1, endPoint2, false);

    final RequestType request2 = m_httpRecording.addRequest(connectionDetails2, "GET", "/foo.gif");
    assertFalse(request2.isSetSleepTime());
    request2.addNewResponse();
    m_httpRecording.markLastResponseTime();
    Thread.sleep(20);

    // Request 3
    final ConnectionDetails connectionDetails3 = new ConnectionDetails(endPoint3, endPoint2, true);

    final RequestType request3 = m_httpRecording.addRequest(connectionDetails3, "GET", "bah.gif");
    assertEquals("bah.gif", request3.getUri().getUnparsed());
    assertTrue(request3.isSetSleepTime());
    request3.addNewResponse().setStatusCode(302);
    assertFalse(request3.isSetAnnotation());

    final RequestType request4 = m_httpRecording.addRequest(connectionDetails3, "GET", "bah.gif");
    request4.addNewResponse().setStatusCode(301);
    assertFalse(request4.isSetAnnotation());

    final RequestType request5 = m_httpRecording.addRequest(connectionDetails3, "GET", "bah.gif");
    request5.addNewResponse().setStatusCode(307);
    assertFalse(request5.isSetAnnotation());

    // Ignored because it doesn't have a response.
    m_httpRecording.addRequest(connectionDetails3, "GET", "bah.gif");

    m_httpRecording.dispose();

    verify(m_resultProcessor).process(m_recordingCaptor.capture());

    final HttpRecordingDocument recording = m_recordingCaptor.getValue();

    XMLBeansUtilities.validate(recording);

    verifyNoMoreInteractions(m_resultProcessor);

    final HTTPRecordingType result = recording.getHttpRecording();
    assertEquals(0, result.getCommonHeadersArray().length);

    assertEquals(2, result.getBaseUriArray().length);
    assertEquals("hostb", result.getBaseUriArray(0).getHost());
    assertEquals("https", result.getBaseUriArray(1).getScheme().toString());

    assertEquals(2, result.getPageArray().length);

    final PageType page0 = result.getPageArray(0);
    assertEquals(2, page0.getRequestArray().length);
    assertEquals(
        result.getBaseUriArray(0).getUriId(), page0.getRequestArray(1).getUri().getExtends());
    assertEquals("/foo.gif", page0.getRequestArray(1).getUri().getPath().getTextArray(0));
    assertFalse(page0.getRequestArray(1).isSetAnnotation());

    final PageType page1 = result.getPageArray(1);
    assertEquals(3, page1.getRequestArray().length);
    assertEquals(0, page1.getRequestArray(0).getHeaders().sizeOfHeaderArray());
    assertTrue(page1.getRequestArray(0).isSetAnnotation());
    assertTrue(page1.getRequestArray(1).isSetAnnotation());
    assertTrue(page1.getRequestArray(2).isSetAnnotation());
  }