Search in sources :

Example 6 with StaplerResponse

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.");
}
Also used : StaplerRequest(org.kohsuke.stapler.StaplerRequest) StaplerResponse(org.kohsuke.stapler.StaplerResponse) File(java.io.File) Test(org.junit.Test)

Example 7 with StaplerResponse

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.");
}
Also used : StaplerRequest(org.kohsuke.stapler.StaplerRequest) StaplerResponse(org.kohsuke.stapler.StaplerResponse) File(java.io.File) Test(org.junit.Test)

Example 8 with StaplerResponse

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.");
}
Also used : StaplerRequest(org.kohsuke.stapler.StaplerRequest) StaplerResponse(org.kohsuke.stapler.StaplerResponse) File(java.io.File) Test(org.junit.Test)

Example 9 with StaplerResponse

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;
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) GeneralSecurityException(java.security.GeneralSecurityException) StaplerResponse(org.kohsuke.stapler.StaplerResponse) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) Cipher(javax.crypto.Cipher) ObjectOutputStream(java.io.ObjectOutputStream) IOException2(hudson.util.IOException2)

Example 10 with StaplerResponse

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);
}
Also used : ArrayList(java.util.ArrayList) StaplerResponse(org.kohsuke.stapler.StaplerResponse) UUID(java.util.UUID) FailingHttpStatusCodeException(com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException) BadCredentialsException(org.acegisecurity.BadCredentialsException) UsernameNotFoundException(org.acegisecurity.userdetails.UsernameNotFoundException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) CSSParseException(org.w3c.css.sac.CSSParseException) AbstractArtifactResolutionException(org.apache.maven.artifact.resolver.AbstractArtifactResolutionException) AuthenticationException(org.acegisecurity.AuthenticationException) SAXException(org.xml.sax.SAXException) DataAccessException(org.springframework.dao.DataAccessException) CSSException(org.w3c.css.sac.CSSException) MalformedURLException(java.net.MalformedURLException)

Aggregations

StaplerResponse (org.kohsuke.stapler.StaplerResponse)12 StaplerRequest (org.kohsuke.stapler.StaplerRequest)9 Test (org.junit.Test)8 File (java.io.File)7 IOException (java.io.IOException)2 URISyntaxException (java.net.URISyntaxException)2 ArrayList (java.util.ArrayList)2 FailingHttpStatusCodeException (com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException)1 CheckForNull (edu.umd.cs.findbugs.annotations.CheckForNull)1 NonNull (edu.umd.cs.findbugs.annotations.NonNull)1 Extension (hudson.Extension)1 Functions (hudson.Functions)1 PluginManager (hudson.PluginManager)1 Util (hudson.Util)1 InitMilestone (hudson.init.InitMilestone)1 Initializer (hudson.init.Initializer)1 ManagementLink (hudson.model.ManagementLink)1 Which (hudson.remoting.Which)1 ACL (hudson.security.ACL)1 ACLContext (hudson.security.ACLContext)1