BrowserMobResponseUpdater { public void updateResponse(final BrowserMobHttpResponse browserMobHttpResponse, final WilmaHttpResponse wilmaResponse) { if (!wilmaResponse.isVolatile()) { return; } Map<String, HttpHeaderChange> headerChangeMap = wilmaResponse.getHeaderChanges(); if (headerChangeMap != null && !headerChangeMap.isEmpty()) { for (Map.Entry<String, HttpHeaderChange> headerChangeEntry : headerChangeMap.entrySet()) { String headerKey = headerChangeEntry.getKey(); HttpHeaderChange headerChange = headerChangeEntry.getValue(); Header header = browserMobHttpResponse.getRawResponse().getFirstHeader(headerKey); if (headerChange instanceof HttpHeaderToBeUpdated) { if (header != null) { ((HttpHeaderToBeUpdated) headerChange).setOriginalValue(header.getValue()); } browserMobHttpResponse.getRawResponse().addHeader(headerKey, ((HttpHeaderToBeUpdated) headerChange).getNewValue()); headerChange.setApplied(); } else { if (header != null) { browserMobHttpResponse.getRawResponse().removeHeader(header); headerChange.setApplied(); } } } } byte[] newBody = wilmaResponse.getNewBody(); if (newBody != null) { try { browserMobHttpResponse.setAnswer(newBody); browserMobHttpResponse.getRawResponse().setEntity(new ByteArrayEntity(wilmaResponse.getNewBody())); } catch (IOException e) { logger.warn("Message ont-the-fly update was failed for message: " + wilmaResponse.getWilmaMessageId(), e); } } }  void updateResponse(final BrowserMobHttpResponse browserMobHttpResponse, final WilmaHttpResponse wilmaResponse);  }
@Test public void testUpdateResponseShouldUpdateHeadersAddPart() throws URISyntaxException { given(browserMobHttpResponse.getRawResponse()).willReturn(httpResponse); String mockID = "WILMA-LOG-MOCK-ID"; Map<String, HttpHeaderChange> headerChanges = new HashMap<>(); HttpHeaderToBeUpdated headerToBeUpdated = new HttpHeaderToBeUpdated("B"); headerChanges.put("A", headerToBeUpdated); given(wilmaHttpResponse.getHeaderChanges()).willReturn(headerChanges); given(wilmaHttpResponse.getWilmaMessageId()).willReturn(mockID); given(wilmaHttpResponse.isVolatile()).willReturn(true); underTest.updateResponse(browserMobHttpResponse, wilmaHttpResponse); verify(httpResponse).addHeader("A", "B"); }
@Test public void testUpdateResponseShouldUpdateHeadersRemovePart() throws URISyntaxException { given(browserMobHttpResponse.getRawResponse()).willReturn(httpResponse); given(httpResponse.getFirstHeader("A")).willReturn(header); given(header.getName()).willReturn("A"); given(header.getValue()).willReturn("B"); String mockID = "WILMA-LOG-MOCK-ID"; Map<String, HttpHeaderChange> headerChanges = new HashMap<>(); HttpHeaderToBeRemoved headerToBeRemoved = new HttpHeaderToBeRemoved(); headerChanges.put("A", headerToBeRemoved); given(wilmaHttpResponse.getHeaderChanges()).willReturn(headerChanges); given(wilmaHttpResponse.getWilmaMessageId()).willReturn(mockID); given(wilmaHttpResponse.isVolatile()).willReturn(true); underTest.updateResponse(browserMobHttpResponse, wilmaHttpResponse); verify(httpResponse).removeHeader(header); }
@Test public void testUpdateResponseShouldUpdateBodyPart() throws URISyntaxException, IOException { given(browserMobHttpResponse.getRawResponse()).willReturn(httpResponse); given(wilmaHttpResponse.getNewBody()).willReturn("NEW BODY".getBytes()); given(wilmaHttpResponse.isVolatile()).willReturn(true); underTest.updateResponse(browserMobHttpResponse, wilmaHttpResponse); verify(browserMobHttpResponse).setAnswer((byte[]) anyObject()); verify(httpResponse).setEntity((HttpEntity) anyObject()); }