Codota Logo
MockHttpServletRequestBuilder.servletPath
Code IndexAdd Codota to your IDE (free)

How to use
servletPath
method
in
org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder

Best Java code snippets using org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder.servletPath (Showing top 17 results out of 315)

  • Common ways to obtain MockHttpServletRequestBuilder
private void myMethod () {
MockHttpServletRequestBuilder m =
  • Codota IconMockMvcRequestBuilders.get("<changeme>")
  • Codota IconString httpMethod;URI url;new MockHttpServletRequestBuilder(httpMethod, url)
  • Smart code suggestions by Codota
}
origin: spring-projects/spring-framework

private void testContextPathServletPathInvalid(String contextPath, String servletPath, String message) {
  try {
    this.builder.contextPath(contextPath);
    this.builder.servletPath(servletPath);
    this.builder.buildRequest(this.servletContext);
  }
  catch (IllegalArgumentException ex) {
    assertEquals(message, ex.getMessage());
  }
}
origin: spring-projects/spring-framework

@Test
public void testRequestAttributeEqualTo() throws Exception {
  this.mockMvc.perform(get("/main/1").servletPath("/main"))
    .andExpect(request().attribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE, "/{id}"))
    .andExpect(request().attribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "/1"))
    .andExpect(request().attribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE, equalTo("/{id}")))
    .andExpect(request().attribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, equalTo("/1")));
}
origin: spring-projects/spring-framework

@Test
public void contextPathServletPathInfo() {
  this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/");
  this.builder.servletPath("/index.html");
  this.builder.pathInfo(null);
  MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
  assertEquals("", request.getContextPath());
  assertEquals("/index.html", request.getServletPath());
  assertNull(request.getPathInfo());
}
origin: spring-projects/spring-framework

@Test
public void contextPathServletPathInfoEmpty() {
  this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/travel/hotels/42");
  this.builder.contextPath("/travel");
  this.builder.servletPath("/hotels/42");
  MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
  assertEquals("/travel", request.getContextPath());
  assertEquals("/hotels/42", request.getServletPath());
  assertNull(request.getPathInfo());
}
origin: spring-projects/spring-framework

@Test
public void contextPathServletPath() {
  this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/travel/main/hotels/42");
  this.builder.contextPath("/travel");
  this.builder.servletPath("/main");
  MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
  assertEquals("/travel", request.getContextPath());
  assertEquals("/main", request.getServletPath());
  assertEquals("/hotels/42", request.getPathInfo());
}
origin: spring-projects/spring-security

@Test
public void requestWhenUsingMvcMatchersAndServletPathThenAuthorizesRequestsAccordingly()
    throws Exception {
  this.spring.configLocations(this.xml("MvcMatchersServletPath")).autowire();
  MockServletContext servletContext = mockServletContext("/spring");
  ConfigurableWebApplicationContext context =
      (ConfigurableWebApplicationContext) this.spring.getContext();
  context.setServletContext(servletContext);
  this.mvc.perform(get("/spring/path").servletPath("/spring"))
      .andExpect(status().isUnauthorized());
  this.mvc.perform(get("/spring/path.html").servletPath("/spring"))
      .andExpect(status().isUnauthorized());
  this.mvc.perform(get("/spring/path/").servletPath("/spring"))
      .andExpect(status().isUnauthorized());
}
origin: cloudfoundry/uaa

@Test
void multiple_oidc_providers_use_response_type_in_url(
    @Autowired JdbcIdentityProviderProvisioning jdbcIdentityProviderProvisioning
) throws Exception {
  String subdomain = "oidc-idp-discovery-multi-" + generator.generate().toLowerCase();
  IdentityZone zone = MultitenancyFixture.identityZone(subdomain, subdomain);
  createOtherIdentityZone(zone.getSubdomain(), mockMvc, webApplicationContext, false);
  createOIDCProvider(jdbcIdentityProviderProvisioning, generator, zone, null);
  createOIDCProvider(jdbcIdentityProviderProvisioning, generator, zone, "code id_token");
  mockMvc.perform(get("/login")
      .header("Accept", TEXT_HTML)
      .servletPath("/login")
      .with(new SetServerNameRequestPostProcessor(zone.getSubdomain() + ".localhost")))
      .andExpect(status().isOk())
      .andExpect(content().string(containsString("http://myauthurl.com?client_id=id&amp;response_type=code&")))
      .andExpect(content().string(containsString("http://myauthurl.com?client_id=id&amp;response_type=code+id_token&")));
}
origin: cloudfoundry/uaa

  @Test
  public void testUserInfoEndpointIsCorrect() throws Exception {
    for (String host : Arrays.asList("localhost", "subdomain.localhost")) {
      for (String url : Arrays.asList("/.well-known/openid-configuration", "/oauth/token/.well-known/openid-configuration")) {
        MockHttpServletResponse response = mockMvc.perform(
          get(url)
            .header("Host", host)
            .servletPath(url)
            .with(new SetServerNameRequestPostProcessor(host))
            .accept(APPLICATION_JSON))
          .andExpect(status().isOk())
          .andReturn().getResponse();

        OpenIdConfiguration openIdConfiguration = JsonUtils.readValue(response.getContentAsString(), OpenIdConfiguration.class);

        mockMvc.perform(get(openIdConfiguration.getUserInfoUrl()))
          .andExpect(status().isUnauthorized());
      }
    }
  }
}
origin: cloudfoundry/uaa

