BrowserMobRequestUpdater { public void updateRequest(final BrowserMobHttpRequest browserMobHttpRequest, final WilmaHttpRequest wilmaRequest) { Map<String, HttpHeaderChange> headerChangeMap = wilmaRequest.getHeaderChanges(); if (headerChangeMap != null && !headerChangeMap.isEmpty()) { for (Map.Entry<String, HttpHeaderChange> headerChangeEntry : headerChangeMap.entrySet()) { String headerKey = headerChangeEntry.getKey(); HttpHeaderChange headerChange = headerChangeEntry.getValue(); Header header = browserMobHttpRequest.getMethod().getFirstHeader(headerKey); if (headerChange instanceof HttpHeaderToBeUpdated) { if (header != null) { ((HttpHeaderToBeUpdated) headerChange).setOriginalValue(header.getValue()); } browserMobHttpRequest.getMethod().addHeader(headerKey, ((HttpHeaderToBeUpdated) headerChange).getNewValue()); headerChange.setApplied(); } else { if (header != null) { browserMobHttpRequest.getMethod().removeHeader(header); headerChange.setApplied(); } } } } byte[] newBody = wilmaRequest.getNewBody(); if (newBody != null) { if (browserMobHttpRequest.getMethod() instanceof HttpEntityEnclosingRequestBase) { HttpEntityEnclosingRequestBase enclosingRequest = (HttpEntityEnclosingRequestBase) browserMobHttpRequest.getMethod(); enclosingRequest.setEntity(new ByteArrayEntity(wilmaRequest.getNewBody())); } } browserMobHttpRequest.setResponseVolatile(wilmaRequest.isResponseVolatile()); browserMobHttpRequest.getMethod().setURI(wilmaRequest.getUri()); }  void updateRequest(final BrowserMobHttpRequest browserMobHttpRequest, final WilmaHttpRequest wilmaRequest);  }
@Test public void testUpdateRequestShouldCallBrowsermobRequestsMethods() throws URISyntaxException { URI uri = new URI("MOCK"); given(browserMobHttpRequest.getMethod()).willReturn(requestBase); given(wilmaHttpRequest.getUri()).willReturn(uri); String mockID = "WILMA-LOG-MOCK-ID"; given(wilmaHttpRequest.getWilmaMessageId()).willReturn(mockID); underTest.updateRequest(browserMobHttpRequest, wilmaHttpRequest); verify(requestBase).setURI(uri); }
@Test public void testUpdateRequestShouldUpdateHeadersAddPart() throws URISyntaxException { URI uri = new URI("MOCK"); given(browserMobHttpRequest.getMethod()).willReturn(requestBase); given(wilmaHttpRequest.getUri()).willReturn(uri); String mockID = "WILMA-LOG-MOCK-ID"; Map<String, HttpHeaderChange> headerChanges = new HashMap<>(); HttpHeaderToBeUpdated headerToBeUpdated = new HttpHeaderToBeUpdated("B"); headerChanges.put("A", headerToBeUpdated); given(wilmaHttpRequest.getHeaderChanges()).willReturn(headerChanges); given(wilmaHttpRequest.getWilmaMessageId()).willReturn(mockID); underTest.updateRequest(browserMobHttpRequest, wilmaHttpRequest); verify(requestBase).addHeader("A", "B"); }
@Test public void testUpdateRequestShouldUpdateHeadersRemovePart() throws URISyntaxException { URI uri = new URI("MOCK"); given(browserMobHttpRequest.getMethod()).willReturn(requestBase); given(requestBase.getFirstHeader("A")).willReturn(header); given(header.getName()).willReturn("A"); given(header.getValue()).willReturn("B"); given(wilmaHttpRequest.getUri()).willReturn(uri); String mockID = "WILMA-LOG-MOCK-ID"; Map<String, HttpHeaderChange> headerChanges = new HashMap<>(); HttpHeaderToBeRemoved headerToBeRemoved = new HttpHeaderToBeRemoved(); headerChanges.put("A", headerToBeRemoved); given(wilmaHttpRequest.getHeaderChanges()).willReturn(headerChanges); given(wilmaHttpRequest.getWilmaMessageId()).willReturn(mockID); underTest.updateRequest(browserMobHttpRequest, wilmaHttpRequest); verify(requestBase).removeHeader(header); }
@Test public void testUpdateRequestShouldUpdateBodyPart() throws URISyntaxException { URI uri = new URI("MOCK"); given(browserMobHttpRequest.getMethod()).willReturn(enclosingRequest); String mockID = "WILMA-LOG-MOCK-ID"; given(wilmaHttpRequest.getWilmaMessageId()).willReturn(mockID); given(wilmaHttpRequest.getNewBody()).willReturn("NEW BODY".getBytes()); given(wilmaHttpRequest.getUri()).willReturn(uri); underTest.updateRequest(browserMobHttpRequest, wilmaHttpRequest); verify(enclosingRequest).setEntity((HttpEntity) Matchers.anyObject()); }