org.kohsuke.stapler.RequestImpl Java Examples

The following examples show how to use org.kohsuke.stapler.RequestImpl. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TokenReloadActionTest.java    From configuration-as-code-plugin with MIT License 6 votes vote down vote up
@Test
public void reloadReturnsUnauthorizedIfTokenDoesNotMatch() throws IOException {
    System.setProperty("casc.reload.token", "someSecretValue");

    RequestImpl request = newRequest(null);
    tokenReloadAction.doIndex(request, new ResponseImpl(null, response));

    assertEquals(401, response.getStatus());

    assertFalse(configWasReloaded());

    List<LogRecord> messages = loggerRule.getRecords();
    assertEquals(1, messages.size());
    assertEquals("Invalid token received, not reloading configuration", messages.get(0).getMessage());
    assertEquals(Level.WARNING, messages.get(0).getLevel());
}
 
Example #2
Source File: GogsWebHookTest.java    From gogs-webhook-plugin with MIT License 6 votes vote down vote up
@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.");
}
 
Example #3
Source File: GogsWebHookTest.java    From gogs-webhook-plugin with MIT License 6 votes vote down vote up
@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.");
}
 
Example #4
Source File: GogsWebHookTest.java    From gogs-webhook-plugin with MIT License 6 votes vote down vote up
@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.");
}
 
Example #5
Source File: GogsWebHookTest.java    From gogs-webhook-plugin with MIT License 6 votes vote down vote up
@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.");
}
 
Example #6
Source File: GogsWebHookTest.java    From gogs-webhook-plugin with MIT License 6 votes vote down vote up
@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.");
}
 
Example #7
Source File: GogsWebHookTest.java    From gogs-webhook-plugin with MIT License 6 votes vote down vote up
@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.");
}
 
Example #8
Source File: GogsWebHookTest.java    From gogs-webhook-plugin with MIT License 6 votes vote down vote up
@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.");
}
 
Example #9
Source File: TokenReloadActionTest.java    From configuration-as-code-plugin with MIT License 5 votes vote down vote up
@Test
public void reloadIsDisabledByDefault() throws IOException {
    System.clearProperty("casc.reload.token");

    RequestImpl request = newRequest(null);
    tokenReloadAction.doIndex(request, new ResponseImpl(null, response));

    assertEquals(404, response.getStatus());

    List<LogRecord> messages = loggerRule.getRecords();
    assertEquals(1, messages.size());
    assertEquals("Configuration reload via token is not enabled", messages.get(0).getMessage());
    assertEquals(Level.WARNING, messages.get(0).getLevel());
    assertFalse(configWasReloaded());
}
 
Example #10
Source File: GogsWebHookTest.java    From gogs-webhook-plugin with MIT License 5 votes vote down vote up
@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.");
}
 
Example #11
Source File: TemplateStaplerRequestWrapper.java    From multi-branch-project-plugin with MIT License 5 votes vote down vote up
/**
 * Constructs this extension of {@link RequestImpl} under the assumption that {@link RequestImpl} is also the
 * underlying type of the {@link StaplerRequest}.
 *
 * @param request the request submitted the {@link TemplateDrivenMultiBranchProject}
 */
TemplateStaplerRequestWrapper(StaplerRequest request) {
    /*
     * Ugly casts to RequestImpl... but should be ok since it will throw
     * errors, which we want anyway if it's not that type.
     */
    super(request.getStapler(), request, ((RequestImpl) request).ancestors,
            ((RequestImpl) request).tokens);
}
 
Example #12
Source File: TokenReloadActionTest.java    From configuration-as-code-plugin with MIT License 4 votes vote down vote up
private RequestImpl newRequest(String authorization) {
    return new RequestImpl(null, new RequestStub(authorization), Collections.emptyList(), null);
}