Search in sources :

Example 46 with StaplerRequest

use of org.kohsuke.stapler.StaplerRequest in project gogs-webhook-plugin by jenkinsci.

the class GogsWebHookTest method whenEmptyJobInQueryStringMustReturnError.

@Test
public void whenEmptyJobInQueryStringMustReturnError() throws Exception {
    // Prepare the SUT
    File uniqueFile = File.createTempFile("webHookTest_", ".txt", new File("target"));
    StaplerRequest staplerRequest = Mockito.mock(RequestImpl.class);
    StaplerResponse staplerResponse = Mockito.mock(ResponseImpl.class);
    when(staplerRequest.getHeader("X-Gogs-Event")).thenReturn("push");
    when(staplerRequest.getQueryString()).thenReturn("job&foo=bar");
    // perform the testÃŽ
    performDoIndexTest(staplerRequest, staplerResponse, uniqueFile);
    // validate that everything was done as planed
    verify(staplerResponse).setStatus(404);
    String expectedOutput = "No value assigned to parameter 'job'";
    isExpectedOutput(uniqueFile, expectedOutput);
    log.info("Test succeeded.");
}
Also used : StaplerRequest(org.kohsuke.stapler.StaplerRequest) StaplerResponse(org.kohsuke.stapler.StaplerResponse) File(java.io.File) Test(org.junit.Test)

Example 47 with StaplerRequest

use of org.kohsuke.stapler.StaplerRequest in project workflow-cps-plugin by jenkinsci.

the class SnippetizerLink method getDisplayUrl.

/**
 * Get the actual URL to use in sidepanel.jelly. If {@link #getUrl()} is not absolute, this will try to get the
 * current Job context and return a url starting with that job's {@link Job#getUrl()} appended with {@link #getUrl()}.
 */
@Nonnull
public final String getDisplayUrl() {
    String u = getUrl();
    try {
        if (new URI(u).isAbsolute()) {
            return u;
        }
    } catch (URISyntaxException e) {
        LOGGER.log(Level.WARNING, "Failed to parse URL for " + u, e);
        return "";
    }
    StaplerRequest req = Stapler.getCurrentRequest();
    if (req == null) {
        return u;
    }
    Item i = req.findAncestorObject(Item.class);
    StringBuilder toAppend = new StringBuilder();
    toAppend.append(req.getContextPath());
    if (!req.getContextPath().endsWith("/")) {
        toAppend.append("/");
    }
    if (i == null) {
        toAppend.append(u);
    } else {
        toAppend.append(i.getUrl());
        if (!i.getUrl().endsWith("/")) {
            toAppend.append("/");
        }
        toAppend.append(u);
    }
    return toAppend.toString();
}
Also used : Item(hudson.model.Item) StaplerRequest(org.kohsuke.stapler.StaplerRequest) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) Nonnull(javax.annotation.Nonnull)

Example 48 with StaplerRequest

use of org.kohsuke.stapler.StaplerRequest in project generic-webhook-trigger-plugin by jenkinsci.

the class GenericWebHookRequestReceiverTest method testThatHeadersCanBeTransformedToListSeveralNames.

@Test
public void testThatHeadersCanBeTransformedToListSeveralNames() {
    final GenericWebHookRequestReceiver sut = new GenericWebHookRequestReceiver();
    final StaplerRequest request = mock(StaplerRequest.class);
    // 
    when(request.getHeaderNames()).thenReturn(new ArrayEnumeration(new String[] { "headerName1", "headerName2" }));
    // 
    when(request.getHeaders("headerName1")).thenReturn(new ArrayEnumeration(new String[] { "headerValue1" }));
    // 
    when(request.getHeaders("headerName2")).thenReturn(new ArrayEnumeration(new String[] { "headerValue2" }));
    final Map<String, List<String>> actual = sut.getHeaders(request);
    final Map<String, List<String>> expected = new HashMap<>();
    expected.put("headername1", newArrayList("headerValue1"));
    expected.put("headername2", newArrayList("headerValue2"));
    // 
    assertThat(actual).isEqualTo(expected);
}
Also used : Maps.newHashMap(com.google.common.collect.Maps.newHashMap) HashMap(java.util.HashMap) StaplerRequest(org.kohsuke.stapler.StaplerRequest) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) Test(org.junit.Test)

Example 49 with StaplerRequest

use of org.kohsuke.stapler.StaplerRequest in project generic-webhook-trigger-plugin by jenkinsci.

the class GenericWebHookRequestReceiverTest method testThatHeadersCanBeTransformedToListSeveralValues.

@Test
public void testThatHeadersCanBeTransformedToListSeveralValues() {
    final GenericWebHookRequestReceiver sut = new GenericWebHookRequestReceiver();
    final StaplerRequest request = mock(StaplerRequest.class);
    // 
    when(request.getHeaderNames()).thenReturn(new ArrayEnumeration(new String[] { "headerName1" }));
    // 
    when(request.getHeaders("headerName1")).thenReturn(new ArrayEnumeration(new String[] { "headerValue1", "headerValue2" }));
    final Map<String, List<String>> actual = sut.getHeaders(request);
    final Map<String, List<String>> expected = new HashMap<>();
    expected.put("headername1", newArrayList("headerValue1", "headerValue2"));
    // 
    assertThat(actual).isEqualTo(expected);
}
Also used : Maps.newHashMap(com.google.common.collect.Maps.newHashMap) HashMap(java.util.HashMap) StaplerRequest(org.kohsuke.stapler.StaplerRequest) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) Test(org.junit.Test)

Example 50 with StaplerRequest

use of org.kohsuke.stapler.StaplerRequest in project hudson-2.x by hudson.

the class AbstractModelObject method requirePOST.

/**
 * Convenience method to verify that the current request is a POST request.
 */
protected final void requirePOST() throws ServletException {
    StaplerRequest req = Stapler.getCurrentRequest();
    // invoked outside the context of servlet
    if (req == null)
        return;
    String method = req.getMethod();
    if (!method.equalsIgnoreCase("POST"))
        throw new ServletException("Must be POST, Can't be " + method);
}
Also used : ServletException(javax.servlet.ServletException) StaplerRequest(org.kohsuke.stapler.StaplerRequest)

Aggregations

StaplerRequest (org.kohsuke.stapler.StaplerRequest)79 Test (org.junit.Test)45 MultiBranchProject (jenkins.branch.MultiBranchProject)28 JSONObject (net.sf.json.JSONObject)20 GitContent (io.jenkins.blueocean.rest.impl.pipeline.scm.GitContent)18 ServiceException (io.jenkins.blueocean.commons.ServiceException)17 BufferedReader (java.io.BufferedReader)16 StringReader (java.io.StringReader)16 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)15 User (hudson.model.User)14 Mailer (hudson.tasks.Mailer)13 StaplerResponse (org.kohsuke.stapler.StaplerResponse)8 File (java.io.File)7 BitbucketScmSaveFileRequest (io.jenkins.blueocean.blueocean_bitbucket_pipeline.BitbucketScmSaveFileRequest)6 ScmFile (io.jenkins.blueocean.rest.impl.pipeline.scm.ScmFile)5 HashMap (java.util.HashMap)5 List (java.util.List)5 Nonnull (javax.annotation.Nonnull)5 Item (hudson.model.Item)4 ArrayList (java.util.ArrayList)4