use of org.kohsuke.stapler.StaplerRequest in project blueocean-plugin by jenkinsci.
the class BlueUrlTokenizer method parseCurrentRequest.
/**
* Parse the {@link Stapler#getCurrentRequest() current Stapler request} and return a {@link BlueUrlTokenizer} instance
* iff the URL is a Blue Ocean UI URL.
*
* @return A {@link BlueUrlTokenizer} instance iff the URL is a Blue Ocean UI URL, otherwise {@code null}.
* @throws IllegalStateException Called outside the scope of an active {@link StaplerRequest}.
*/
@CheckForNull
public static BlueUrlTokenizer parseCurrentRequest() throws IllegalStateException {
StaplerRequest currentRequest = Stapler.getCurrentRequest();
if (currentRequest == null) {
throw new IllegalStateException("Illegal call to BlueoceanUrl.parseCurrentRequest outside the scope of an active StaplerRequest.");
}
String path = currentRequest.getOriginalRequestURI();
String contextPath = currentRequest.getContextPath();
path = path.substring(contextPath.length());
return parse(path);
}
use of org.kohsuke.stapler.StaplerRequest in project gogs-webhook-plugin by jenkinsci.
the class GogsWebHookTest method whenQueryStringIsNullMustThrowException.
@Test
public void whenQueryStringIsNullMustThrowException() throws Exception {
// Prepare the SUT
StaplerRequest staplerRequest = Mockito.mock(RequestImpl.class);
StaplerResponse staplerResponse = Mockito.mock(ResponseImpl.class);
when(staplerRequest.getHeader("X-Gogs-Event")).thenReturn("push");
when(staplerRequest.getQueryString()).thenReturn(null);
GogsWebHook gogsWebHook = new GogsWebHook();
try {
gogsWebHook.doIndex(staplerRequest, staplerResponse);
} catch (NullPointerException e) {
String expectedErrMsg = "The queryString in the request is null";
assertEquals("Not the expected error message.", expectedErrMsg, e.getMessage());
log.info("call failed as expected.");
return;
}
fail("The call should have failed.");
}
use of org.kohsuke.stapler.StaplerRequest in project gogs-webhook-plugin by jenkinsci.
the class GogsWebHookTest method callDoIndexWithNullResponseMessageMustThrowException.
@Test
public void callDoIndexWithNullResponseMessageMustThrowException() throws IOException {
GogsWebHook gogsWebHook = new GogsWebHook();
StaplerRequest staplerRequest = Mockito.mock(RequestImpl.class);
try {
gogsWebHook.doIndex(staplerRequest, null);
} catch (NullPointerException e) {
String expectedErrMsg = "Null reply submitted to doIndex method";
assertEquals("Not the expected error message.", expectedErrMsg, e.getMessage());
log.info("call failed as expected.");
return;
}
fail("The call should have failed.");
}
use of org.kohsuke.stapler.StaplerRequest in project gogs-webhook-plugin by jenkinsci.
the class GogsWebHookTest method whenUriDoesNotContainUrlNameMustReturnError.
@Test
public void whenUriDoesNotContainUrlNameMustReturnError() 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=myJob");
MockServletInputStream inputStream = new MockServletInputStream("body");
when(staplerRequest.getInputStream()).thenReturn(inputStream);
when(staplerRequest.getRequestURI()).thenReturn("/badUri/aaa");
// perform the testÎ
performDoIndexTest(staplerRequest, staplerResponse, uniqueFile);
// validate that everything was done as planed
verify(staplerResponse).setStatus(404);
String expectedOutput = "No payload or URI contains invalid entries.";
isExpectedOutput(uniqueFile, expectedOutput);
log.info("Test succeeded.");
}
use of org.kohsuke.stapler.StaplerRequest in project gogs-webhook-plugin by jenkinsci.
the class GogsWebHookTest method whenEmptyJob2InQueryStringMustReturnError.
@Test
public void whenEmptyJob2InQueryStringMustReturnError() 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.");
}
Aggregations