Class DebuggingWebConnection
java.lang.Object
org.htmlunit.util.WebConnectionWrapper
org.htmlunit.util.DebuggingWebConnection
- All Implemented Interfaces:
AutoCloseable, WebConnection
Wrapper around a "real" WebConnection that will use the wrapped web connection
to do the real job and save all received responses
in the temp directory with an overview page.
This may be useful at conception time to understand what is "browsed".
Example:
This class is only intended as a help during the conception.
This may be useful at conception time to understand what is "browsed".
Example:
final WebClient client = new WebClient(); final WebConnection connection = new DebuggingWebConnection(client.getWebConnection(), "myTest"); client.setWebConnection(connection);In this example an overview page will be generated under the name myTest/index.html in the temp directory and all received responses will be saved into the myTest folder.
This class is only intended as a help during the conception.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate intprivate static final Patternprivate final Fileprivate static final org.apache.commons.logging.Logprivate final Fileprivate booleanprivate final WebConnection -
Constructor Summary
ConstructorsConstructorDescriptionDebuggingWebConnection(WebConnection webConnection, String dirName) Wraps a web connection to have a report generated of the received responses. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a mark that will be visible in the HTML result page generated by this class.private voidappendToJSFile(String str) (package private) static StringchooseExtension(String contentType) private FilecreateFile(URL url, String extension) Computes the best file to save the response to the given URL.private voidCreates the summary file and the JavaScript file that will be updated for each received response(package private) static StringescapeJSString(String string) (package private) FilegetResponse(WebRequest request) Calls the wrapped webconnection and save the received response.(package private) static booleanisJavaScript(String contentType) Indicates if the response contains JavaScript content.booleanIndicates if it should try to format responses recognized as JavaScript.(package private) static StringnameValueListToJsMap(List<NameValuePair> headers) Produces a String that will produce a JS map like "{'key1': 'value1', 'key 2': 'value2'}".protected voidsaveResponse(WebResponse response, WebRequest request) Saves the response content in the temp dir and adds it to the summary page.voidsetUncompressJavaScript(boolean decompress) Indicates that responses recognized as JavaScript should be formatted or not.protected WebResponseuncompressJavaScript(WebResponse response) Tries to uncompress the JavaScript code in the provided response.Methods inherited from class WebConnectionWrapper
close, getWrappedWebConnection
-
Field Details
-
LOG
private static final org.apache.commons.logging.Log LOG -
ESCAPE_QUOTE_PATTERN
-
counter_
private int counter_ -
wrappedWebConnection_
-
javaScriptFile_
-
reportFolder_
-
uncompressJavaScript_
private boolean uncompressJavaScript_
-
-
Constructor Details
-
DebuggingWebConnection
Wraps a web connection to have a report generated of the received responses.- Parameters:
webConnection- the webConnection that do the real workdirName- the name of the directory to create in the tmp folder to save received responses. If this folder already exists, it will be deleted first.- Throws:
IOException- in case of problems writing the files
-
-
Method Details
-
getResponse
Calls the wrapped webconnection and save the received response. Submits a request and retrieves a response. The default behavior of this method is to returnWebConnection.getResponse(WebRequest)on the wrapped connection object.- Specified by:
getResponsein interfaceWebConnection- Overrides:
getResponsein classWebConnectionWrapper- Parameters:
request- the request- Returns:
- the response to the request defined by the specified request
- Throws:
IOException- if an IO error occurs
-
uncompressJavaScript
Tries to uncompress the JavaScript code in the provided response.- Parameters:
response- the response to uncompress- Returns:
- a new response with uncompressed JavaScript code or the original response in case of failure
-
addMark
Adds a mark that will be visible in the HTML result page generated by this class.- Parameters:
mark- the text- Throws:
IOException- if a problem occurs writing the file
-
saveResponse
Saves the response content in the temp dir and adds it to the summary page.- Parameters:
response- the response to saverequest- the request used to get the response- Throws:
IOException- if a problem occurs writing the file
-
escapeJSString
-
chooseExtension
-
isJavaScript
Indicates if the response contains JavaScript content.- Parameters:
contentType- the response's content type- Returns:
falseif it is not recognized as JavaScript
-
isUncompressJavaScript
public boolean isUncompressJavaScript()Indicates if it should try to format responses recognized as JavaScript.- Returns:
- default is
falseto deliver the original content
-
setUncompressJavaScript
public void setUncompressJavaScript(boolean decompress) Indicates that responses recognized as JavaScript should be formatted or not. Formatting is interesting for debugging when the original script is compressed on a single line. It allows to better follow with a debugger and to obtain more interesting error messages.- Parameters:
decompress-trueif JavaScript responses should be uncompressed
-
appendToJSFile
- Throws:
IOException
-
createFile
Computes the best file to save the response to the given URL.- Parameters:
url- the requested URLextension- the preferred extension- Returns:
- the file to create
- Throws:
IOException- if a problem occurs creating the file
-
nameValueListToJsMap
Produces a String that will produce a JS map like "{'key1': 'value1', 'key 2': 'value2'}".- Parameters:
headers- a list ofNameValuePair- Returns:
- the JS String
-
createOverview
Creates the summary file and the JavaScript file that will be updated for each received response- Throws:
IOException- if a problem occurs writing the file
-
getReportFolder
File getReportFolder()
-