use of org.kohsuke.stapler.StaplerResponse 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.StaplerResponse 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.");
}
use of org.kohsuke.stapler.StaplerResponse 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.StaplerResponse in project hudson-2.x by hudson.
the class AnnotatedLargeText method writeHtmlTo.
public long writeHtmlTo(long start, Writer w) throws IOException {
ConsoleAnnotationOutputStream caw = new ConsoleAnnotationOutputStream(w, createAnnotator(Stapler.getCurrentRequest()), context, charset);
long r = super.writeLogTo(start, caw);
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Cipher sym = Secret.getCipher("AES");
sym.init(Cipher.ENCRYPT_MODE, Hudson.getInstance().getSecretKeyAsAES128());
ObjectOutputStream oos = new ObjectOutputStream(new GZIPOutputStream(new CipherOutputStream(baos, sym)));
// send timestamp to prevent a replay attack
oos.writeLong(System.currentTimeMillis());
oos.writeObject(caw.getConsoleAnnotator());
oos.close();
StaplerResponse rsp = Stapler.getCurrentResponse();
if (rsp != null)
rsp.setHeader("X-ConsoleAnnotator", new String(Base64.encode(baos.toByteArray())));
} catch (GeneralSecurityException e) {
throw new IOException2(e);
}
return r;
}
use of org.kohsuke.stapler.StaplerResponse in project hudson-2.x by hudson.
the class HudsonTestCase method executeOnServer.
/**
* Executes the given closure on the server, by the servlet request handling thread,
* in the context of an HTTP request.
*
* <p>
* In {@link HudsonTestCase}, a thread that's executing the test code is different from the thread
* that carries out HTTP requests made through {@link WebClient}. But sometimes you want to
* make assertions and other calls with side-effect from within the request handling thread.
*
* <p>
* This method allows you to do just that. It is useful for testing some methods that
* require {@link StaplerRequest} and {@link StaplerResponse}, or getting the credential
* of the current user (via {@link Hudson#getAuthentication()}, and so on.
*
* @param c
* The closure to be executed on the server.
* @return
* The return value from the closure.
* @throws Exception
* If a closure throws any exception, that exception will be carried forward.
*/
public <V> V executeOnServer(final Callable<V> c) throws Exception {
final Exception[] t = new Exception[1];
// size 1 list
final List<V> r = new ArrayList<V>(1);
ClosureExecuterAction cea = hudson.getExtensionList(RootAction.class).get(ClosureExecuterAction.class);
UUID id = UUID.randomUUID();
cea.add(id, new Runnable() {
public void run() {
try {
StaplerResponse rsp = Stapler.getCurrentResponse();
rsp.setStatus(200);
rsp.setContentType("text/html");
r.add(c.call());
} catch (Exception e) {
t[0] = e;
}
}
});
createWebClient().goTo("closures/?uuid=" + id);
if (t[0] != null)
throw t[0];
return r.get(0);
}
Aggregations