use of org.kohsuke.stapler.StaplerRequest in project blueocean-plugin by jenkinsci.
the class BlueOceanWebURLBuilder method getTryBlueOceanURLs.
/**
* Get the {@link TryBlueOceanURLs} instance for the {@link ModelObject}
* associated with the current Stapler request.
*
* @return The {@link TryBlueOceanURLs} instance for the current classic
* Jenkins page. The URL to the Blue Ocean homepage is returned if a more
* appropriate URL is not found.
*/
@Nonnull
public static TryBlueOceanURLs getTryBlueOceanURLs() {
StaplerRequest staplerRequest = Stapler.getCurrentRequest();
List<Ancestor> list = staplerRequest.getAncestors();
// Blue Ocean page we can link onto.
for (int i = list.size() - 1; i >= 0; i--) {
Ancestor ancestor = list.get(i);
Object object = ancestor.getObject();
if (object instanceof ModelObject) {
String blueUrl = toBlueOceanURL((ModelObject) object);
if (blueUrl != null) {
if (object instanceof Item) {
return new TryBlueOceanURLs(blueUrl, ((Item) object).getUrl());
} else if (object instanceof Run) {
return new TryBlueOceanURLs(blueUrl, ((Run) object).getUrl());
} else {
return new TryBlueOceanURLs(blueUrl);
}
} else if (object instanceof Item) {
return new TryBlueOceanURLs(getBlueHome(), ((Item) object).getUrl());
}
}
}
// Otherwise just return Blue Ocean home.
return new TryBlueOceanURLs(getBlueHome());
}
use of org.kohsuke.stapler.StaplerRequest in project generic-webhook-trigger-plugin by jenkinsci.
the class GenericWebHookRequestReceiverTest method testThatHeadersCanBeTransformedToList.
@Test
public void testThatHeadersCanBeTransformedToList() {
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" }));
final Map<String, List<String>> actual = sut.getHeaders(request);
final Map<String, List<String>> expected = new HashMap<>();
expected.put("headername1", newArrayList("headerValue1"));
//
assertThat(actual).isEqualTo(expected);
}
use of org.kohsuke.stapler.StaplerRequest in project gogs-webhook-plugin by jenkinsci.
the class GogsWebHookTest method whenNoJobInQueryStringMustReturnError.
@Test
public void whenNoJobInQueryStringMustReturnError() 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("foo=bar&blaah=blaah");
// perform the testÎ
performDoIndexTest(staplerRequest, staplerResponse, uniqueFile);
// validate that everything was done as planed
verify(staplerResponse).setStatus(404);
String expectedOutput = "Parameter 'job' is missing.";
isExpectedOutput(uniqueFile, expectedOutput);
log.info("Test succeeded.");
}
use of org.kohsuke.stapler.StaplerRequest in project gogs-webhook-plugin by jenkinsci.
the class GogsWebHookTest method whenEmptyHeaderTypeMustReturnError.
@Test
public void whenEmptyHeaderTypeMustReturnError() 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);
// perform the test
performDoIndexTest(staplerRequest, staplerResponse, uniqueFile);
// validate that everything was done as planed
verify(staplerResponse).setStatus(403);
String expectedOutput = "Only push event can be accepted.";
isExpectedOutput(uniqueFile, expectedOutput);
log.info("Test succeeded.");
}
use of org.kohsuke.stapler.StaplerRequest in project gogs-webhook-plugin by jenkinsci.
the class GogsWebHookTest method whenWrongHeaderTypeMustReturnError.
@Test
public void whenWrongHeaderTypeMustReturnError() 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("junk");
// perform the testÎ
performDoIndexTest(staplerRequest, staplerResponse, uniqueFile);
// validate that everything was done as planed
verify(staplerResponse).setStatus(403);
String expectedOutput = "Only push event can be accepted.";
isExpectedOutput(uniqueFile, expectedOutput);
log.info("Test succeeded.");
}
Aggregations