.accept(TEXT_HTML)
.session(session)
.servletPath("/login")
.with(new SetServerNameRequestPostProcessor(identityZone.getSubdomain() + ".localhost"))
origin: cloudfoundry/uaa

@Test
void idpDiscoveryRedirectsToOIDCProvider(
    @Autowired JdbcIdentityProviderProvisioning jdbcIdentityProviderProvisioning
) throws Exception {
  String subdomain = "oidc-discovery-" + generator.generate().toLowerCase();
  IdentityZone zone = MultitenancyFixture.identityZone(subdomain, subdomain);
  createOtherIdentityZone(zone.getSubdomain(), mockMvc, webApplicationContext, false);
  String originKey = createOIDCProvider(jdbcIdentityProviderProvisioning, generator, zone, "id_token code");
  MvcResult mvcResult = mockMvc.perform(post("/login/idp_discovery")
      .with(cookieCsrf())
      .header("Accept", TEXT_HTML)
      .servletPath("/login/idp_discovery")
      .param("email", "marissa@test.org")
      .with(new SetServerNameRequestPostProcessor(zone.getSubdomain() + ".localhost")))
      .andExpect(status().isFound())
      .andReturn();
  String location = mvcResult.getResponse().getHeader("Location");
  Map<String, String> queryParams =
      UriComponentsBuilder.fromUriString(location).build().getQueryParams().toSingleValueMap();
  assertThat(location, startsWith("http://myauthurl.com"));
  assertThat(queryParams, hasEntry("client_id", "id"));
  assertThat(queryParams, hasEntry("response_type", "id_token+code"));
  assertThat(queryParams, hasEntry("redirect_uri", "http%3A%2F%2F" + subdomain + ".localhost%2Flogin%2Fcallback%2F" + originKey));
  assertThat(queryParams, hasKey("nonce"));
}
origin: cloudfoundry/uaa

.servletPath("/login")
.with(new SetServerNameRequestPostProcessor(identityZone.getSubdomain() + ".localhost")))
.andExpect(status().isFound())
origin: cloudfoundry/uaa

.servletPath("/login")
.with(new SetServerNameRequestPostProcessor(identityZone.getSubdomain() + ".localhost")))
.andExpect(status().isFound())
origin: cloudfoundry/uaa

get(url)
  .header("Host", host)
  .servletPath(url)
  .with(new SetServerNameRequestPostProcessor(host))
  .accept(APPLICATION_JSON))
origin: cloudfoundry/uaa

.servletPath("/login")
.with(new SetServerNameRequestPostProcessor(identityZone.getSubdomain() + ".localhost")))
.andExpect(status().isFound())
origin: cloudfoundry/uaa

.servletPath("/.well-known/openid-configuration")
.accept(APPLICATION_JSON))
.andExpect(status().isOk())
origin: otto-de/edison-microservice

@Test
public void shouldTriggerJobAndReturnItsURL() throws Exception {
  when(jobService.startAsyncJob("someJobType")).thenReturn(Optional.of("theJobId"));
  mockMvc.perform(MockMvcRequestBuilders
      .post("/some-microservice/internal/jobs/someJobType")
      .servletPath("/internal/jobs/someJobType"))
      .andExpect(status().is(204))
      .andExpect(header().string("Location", "http://localhost/some-microservice/internal/jobs/theJobId"));
  verify(jobService).startAsyncJob("someJobType");
}
origin: otto-de/edison-microservice

