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.");
}
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();
}
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);
}
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);
}
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);
}
Aggregations