@Test
  public void testTransferResponse() throws Exception {

    // Exception

    MockRegistrationReference mockRegistrationReference =
        new MockRegistrationReference(new MockIntraband());

    HttpClientSPIAgent httpClientSPIAgent =
        new HttpClientSPIAgent(_spiConfiguration, mockRegistrationReference);

    MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();

    mockHttpServletRequest.setAttribute(
        WebKeys.SPI_AGENT_REQUEST, new SPIAgentRequest(_mockHttpServletRequest));

    RecordSPIAgentResponse recordSPIAgentResponse = new RecordSPIAgentResponse();

    mockHttpServletRequest.setAttribute(WebKeys.SPI_AGENT_RESPONSE, recordSPIAgentResponse);

    MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();

    mockHttpServletRequest.setAttribute(
        WebKeys.SPI_AGENT_ORIGINAL_RESPONSE, mockHttpServletResponse);

    Exception exception = new Exception();

    httpClientSPIAgent.transferResponse(
        mockHttpServletRequest,
        new BufferCacheServletResponse(new MockHttpServletResponse()),
        exception);

    Assert.assertNull(mockHttpServletRequest.getAttribute(WebKeys.SPI_AGENT_REQUEST));
    Assert.assertNull(mockHttpServletRequest.getAttribute(WebKeys.SPI_AGENT_RESPONSE));
    Assert.assertSame(exception, recordSPIAgentResponse.exception);
    Assert.assertNull(mockHttpServletRequest.getAttribute(WebKeys.SPI_AGENT_ORIGINAL_RESPONSE));
    Assert.assertEquals(8, mockHttpServletResponse.getContentLength());
    Assert.assertNull(recordSPIAgentResponse._request);
    Assert.assertNull(recordSPIAgentResponse._bufferCacheServletResponse);
    Assert.assertSame(mockRegistrationReference, recordSPIAgentResponse._registrationReference);
    Assert.assertSame(
        mockHttpServletResponse.getOutputStream(), recordSPIAgentResponse._outputStream);

    // Response

    SPIAgentRequest spiAgentRequest = new SPIAgentRequest(_mockHttpServletRequest);

    File tempFile = File.createTempFile(HttpClientSPIAgentTest.class.getName(), null);

    Assert.assertTrue(tempFile.exists());

    spiAgentRequest.requestBodyFile = tempFile;

    mockHttpServletRequest.setAttribute(WebKeys.SPI_AGENT_REQUEST, spiAgentRequest);

    recordSPIAgentResponse = new RecordSPIAgentResponse();

    mockHttpServletRequest.setAttribute(WebKeys.SPI_AGENT_RESPONSE, recordSPIAgentResponse);

    mockHttpServletResponse = new MockHttpServletResponse();

    mockHttpServletRequest.setAttribute(
        WebKeys.SPI_AGENT_ORIGINAL_RESPONSE, mockHttpServletResponse);

    BufferCacheServletResponse bufferCacheServletResponse =
        new BufferCacheServletResponse(new MockHttpServletResponse());

    httpClientSPIAgent.transferResponse(mockHttpServletRequest, bufferCacheServletResponse, null);

    Assert.assertFalse(tempFile.exists());
    Assert.assertNull(mockHttpServletRequest.getAttribute(WebKeys.SPI_AGENT_REQUEST));
    Assert.assertNull(mockHttpServletRequest.getAttribute(WebKeys.SPI_AGENT_RESPONSE));
    Assert.assertNull(recordSPIAgentResponse.exception);
    Assert.assertNull(mockHttpServletRequest.getAttribute(WebKeys.SPI_AGENT_ORIGINAL_RESPONSE));
    Assert.assertEquals(8, mockHttpServletResponse.getContentLength());
    Assert.assertSame(mockHttpServletRequest, recordSPIAgentResponse._request);
    Assert.assertSame(
        bufferCacheServletResponse, recordSPIAgentResponse._bufferCacheServletResponse);
    Assert.assertSame(mockRegistrationReference, recordSPIAgentResponse._registrationReference);
    Assert.assertSame(
        mockHttpServletResponse.getOutputStream(), recordSPIAgentResponse._outputStream);

    // Undeletable request body file

    spiAgentRequest = new SPIAgentRequest(_mockHttpServletRequest);

    tempFile = File.createTempFile(HttpClientSPIAgentTest.class.getName(), null);

    Assert.assertTrue(tempFile.exists());

    spiAgentRequest.requestBodyFile = tempFile;

    mockHttpServletRequest.setAttribute(WebKeys.SPI_AGENT_REQUEST, spiAgentRequest);

    recordSPIAgentResponse = new RecordSPIAgentResponse();

    mockHttpServletRequest.setAttribute(WebKeys.SPI_AGENT_RESPONSE, recordSPIAgentResponse);

    mockHttpServletResponse = new MockHttpServletResponse();

    mockHttpServletRequest.setAttribute(
        WebKeys.SPI_AGENT_ORIGINAL_RESPONSE, mockHttpServletResponse);

    bufferCacheServletResponse = new BufferCacheServletResponse(new MockHttpServletResponse());

    Assert.assertTrue(tempFile.delete());

    httpClientSPIAgent.transferResponse(mockHttpServletRequest, bufferCacheServletResponse, null);

    Class<?> clazz = Class.forName("java.io.DeleteOnExitHook");

    Field filesField = ReflectionUtil.getDeclaredField(clazz, "files");

    Set<String> files = (Set<String>) filesField.get(null);

    Assert.assertTrue(files.contains(tempFile.getPath()));
  }
  @Test
  public void testTransferResponse() throws IOException {

    // Exception

    MockRegistrationReference mockRegistrationReference =
        new MockRegistrationReference(new MockIntraband());

    HttpClientSPIAgent httpClientSPIAgent =
        new HttpClientSPIAgent(_spiConfiguration, mockRegistrationReference);

    MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();

    mockHttpServletRequest.setAttribute(
        WebKeys.SPI_AGENT_REQUEST, new SPIAgentRequest(new MockHttpServletRequest()));

    RecordSPIAgentResponse recordSPIAgentResponse = new RecordSPIAgentResponse();

    mockHttpServletRequest.setAttribute(WebKeys.SPI_AGENT_RESPONSE, recordSPIAgentResponse);

    MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();

    mockHttpServletRequest.setAttribute(
        WebKeys.SPI_AGENT_ORIGINAL_RESPONSE, mockHttpServletResponse);

    Exception exception = new Exception();

    httpClientSPIAgent.transferResponse(
        mockHttpServletRequest,
        new BufferCacheServletResponse(new MockHttpServletResponse()),
        exception);

    Assert.assertNull(mockHttpServletRequest.getAttribute(WebKeys.SPI_AGENT_REQUEST));
    Assert.assertNull(mockHttpServletRequest.getAttribute(WebKeys.SPI_AGENT_RESPONSE));
    Assert.assertSame(exception, recordSPIAgentResponse.exception);
    Assert.assertNull(mockHttpServletRequest.getAttribute(WebKeys.SPI_AGENT_ORIGINAL_RESPONSE));
    Assert.assertEquals(8, mockHttpServletResponse.getContentLength());
    Assert.assertNull(recordSPIAgentResponse._request);
    Assert.assertNull(recordSPIAgentResponse._bufferCacheServletResponse);
    Assert.assertSame(mockRegistrationReference, recordSPIAgentResponse._registrationReference);
    Assert.assertSame(
        mockHttpServletResponse.getOutputStream(), recordSPIAgentResponse._outputStream);

    // Response

    mockHttpServletRequest.setAttribute(
        WebKeys.SPI_AGENT_REQUEST, new SPIAgentRequest(new MockHttpServletRequest()));

    recordSPIAgentResponse = new RecordSPIAgentResponse();

    mockHttpServletRequest.setAttribute(WebKeys.SPI_AGENT_RESPONSE, recordSPIAgentResponse);

    mockHttpServletResponse = new MockHttpServletResponse();

    mockHttpServletRequest.setAttribute(
        WebKeys.SPI_AGENT_ORIGINAL_RESPONSE, mockHttpServletResponse);

    BufferCacheServletResponse bufferCacheServletResponse =
        new BufferCacheServletResponse(new MockHttpServletResponse());

    httpClientSPIAgent.transferResponse(mockHttpServletRequest, bufferCacheServletResponse, null);

    Assert.assertNull(mockHttpServletRequest.getAttribute(WebKeys.SPI_AGENT_REQUEST));
    Assert.assertNull(mockHttpServletRequest.getAttribute(WebKeys.SPI_AGENT_RESPONSE));
    Assert.assertNull(recordSPIAgentResponse.exception);
    Assert.assertNull(mockHttpServletRequest.getAttribute(WebKeys.SPI_AGENT_ORIGINAL_RESPONSE));
    Assert.assertEquals(8, mockHttpServletResponse.getContentLength());
    Assert.assertSame(mockHttpServletRequest, recordSPIAgentResponse._request);
    Assert.assertSame(
        bufferCacheServletResponse, recordSPIAgentResponse._bufferCacheServletResponse);
    Assert.assertSame(mockRegistrationReference, recordSPIAgentResponse._registrationReference);
    Assert.assertSame(
        mockHttpServletResponse.getOutputStream(), recordSPIAgentResponse._outputStream);
  }