Example usage for org.springframework.mock.web MockHttpServletRequest MockHttpServletRequest

List of usage examples for org.springframework.mock.web MockHttpServletRequest MockHttpServletRequest

Introduction

In this page you can find the example usage for org.springframework.mock.web MockHttpServletRequest MockHttpServletRequest.

Prototype

public MockHttpServletRequest() 

Source Link

Document

Create a new MockHttpServletRequest with a default MockServletContext .

Usage

From source file:org.openmrs.module.reportingrest.web.resource.BaseEvaluatedResourceTest.java

protected RequestContext buildRequestContext(String... paramNamesAndValues) {
    MockHttpServletRequest request = new MockHttpServletRequest();
    for (int i = 0; i < paramNamesAndValues.length; i += 2) {
        request.addParameter(paramNamesAndValues[i], paramNamesAndValues[i + 1]);
    }// w  w  w.j a  v a  2 s. c o m
    RequestContext context = new RequestContext();
    context.setRequest(request);
    return context;
}

From source file:com.hp.autonomy.frontend.find.idol.view.IdolViewControllerTest.java

@Test
public void viewDocumentNotFound() {
    assertNotNull(viewController.handleViewDocumentNotFoundException(
            new ViewDocumentNotFoundException("some reference"), new MockHttpServletRequest(),
            new MockHttpServletResponse()));
}

From source file:com.thoughtworks.go.server.security.BasicAuthenticationFilterTest.java

@Before
public void setUp() throws Exception {
    errorMessage = "There was an error authenticating you. Please check the server logs, or contact your the go administrator.";
    httpRequest = new MockHttpServletRequest();
    httpResponse = new MockHttpServletResponse();
    filter = new BasicAuthenticationFilter();
}

From source file:fragment.web.TasksControllerTest.java

@Before
public void setUp() {
    map = new ModelMap();
    request = new MockHttpServletRequest();
}

From source file:org.sventon.web.ctrl.template.GetFileControllerTest.java

@Before
public void setUp() throws Exception {
    request = new MockHttpServletRequest();
    response = new MockHttpServletResponse();

    mockServletContext.setContextPath("sventon-test");

    repositoryName = new RepositoryName("test");

    configDirectory = TestUtils.getTestConfigDirectory();
    configDirectory.setCreateDirectories(false);
    configDirectory.setServletContext(mockServletContext);

    application = new Application(configDirectory);
    application.setConfigured(true);//from w ww .  j  a v  a 2 s . co m

    repositoryConfiguration = new RepositoryConfiguration(repositoryName.toString());
    application.addConfiguration(repositoryConfiguration);
}

From source file:com.xyxy.platform.modules.core.web.ServletsTest.java

@Test
public void getParametersStartingWith() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter("pre_a", "aa");
    request.addParameter("pre_b", "bb");
    request.addParameter("c", "c");
    Map<String, Object> result = Servlets.getParametersStartingWith(request, "pre_");
    assertThat(result).containsOnly(entry("a", "aa"), entry("b", "bb"));

    result = Servlets.getParametersStartingWith(request, "error_");
    assertThat(result).isEmpty();//from w w w  . j a v a 2  s.  co m

    result = Servlets.getParametersStartingWith(request, null);
    assertThat(result).hasSize(3);
}

From source file:com.sse.abtester.VariationAssignerTest.java

/**
 * Test n variants.// ww w .  j  a  v a 2 s. c  o m
 *
 * @param num_variants the num_variants
 */
