/**
  * Tests that configuring an existing project via jenkins http rest doesn't produce duplicated
  * triggers and that the trigger is configured for the new project pattern.
  *
  * @throws Exception if so
  */
 @Test
 @LocalData
 public void testReconfigureUsingRestApi() throws Exception {
   assertNrOfEventListeners(0);
   TopLevelItem testProj = j.jenkins.getItem("testProj");
   String gerritProjectPattern = "someotherproject";
   XmlPage xmlPage = loadConfigXmlViaHttp(testProj);
   Document document = xmlPage.getXmlDocument();
   String xml = changeConfigXml(gerritProjectPattern, document);
   URL url = UrlUtils.toUrlUnsafe(j.getURL().toExternalForm() + testProj.getUrl() + "config.xml");
   WebRequestSettings request = new WebRequestSettings(url, HttpMethod.POST);
   request.setRequestBody(xml);
   j.jenkins.setCrumbIssuer(null);
   Page page = j.createWebClient().getPage(request);
   j.assertGoodStatus(page);
   assertNrOfEventListeners(0);
   assertEventListenerWithSomeOtherProjectSet(gerritProjectPattern);
 }
  private void ProcessTest(WebTest test) {
    HtmlPage page = null;

    // Orville.LOG().info(String.format("Executing test %s\n", event));

    WebClient webClient = new WebClient();
    webClient.setRefreshHandler(new ThreadedRefreshHandler());
    webClient.getOptions().setJavaScriptEnabled(false);

    try {
      if (test.getMethod().toLowerCase().equals("post")) {
        WebRequest request =
            new WebRequest(UrlUtils.toUrlUnsafe(test.getUrlString()), HttpMethod.POST);

        request.setRequestParameters(new ArrayList<NameValuePair>());

        for (Map.Entry<String, String> entry : test.getPostData().entrySet()) {
          request.getRequestParameters().add(new NameValuePair(entry.getKey(), entry.getValue()));
        }

        page = webClient.getPage(request);
      } else {
        page = webClient.getPage(test.getUrlString());
      }

      ProcessResult(test, page.getWebResponse());

    } catch (FailingHttpStatusCodeException fsc) {
      Orville.LOG()
          .warning(
              String.format(
                  "Failing HTTP Status code caught executing the test command: %s\n",
                  fsc.getMessage()));
    } catch (IOException ioe) {
      Orville.LOG()
          .warning(
              String.format(
                  "IO Exception caught executing the test command: %s\n", ioe.getMessage()));
    }

    webClient.closeAllWindows();
  }
Example #3
0
  /**
   * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN
   * RISK.</span><br>
   * Gets the request for a submission of this form with the specified SubmittableElement.
   *
   * @param submitElement the element that caused the submit to occur
   * @return the request
   */
  public WebRequest getWebRequest(final SubmittableElement submitElement) {
    final HtmlPage htmlPage = (HtmlPage) getPage();
    final List<NameValuePair> parameters = getParameterListForSubmit(submitElement);
    final HttpMethod method;
    final String methodAttribute = getMethodAttribute();
    if ("post".equalsIgnoreCase(methodAttribute)) {
      method = HttpMethod.POST;
    } else {
      if (!"get".equalsIgnoreCase(methodAttribute) && StringUtils.isNotBlank(methodAttribute)) {
        notifyIncorrectness("Incorrect submit method >" + getMethodAttribute() + "<. Using >GET<.");
      }
      method = HttpMethod.GET;
    }

    final BrowserVersion browser = getPage().getWebClient().getBrowserVersion();
    String actionUrl = getActionAttribute();
    String anchor = null;
    String queryFromFields = "";
    if (HttpMethod.GET == method) {
      if (actionUrl.contains("#")) {
        anchor = StringUtils.substringAfter(actionUrl, "#");
      }
      final String enc = getPage().getPageEncoding();
      queryFromFields =
          URLEncodedUtils.format(Arrays.asList(NameValuePair.toHttpClient(parameters)), enc);

      // action may already contain some query parameters: they have to be removed
      actionUrl = StringUtils.substringBefore(actionUrl, "#");
      actionUrl = StringUtils.substringBefore(actionUrl, "?");
      parameters.clear(); // parameters have been added to query
    }
    URL url;
    try {
      if (actionUrl.isEmpty()) {
        url = WebClient.expandUrl(htmlPage.getUrl(), actionUrl);
      } else {
        url = htmlPage.getFullyQualifiedUrl(actionUrl);
      }

      if (queryFromFields.length() > 0) {
        url = UrlUtils.getUrlWithNewQuery(url, queryFromFields);
      }

      if (HttpMethod.GET == method
          && browser.hasFeature(FORM_SUBMISSION_URL_WITHOUT_HASH)
          && WebClient.URL_ABOUT_BLANK != url) {
        url = UrlUtils.getUrlWithNewRef(url, null);
      } else if (HttpMethod.POST == method
          && browser.hasFeature(FORM_SUBMISSION_URL_WITHOUT_HASH)
          && WebClient.URL_ABOUT_BLANK != url
          && StringUtils.isEmpty(actionUrl)) {
        url = UrlUtils.getUrlWithNewRef(url, null);
      } else if (anchor != null && WebClient.URL_ABOUT_BLANK != url) {
        url = UrlUtils.getUrlWithNewRef(url, anchor);
      }
    } catch (final MalformedURLException e) {
      throw new IllegalArgumentException("Not a valid url: " + actionUrl);
    }

    final WebRequest request = new WebRequest(url, method);
    request.setRequestParameters(parameters);
    if (HttpMethod.POST == method) {
      request.setEncodingType(FormEncodingType.getInstance(getEnctypeAttribute()));
    }
    request.setCharset(getSubmitCharset());
    request.setAdditionalHeader("Referer", htmlPage.getUrl().toExternalForm());
    return request;
  }