Hallo Andreas,
the 2 patches make sense to me. Can you open an issue in WebTest and a
separate issue by HtmlUnit for that (tests are naturally appreciated)?
Btw, do you already use WebTest/HtmlUnit for the functional tests in
Cocoon or Lenya?
Cheers,
Marc.
Andreas Hartmann wrote:
> Andreas Hartmann schrieb:
>
>> I'm trying to setup a test harness for a server which provides a REST
>> interface based on the Atom publishing protocol. Unfortunately I can't
>> get POST and PUT to work properly. Setting the Content-Header doesn't
>> seem to be supported.
>
> […]
>
> I patched HtmlUnit and WebTest (in a naive way) to make my tests run.
> Here's what works for me. Any comments are greatly appreciated, if I
> find the time I could try to polish the patches so that they might be
> considered for integrating in the codebase. TIA!
>
> HtmlUnit Patch
> ==============
>
> Index: src/main/java/com/gargoylesoftware/htmlunit/WebRequestSettings.java
> ===================================================================
> --- src/main/java/com/gargoylesoftware/htmlunit/WebRequestSettings.java
> (revision 3047)
> +++ src/main/java/com/gargoylesoftware/htmlunit/WebRequestSettings.java
> (working copy)
> @@ -201,8 +201,8 @@
> + "the two are mutually exclusive!";
> throw new RuntimeException(msg);
> }
> - if (httpMethod_ != HttpMethod.POST) {
> - final String msg = "The request body may only be set for
> POST requests!";
> + if (httpMethod_ != HttpMethod.POST && httpMethod_ !=
> HttpMethod.PUT) {
> + final String msg = "The request body may only be set for
> POST and PUT requests!";
> throw new RuntimeException(msg);
> }
> requestBody_ = requestBody;
> Index: src/main/java/com/gargoylesoftware/htmlunit/HttpWebConnection.java
> ===================================================================
> --- src/main/java/com/gargoylesoftware/htmlunit/HttpWebConnection.java
> (revision 3047)
> +++ src/main/java/com/gargoylesoftware/htmlunit/HttpWebConnection.java
> (working copy)
> @@ -221,7 +221,7 @@
> postMethod.addParameter(pair.getName(),
> pair.getValue());
> }
> }
> - else {
> + else if (webRequestSettings.getEncodingType() ==
> FormEncodingType.MULTIPART) {
> final List<PartBase> partList = new ArrayList<PartBase>();
> for (final NameValuePair pair :
> webRequestSettings.getRequestParameters()) {
> final PartBase newPart;
> @@ -241,6 +241,12 @@
> parts = partList.toArray(parts);
> method.setRequestEntity(new
> MultipartRequestEntity(parts, method.getParams()));
> }
> + else {
> + String body = webRequestSettings.getRequestBody();
> + String charset = webRequestSettings.getCharset();
> + String contentType =
> webRequestSettings.getAdditionalHeaders().get("Content-type");
> + method.setRequestEntity(new StringRequestEntity(body,
> contentType, charset));
> + }
> }
>
>
> WebTest Patch
> =============
>
> Index: src/main/java/com/canoo/webtest/steps/request/InvokePage.java
> ===================================================================
> --- src/main/java/com/canoo/webtest/steps/request/InvokePage.java
> (revision 46484)
> +++ src/main/java/com/canoo/webtest/steps/request/InvokePage.java
> (working copy)
> @@ -33,6 +33,7 @@
> * description="This step executes a request to a particular URL."
> */
> public class InvokePage extends AbstractTargetAction {
> + protected static final String DEFAULT_CONTENT_TYPE =
> "application/x-www-form-urlencoded";
> private String fUrl;
> private String fCompleteUrl;
> private String fMethod = "GET";
> @@ -40,6 +41,7 @@
> private String fContent;
>
> private String fSoapAction;
> + private String contentType;
>
> public String getMethod() {
> return fMethod;
> @@ -125,6 +127,14 @@
> public void setContent(final String content) {
> fContent = content;
> }
> +
> + public void setContentType(final String contentType) {
> + this.contentType = contentType;
> + }
> +
> + public String getContentType() {
> + return this.contentType;
> + }
>
> public String getSoapAction() {
> return fSoapAction;
> @@ -151,8 +161,9 @@
> }
>
> protected Page findTarget() throws IOException, SAXException {
> - if ("POST".equals(getMethod())) {
> - return findTargetByPost();
> + String method = getMethod();
> + if ("POST".equals(method) || "PUT".equals(method)) {
> + return findTarget(SubmitMethod.getInstance(method));
> }
> fCompleteUrl = getContext().getConfig().getUrlForPage(getUrl());
> final WebRequestSettings settings = new WebRequestSettings(new
> URL(fCompleteUrl));
> @@ -160,9 +171,9 @@
> return getResponse(settings);
> }
>
> - private Page findTargetByPost() throws IOException, SAXException {
> + private Page findTarget(SubmitMethod method) throws IOException,
> SAXException {
> String url = getContext().getConfig().getUrlForPage(getUrl());
> - WebRequestSettings settings = new WebRequestSettings(new
> URL(url), SubmitMethod.POST);
> + WebRequestSettings settings = new WebRequestSettings(new
> URL(url), method);
>
> // get default encoding
> final String charset = System.getProperty("file.encoding");
> @@ -174,7 +185,8 @@
> }
> else {
> // TODO: is this the correct Content-type for non-SOAP posts?
> - headers.put("Content-type",
> "application/x-www-form-urlencoded");
> + String type = this.contentType != null ? this.contentType :
> DEFAULT_CONTENT_TYPE;
> + headers.put("Content-type", type);
> }
> settings.setAdditionalHeaders(headers);
> final String content;
>
>
>
> -- Andreas
>
>
_______________________________________________
WebTest mailing list
[email protected]
http://lists.canoo.com/mailman/listinfo/webtest