@Test
public void shouldReturnJobIfJobExists() throws Exception {
  // given
  ZoneId cet = ZoneId.of("CET");
  OffsetDateTime now = OffsetDateTime.now(cet).truncatedTo(ChronoUnit.MILLIS);
  JobInfo expectedJob = newJobInfo("42", "TEST", fixed(now.toInstant(), cet), "localhost");
  when(jobService.findJob("42")).thenReturn(Optional.of(expectedJob));
  String nowAsString = ISO_OFFSET_DATE_TIME.format(now);
  mockMvc.perform(MockMvcRequestBuilders
      .get("/some-microservice/internal/jobs/42")
      .servletPath("/internal/jobs/42"))
      .andExpect(status().is(200))
      .andExpect(jsonPath("$.status").value("OK"))
      .andExpect(jsonPath("$.messages").isArray())
      .andExpect(jsonPath("$.jobType").value("TEST"))
      .andExpect(jsonPath("$.hostname").value("localhost"))
      .andExpect(jsonPath("$.started").value(nowAsString))
      .andExpect(jsonPath("$.stopped").value(""))
      .andExpect(jsonPath("$.lastUpdated").value(nowAsString))
      .andExpect(jsonPath("$.jobUri").value("http://localhost/some-microservice/internal/jobs/42"))
      .andExpect(jsonPath("$.links").isArray())
      .andExpect(jsonPath("$.links[0].href").value("http://localhost/some-microservice/internal/jobs/42"))
      .andExpect(jsonPath("$.links[1].href").value("http://localhost/some-microservice/internal/jobdefinitions/TEST"))
      .andExpect(jsonPath("$.links[2].href").value("http://localhost/some-microservice/internal/jobs"))
      .andExpect(jsonPath("$.links[3].href").value("http://localhost/some-microservice/internal/jobs?type=TEST"))
      .andExpect(jsonPath("$.runtime").value("00:00:00"))
      .andExpect(jsonPath("$.state").value("Running"));
  verify(jobService).findJob("42");
}
org.springframework.test.web.servlet.requestMockHttpServletRequestBuilderservletPath

Javadoc

Specify the portion of the requestURI that represents the path to which the Servlet is mapped. This is typically a portion of the requestURI after the context path.

In most cases, tests can be written by omitting the servlet path from the requestURI. This is because most applications don't actually depend on the prefix to which a servlet is mapped. For example if a Servlet is mapped to "/main/*", tests can be written with the requestURI "/accounts/1" as opposed to "/main/accounts/1". If specified here, the servletPath must start with a "/" and must not end with a "/".

Popular methods of MockHttpServletRequestBuilder

  • contentType
    Set the 'Content-Type' header of the request.
  • content
    Set the request body.
  • param
    Add a request parameter to the MockHttpServletRequest.If called more than once, new values get added
  • accept
    Set the 'Accept' header to the given media type(s).
  • header
    Add a header to the request. Values are always added.
  • with
    An extension point for further initialization of MockHttpServletRequestin ways not built directly in
  • requestAttr
    Set a request attribute.
  • buildRequest
    Build a MockHttpServletRequest.
  • contextPath
    Specify the portion of the requestURI that represents the context path. The context path, if specifi
  • principal
    Set the principal of the request.
  • flashAttr
    Set an "input" flash attribute.
  • headers
    Add all headers to the request. Values are always added.
  • flashAttr,
  • headers,
  • session,
  • sessionAttr,
  • cookie,
  • params,
  • <init>,
  • characterEncoding,
  • locale

Popular in Java

  • Reactive rest calls using spring rest template
  • getSupportFragmentManager (FragmentActivity)
  • scheduleAtFixedRate (ScheduledExecutorService)
  • compareTo (BigDecimal)
    Compares this BigDecimal with the specified BigDecimal. Two BigDecimal objects that are equal in val
  • SQLException (java.sql)
    An exception that indicates a failed JDBC operation. It provides the following information about pro
  • Queue (java.util)
    A collection designed for holding elements prior to processing. Besides basic java.util.Collection o
  • SortedMap (java.util)
    A map that has its keys ordered. The sorting is according to either the natural ordering of its keys
  • StringTokenizer (java.util)
    The string tokenizer class allows an application to break a string into tokens. The tokenization met
  • Callable (java.util.concurrent)
    A task that returns a result and may throw an exception. Implementors define a single method with no
  • XPath (javax.xml.xpath)
    XPath provides access to the XPath evaluation environment and expressions. Evaluation of XPath Expr
Codota Logo
  • Products

    Search for Java codeSearch for JavaScript codeEnterprise
  • IDE Plugins

    IntelliJ IDEAWebStormAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimAtomGoLandRubyMineEmacsJupyter
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogCodota Academy Plugin user guide Terms of usePrivacy policyJava Code IndexJavascript Code Index
Get Codota for your IDE now