HttpClientRequestTransport implements BaseHttpRequestTransport { public Response sendRequest(SubmitContext submitContext, Request request) throws Exception { AbstractHttpRequestInterface<?> httpRequest = (AbstractHttpRequestInterface<?>) request; HttpClientSupport.SoapUIHttpClient httpClient = getSoapUIHttpClient(); ExtendedHttpMethod httpMethod = createHttpMethod(httpRequest); boolean createdContext = false; HttpContext httpContext = (HttpContext) submitContext.getProperty(SubmitContext.HTTP_STATE_PROPERTY); if (httpContext == null) { httpContext = HttpClientSupport.createEmptyContext(); submitContext.setProperty(SubmitContext.HTTP_STATE_PROPERTY, httpContext); createdContext = true; } String localAddress = System.getProperty("soapui.bind.address", httpRequest.getBindAddress()); if (localAddress == null || localAddress.trim().length() == 0) { localAddress = SoapUI.getSettings().getString(HttpSettings.BIND_ADDRESS, null); } org.apache.http.HttpResponse httpResponse; if (localAddress != null && localAddress.trim().length() > 0) { try { httpMethod.getParams().setParameter(ConnRoutePNames.LOCAL_ADDRESS, InetAddress.getByName(localAddress)); } catch (Exception e) { SoapUI.logError(e, "Failed to set localAddress to [" + localAddress + "]"); } } submitContext.removeProperty(RESPONSE); submitContext.setProperty(HTTP_METHOD, httpMethod); submitContext.setProperty(POST_METHOD, httpMethod); submitContext.setProperty(HTTP_CLIENT, httpClient); submitContext.setProperty(REQUEST_CONTENT, httpRequest.getRequestContent()); submitContext.setProperty(WSDL_REQUEST, httpRequest); submitContext.setProperty(RESPONSE_PROPERTIES, new StringToStringMap()); filterRequest(submitContext, httpRequest); try { Settings settings = httpRequest.getSettings(); StringToStringsMap headers = httpRequest.getRequestHeaders(); for (String headerName : headers.keySet()) { String expandedHeaderName = PropertyExpander.expandProperties(submitContext, headerName); httpMethod.removeHeaders(expandedHeaderName); for (String headerValue : headers.get(headerName)) { headerValue = PropertyExpander.expandProperties(submitContext, headerValue); httpMethod.addHeader(expandedHeaderName, headerValue); } } WsdlProject project = (WsdlProject) ModelSupport.getModelItemProject(httpRequest); WssCrypto crypto = null; if (project != null && project.getWssContainer() != null) { crypto = project.getWssContainer().getCryptoByName( PropertyExpander.expandProperties(submitContext, httpRequest.getSslKeystore())); } if (crypto != null && WssCrypto.STATUS_OK.equals(crypto.getStatus())) { httpMethod.getParams().setParameter(SoapUIHttpRoute.SOAPUI_SSL_CONFIG, crypto.getSource() + " " + crypto.getPassword()); } httpMethod.setDumpFile(PathUtils.expandPath(httpRequest.getDumpFile(), (AbstractWsdlModelItem<?>) httpRequest, submitContext)); if (settings.getBoolean(HttpSettings.INCLUDE_REQUEST_IN_TIME_TAKEN)) { httpMethod.initStartTime(); } if (httpMethod.getMetrics() != null) { httpMethod.getMetrics().setHttpMethod(httpMethod.getMethod()); captureMetrics(httpMethod, httpClient); httpMethod.getMetrics().getTotalTimer().start(); } httpResponse = submitRequest(httpMethod, httpContext); saveRequestHeaders(httpMethod, httpContext); if (httpMethod.getMetrics() != null) { httpMethod.getMetrics().getReadTimer().stop(); httpMethod.getMetrics().getTotalTimer().stop(); } if (isRedirectResponse(httpResponse.getStatusLine().getStatusCode()) && httpRequest.isFollowRedirects()) { if (httpResponse.getEntity() != null) { EntityUtils.consume(httpResponse.getEntity()); } httpMethod = followRedirects(httpClient, 0, httpMethod, httpResponse, httpContext, submitContext); submitContext.setProperty(HTTP_METHOD, httpMethod); } } catch (Throwable t) { httpMethod.setFailed(t); if (t instanceof Exception) { throw (Exception) t; } SoapUI.logError(t); throw new Exception(t); } finally { if (!httpMethod.isFailed()) { if (httpMethod.getMetrics() != null) { if (httpMethod.getMetrics().getReadTimer().getStop() == 0) { httpMethod.getMetrics().getReadTimer().stop(); } if (httpMethod.getMetrics().getTotalTimer().getStop() == 0) { httpMethod.getMetrics().getTotalTimer().stop(); } } } else { httpMethod.getMetrics().reset(); httpMethod.getMetrics().setTimestamp(System.currentTimeMillis()); captureMetrics(httpMethod, httpClient); } for (int c = filters.size() - 1; c >= 0; c--) { RequestFilter filter = filters.get(c); filter.afterRequest(submitContext, httpRequest); } if (!submitContext.hasProperty(RESPONSE)) { createDefaultResponse(submitContext, httpRequest, httpMethod); } Response response = (Response) submitContext.getProperty(BaseHttpRequestTransport.RESPONSE); StringToStringMap responseProperties = (StringToStringMap) submitContext .getProperty(BaseHttpRequestTransport.RESPONSE_PROPERTIES); for (String key : responseProperties.keySet()) { response.setProperty(key, responseProperties.get(key)); } if (createdContext) { submitContext.setProperty(SubmitContext.HTTP_STATE_PROPERTY, null); } } return (Response) submitContext.getProperty(BaseHttpRequestTransport.RESPONSE); } HttpClientRequestTransport(); void addRequestFilter(RequestFilter filter); void removeRequestFilter(RequestFilter filterToRemove); @Override void insertRequestFilter(RequestFilter filter, RequestFilter refFilter); void removeRequestFilter(Class<T> filterClass); void replaceRequestFilter(Class<T> filterClass, RequestFilter newFilter); @CheckForNull RequestFilter findFilterByType(Class<T> filterType); void abortRequest(SubmitContext submitContext); Response sendRequest(SubmitContext submitContext, Request request);  }
@Test public void processesRequestCorrectly() throws Exception { StringToStringsMap emptyHeaders = new StringToStringsMap(); AbstractHttpRequest request = prepareRequestWithHeaders(emptyHeaders); SubmitContext submitContext = new StubbedSubmitContext(request); httpTransport.sendRequest(submitContext, request); assertThat(methodExecuted, is(notNullValue())); assertThat(contextUsed, is(notNullValue())); }
@Test public void expandsPropertiesInHeaderName() throws Exception { StringToStringsMap headers = new StringToStringsMap(); String headerValue = "The value"; headers.add("Header-for-${request}", headerValue); AbstractHttpRequest request = prepareRequestWithHeaders(headers); SubmitContext submitContext = new StubbedSubmitContext(request); String requestName = "Fin-fin request"; submitContext.setProperty("request", requestName); httpTransport.sendRequest(submitContext, request); String expectedHeaderName = "Header-for-" + requestName; Header[] modifiedHeaders = methodExecuted.getHeaders(expectedHeaderName); assertThat(modifiedHeaders.length, is(1)); assertThat(modifiedHeaders[0].getName(), is(expectedHeaderName)); assertThat(modifiedHeaders[0].getValue(), is(headerValue)); }