public void testNVariants(int num_variants) {
    // make up some variants
    AbstractMap<String, IVariant<VariantBean>> coll = new HashMap<String, IVariant<VariantBean>>();

    for (int i = 0; i < num_variants; i++) {
        String name = "Bean_" + i;
        IVariant<VariantBean> vb = new VariantBean(name);
        vb.setDispatchable(true); // else non of the bean is
        // copied into normalized/weighted collections
        vb.setTargetFreq(1.0 / num_variants);
        vb.setVariationStrategy(new Default());
        coll.put(name, vb);
    }
    VariationAssigner<VariantBean> va = new VariationAssigner<VariantBean>();

    va.setIVariantCollection(coll);
    Frequency f = new Frequency();
    MockHttpServletRequest req = new MockHttpServletRequest();
    int TEST_LOOPS = 1000;
    double ALLOWABLE_DELTA = 100.0 / TEST_LOOPS; // rough check
    for (int i = 0; i < TEST_LOOPS; i++) {
        IVariant<VariantBean> assigned = va.enrollRequest(req);
        if (assigned != null)
            f.addValue(assigned.getName());
    }
    for (IVariant<VariantBean> vrb : coll.values()) {
        assertEquals(1.0 / num_variants, f.getPct(vrb.getName()), // NaN here can mean unknown name
                ALLOWABLE_DELTA);
    }
}

From source file:com.trenako.web.infrastructure.SearchRequestArgumentResolverTests.java

@Test
public void shouldResolveRequestAsSearchRequest() throws Exception {
    HttpServletRequest request = new MockHttpServletRequest();
    ((MockHttpServletRequest) request).setRequestURI("/trenako-web/rs/brand/acme/railway/fs");
    NativeWebRequest webRequest = mock(NativeWebRequest.class);
    when(webRequest.getNativeRequest()).thenReturn(request);

    WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class);
    when(binderFactory.createBinder(eq(webRequest), isA(SearchRequest.class), eq("")))
            .thenAnswer(new Answer<ExtendedServletRequestDataBinder>() {
                @Override//from w ww .j a va2 s  .  c  om
                public ExtendedServletRequestDataBinder answer(InvocationOnMock invocation) throws Throwable {
                    Object[] args = invocation.getArguments();
                    SearchRequest req = (SearchRequest) args[1];
                    return new ExtendedServletRequestDataBinder(req, "");
                }
            });

    Object obj = resolver.resolveArgument(parSearchRequest, null, webRequest, binderFactory);

    assertNotNull(obj);
    assertTrue(obj instanceof SearchRequest);

    SearchRequest expected = new SearchRequest();
    expected.setBrand("acme");
    expected.setRailway("fs");

    assertEquals(expected, (SearchRequest) obj);
}

From source file:com.ge.predix.acs.commons.web.RestErrorHandlerTest.java

@Test
public void testIllegalArgumentException() {
    RestErrorHandler errorHandler = new RestErrorHandler();

    HttpServletRequest request = new MockHttpServletRequest();
    HttpServletResponse response = new MockHttpServletResponse();
    Exception e = new IllegalArgumentException("Descriptive Error Message");

    ModelAndView errorResponse = errorHandler.createApiErrorResponse(e, request, response);

    // The default error status code for IllegalArgumentException is 400
    Assert.assertEquals(response.getStatus(), HttpStatus.BAD_REQUEST.value());

    Assert.assertNotNull(errorResponse);
    Assert.assertNotNull(errorResponse.getModel().get("ErrorDetails"));

    // Response payload with default error code and the
    // IllegalArgumentException's message
    assertRestApiErrorResponse(errorResponse, "FAILED", "Descriptive Error Message");
}

From source file:org.openmrs.module.feedback.web.AddFeedbackFormControllerTest.java

@Before
public void setUp() throws Exception {

    /* executed before the test is run */
    this.service = Context.getService(FeedbackService.class);
    this.controller = new FeedbackAdminListController();
    this.request = new MockHttpServletRequest();
    this.response = new MockHttpServletResponse();

    /* this file is in the same folder of test resources where the hibernate mapping file is located */
    initializeInMemoryDatabase();//from w w w .  jav a 2  s .  c  om
    executeDataSet("FeedbackDataset.xml");

    /* Sample data is loaded into the system */
    authenticate();
}