Author: pmouawad Date: Fri Oct 27 10:49:08 2017 New Revision: 1813507 URL: http://svn.apache.org/viewvc?rev=1813507&view=rev Log: Bug 57760 - View Results Tree : Cookie Header is wrongly shown as empty(no cookies) when viewing a recorder Sample Result Bugzilla Id: 57760
Modified: jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPAbstractImpl.java jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHC4Impl.java jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPJavaImpl.java jmeter/trunk/xdocs/changes.xml Modified: jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPAbstractImpl.java URL: http://svn.apache.org/viewvc/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPAbstractImpl.java?rev=1813507&r1=1813506&r2=1813507&view=diff ============================================================================== --- jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPAbstractImpl.java (original) +++ jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPAbstractImpl.java Fri Oct 27 10:49:08 2017 @@ -29,6 +29,7 @@ import java.net.NetworkInterface; import java.net.SocketException; import java.net.URL; import java.net.UnknownHostException; +import java.util.function.Predicate; import org.apache.jmeter.config.Arguments; import org.apache.jmeter.protocol.http.control.AuthManager; @@ -36,6 +37,7 @@ import org.apache.jmeter.protocol.http.c import org.apache.jmeter.protocol.http.control.CookieManager; import org.apache.jmeter.protocol.http.control.HeaderManager; import org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.SourceType; +import org.apache.jmeter.protocol.http.util.HTTPConstants; import org.apache.jmeter.protocol.http.util.HTTPConstantsInterface; import org.apache.jmeter.protocol.http.util.HTTPFileArg; import org.apache.jmeter.samplers.Interruptible; @@ -72,6 +74,10 @@ public abstract class HTTPAbstractImpl i private static final String RETURN_CUSTOM_STATUS_CODE = JMeterUtils.getProperty("RETURN_CUSTOM_STATUS.code");//$NON-NLS-1$ + protected static final Predicate<String> ALL_EXCEPT_COOKIE = s -> !HTTPConstants.HEADER_COOKIE.equalsIgnoreCase(s); + + protected static final Predicate<String> ONLY_COOKIE = s -> HTTPConstants.HEADER_COOKIE.equalsIgnoreCase(s); + /** * Custom response message for cached resource */ Modified: jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHC4Impl.java URL: http://svn.apache.org/viewvc/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHC4Impl.java?rev=1813507&r1=1813506&r2=1813507&view=diff ============================================================================== --- jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHC4Impl.java (original) +++ jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPHC4Impl.java Fri Oct 27 10:49:08 2017 @@ -36,6 +36,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; +import java.util.function.Predicate; import java.util.regex.Pattern; import java.util.zip.GZIPInputStream; @@ -461,7 +462,7 @@ public class HTTPHC4Impl extends HTTPHCA if (localAddr != null) { request.addHeader(HEADER_LOCAL_ADDRESS, localAddr.toString()); } - res.setRequestHeaders(getConnectionHeaders(request)); + res.setRequestHeaders(getAllHeadersExceptCookie(request)); Header contentType = httpResponse.getLastHeader(HTTPConstants.HEADER_CONTENT_TYPE); if (contentType != null){ @@ -542,7 +543,7 @@ public class HTTPHC4Impl extends HTTPHCA if (res.getRequestHeaders() != null) { log.debug("Overwriting request old headers: {}", res.getRequestHeaders()); } - res.setRequestHeaders(getConnectionHeaders((HttpRequest) localContext.getAttribute(HttpCoreContext.HTTP_REQUEST))); + res.setRequestHeaders(getAllHeadersExceptCookie((HttpRequest) localContext.getAttribute(HttpCoreContext.HTTP_REQUEST))); errorResult(e, res); return res; } catch (RuntimeException e) { @@ -1003,7 +1004,12 @@ public class HTTPHC4Impl extends HTTPHCA String cookies = setConnectionCookie(httpRequest, url, getCookieManager()); if (res != null) { - res.setCookies(cookies); + if(cookies != null && !cookies.isEmpty()) { + res.setCookies(cookies); + } else { + // During recording Cookie Manager doesn't handle cookies + res.setCookies(getOnlyCookieFromHeaders(httpRequest)); + } } } @@ -1146,20 +1152,43 @@ public class HTTPHC4Impl extends HTTPHCA } /** - * Get all the request headers for the <code>HttpMethod</code> + * Get all the request headers except Cookie for the <code>HttpRequest</code> + * + * @param method + * <code>HttpMethod</code> which represents the request + * @return the headers as a string + */ + private String getAllHeadersExceptCookie(HttpRequest method) { + return getFromHeadersMatchingPredicate(method, ALL_EXCEPT_COOKIE); + } + + /** + * Get only Cookie header for the <code>HttpRequest</code> + * + * @param method + * <code>HttpMethod</code> which represents the request + * @return the headers as a string + */ + private String getOnlyCookieFromHeaders(HttpRequest method) { + return getFromHeadersMatchingPredicate(method, ONLY_COOKIE); + } + + + /** + * Get only cookies from request headers for the <code>HttpRequest</code> * * @param method * <code>HttpMethod</code> which represents the request * @return the headers as a string */ - private String getConnectionHeaders(HttpRequest method) { + private String getFromHeadersMatchingPredicate(HttpRequest method, Predicate<String> predicate) { if(method != null) { // Get all the request headers StringBuilder hdrs = new StringBuilder(150); Header[] requestHeaders = method.getAllHeaders(); for (Header requestHeader : requestHeaders) { - // Exclude the COOKIE header, since cookie is reported separately in the sample - if (!HTTPConstants.HEADER_COOKIE.equalsIgnoreCase(requestHeader.getName())) { + // Get header if it matches predicate + if (predicate.test(requestHeader.getName())) { writeHeader(hdrs, requestHeader); } } Modified: jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPJavaImpl.java URL: http://svn.apache.org/viewvc/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPJavaImpl.java?rev=1813507&r1=1813506&r2=1813507&view=diff ============================================================================== --- jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPJavaImpl.java (original) +++ jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPJavaImpl.java Fri Oct 27 10:49:08 2017 @@ -31,6 +31,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.function.Predicate; import java.util.zip.GZIPInputStream; import org.apache.commons.io.input.CountingInputStream; @@ -207,8 +208,14 @@ public class HTTPJavaImpl extends HTTPAb } if (res != null) { - res.setRequestHeaders(getConnectionHeaders(conn, securityHeaders)); - res.setCookies(cookies); + res.setRequestHeaders(getAllHeadersExceptCookie(conn, securityHeaders)); + if(cookies != null && !cookies.isEmpty()) { + res.setCookies(cookies); + } else { + // During recording Cookie Manager doesn't handle cookies + res.setCookies(getOnlyCookieFromHeaders(conn, securityHeaders)); + + } } return conn; @@ -388,6 +395,19 @@ public class HTTPJavaImpl extends HTTPAb } /** + * Get only the Cookie headers for the <code>HttpURLConnection</code> passed in + * + * @param conn + * <code>HttpUrlConnection</code> which represents the URL + * request + * @param securityHeaders Map of security Header + * @return the headers as a string + */ + private String getOnlyCookieFromHeaders(HttpURLConnection conn, Map<String, String> securityHeaders) { + return getFromConnectionHeaders(conn, securityHeaders, ONLY_COOKIE, false); + } + + /** * Get all the headers for the <code>HttpURLConnection</code> passed in * * @param conn @@ -396,14 +416,29 @@ public class HTTPJavaImpl extends HTTPAb * @param securityHeaders Map of security Header * @return the headers as a string */ - private String getConnectionHeaders(HttpURLConnection conn, Map<String, String> securityHeaders) { + private String getAllHeadersExceptCookie(HttpURLConnection conn, Map<String, String> securityHeaders) { + return getFromConnectionHeaders(conn, securityHeaders, ALL_EXCEPT_COOKIE, true); + } + + /** + * Get all the headers for the <code>HttpURLConnection</code> passed in + * + * @param conn + * <code>HttpUrlConnection</code> which represents the URL + * request + * @param securityHeaders Map of security Header + * @param predicate {@link Predicate} + * @return the headers as a string + */ + private String getFromConnectionHeaders(HttpURLConnection conn, Map<String, String> securityHeaders, + Predicate<String> predicate, boolean addSecurityHeaders) { // Get all the request properties, which are the headers set on the connection StringBuilder hdrs = new StringBuilder(100); Map<String, List<String>> requestHeaders = conn.getRequestProperties(); for(Map.Entry<String, List<String>> entry : requestHeaders.entrySet()) { String headerKey=entry.getKey(); // Exclude the COOKIE header, since cookie is reported separately in the sample - if(!HTTPConstants.HEADER_COOKIE.equalsIgnoreCase(headerKey)) { + if(predicate.test(headerKey)) { // value is a List of Strings for (String value : entry.getValue()){ hdrs.append(headerKey); @@ -413,9 +448,11 @@ public class HTTPJavaImpl extends HTTPAb } } } - for(Map.Entry<String, String> entry : securityHeaders.entrySet()) { - hdrs.append(entry.getKey()).append(": ") // $NON-NLS-1$ - .append(entry.getValue()).append("\n"); // $NON-NLS-1$ + if(addSecurityHeaders) { + for(Map.Entry<String, String> entry : securityHeaders.entrySet()) { + hdrs.append(entry.getKey()).append(": ") // $NON-NLS-1$ + .append(entry.getValue()).append("\n"); // $NON-NLS-1$ + } } return hdrs.toString(); } Modified: jmeter/trunk/xdocs/changes.xml URL: http://svn.apache.org/viewvc/jmeter/trunk/xdocs/changes.xml?rev=1813507&r1=1813506&r2=1813507&view=diff ============================================================================== --- jmeter/trunk/xdocs/changes.xml [utf-8] (original) +++ jmeter/trunk/xdocs/changes.xml [utf-8] Fri Oct 27 10:49:08 2017 @@ -102,6 +102,7 @@ Summary <h3>Listeners</h3> <ul> + <li><bug>57760</bug>View Results Tree : Cookie Header is wrongly shown as empty(no cookies) when viewing a recorder Sample Result. Contributed by Ubik Load Pack (support at ubikloadpack.com)</li> </ul> <h3>Timers, Assertions, Config, Pre- & Post-Processors</h3>