Example usage for org.springframework.mock.web MockHttpServletResponse getHeader

List of usage examples for org.springframework.mock.web MockHttpServletResponse getHeader

Introduction

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

Prototype

@Override
@Nullable
public String getHeader(String name) 

Source Link

Document

Return the primary value for the given header as a String, if any.

Usage

From source file:org.terasoluna.gfw.web.logging.mdc.XTrackMDCPutFilterTest.java

@Test
public void testGetMDCValue_changed_by_initParam() throws ServletException {
    mockFilterConfig.addInitParameter("attributeName", "X-Hoge");
    xTrackMDCPutFilter.init(mockFilterConfig);
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    String xTrack = xTrackMDCPutFilter.getMDCValue(request, response);
    assertThat(xTrack, is(notNullValue()));
    assertThat(xTrack.matches("^[a-f0-9]{32}$"), is(true));
    assertThat(response.getHeader("X-Hoge"), is(xTrack));
    assertThat((String) request.getAttribute("X-Hoge"), is(xTrack));
}

From source file:se.vgregion.urlservice.controllers.BookmarkControllerTest.java

@Test
public void redirectWithExistingHash() throws IOException {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();

    ModelAndView mav = controller.redirect("foo", user.getUsername(), request, response);

    Assert.assertEquals(301, response.getStatus());
    Assert.assertEquals("http://example.com", response.getHeader("Location"));
    Assert.assertEquals("http://example.com", mav.getModel().get("longUrl"));
}

From source file:com.xemantic.tadedon.guice.servlet.mock.FakeServletContainerTest.java

/**
 * Tests {@link UriReturningServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)}.
 *
 * @throws IOException if an input or output error is detected during request handling
 * @throws ServletException if container cannot handle request. 
 *//*w w  w.  j  av  a 2 s  . co m*/
@Test
public void shouldReturnUriAndAddFooHeader() throws IOException, ServletException {
    // given
    MockHttpServletRequest request = servletContainer.newRequest("GET", "/UriReturningServlet");
    MockHttpServletResponse response = new MockHttpServletResponse();

    // when
    servletContainer.service(request, response);

    // then
    assertThat(response.getContentAsString(), is("/UriReturningServlet"));
    assertThat((String) response.getHeader("Foo"), is("foo"));
}

From source file:com.google.api.server.spi.handlers.ExplorerHandlerTest.java

private void testHandle(String scheme, int port, String expectedLocation) throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setScheme(scheme);//from w ww.ja  v a 2  s  .com
    request.setServerName("localhost");
    request.setServerPort(port);
    request.setRequestURI("/_ah/api/explorer/");
    MockHttpServletResponse response = new MockHttpServletResponse();
    ExplorerHandler handler = new ExplorerHandler();
    EndpointsContext context = new EndpointsContext("GET", "explorer", request, response);
    handler.handle(context);

    assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_FOUND);
    assertThat(response.getHeader("Location")).isEqualTo(expectedLocation);
}

From source file:org.terasoluna.gfw.web.logging.mdc.XTrackMDCPutFilterTest.java

@Test
public void testGetMDCValue_default_attributeName_set_in_http_request() throws ServletException {
    xTrackMDCPutFilter.init(mockFilterConfig);
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();

    request.addHeader("X-Track", "hoge");
    String xTrack = xTrackMDCPutFilter.getMDCValue(request, response);
    assertThat(xTrack, is(notNullValue()));
    assertThat(xTrack, is("hoge"));
    assertThat(response.getHeader("X-Track"), is("hoge"));
    assertThat((String) request.getAttribute("X-Track"), is("hoge"));
}

From source file:org.surfnet.oaaas.auth.AuthorizationServerFilterTest.java

@Test
public void testCorsHeadersPreflight() throws IOException, ServletException {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader(HttpHeaders.AUTHORIZATION, "bearer dummy-access-token");
    request.addHeader("Origin", "http://www.test.org");
    request.addHeader("Access-Control-Request-Method", "PATCH");
    request.addHeader("Access-Control-Request-Headers", HttpHeaders.AUTHORIZATION + ", X-Custom-Header");
    request.setMethod("OPTIONS");

    MockHttpServletResponse response = new MockHttpServletResponse();
    //null FilterChain as chain.filter to proceed the flow is not called with preflight
    filter.doFilter(request, response, null);

    assertEquals("86400", response.getHeader("Access-Control-Max-Age"));
    assertEquals(HttpHeaders.AUTHORIZATION + ", X-Custom-Header",
            response.getHeader("Access-Control-Allow-Headers"));
    assertEquals("GET, OPTIONS, HEAD, PUT, PATCH, POST, DELETE",
            response.getHeader("Access-Control-Allow-Methods"));
    assertEquals("*", response.getHeader("Access-Control-Allow-Origin"));

}

From source file:example.springdata.rest.headers.WebIntegrationTests.java

@Test
public void executeConditionalGetRequests() throws Exception {

    Customer customer = customers.findAll().iterator().next();
    URI uri = new UriTemplate("/customers/{id}").expand(customer.getId());

    MockHttpServletResponse response = mvc.perform(get(uri)).//
            andExpect(header().string(ETAG, is(notNullValue()))).//
            andExpect(header().string(LAST_MODIFIED, is(notNullValue()))).//
            andReturn().getResponse();/* w ww  .j a v  a2 s.  c  o m*/

    // ETag-based

    response = mvc.perform(get(uri).header(IF_NONE_MATCH, response.getHeader(ETAG))).//
            andExpect(status().isNotModified()).//
            andExpect(header().string(ETAG, is(notNullValue()))).//
            andExpect(header().string(LAST_MODIFIED, is(notNullValue()))).//
            andDo(document("if-none-match")).//
            andReturn().getResponse();

    // Last-modified-based

    mvc.perform(get(uri).header(IF_MODIFIED_SINCE, response.getHeader(LAST_MODIFIED))).//
            andExpect(status().isNotModified()).//
            andExpect(header().string(ETAG, is(notNullValue()))).//
            andExpect(header().string(LAST_MODIFIED, is(notNullValue()))).//
            andDo(document("if-modified-since"));
}

From source file:de.otto.jsonhome.controller.JsonHomeControllerTest.java

@Test
public void testGetHomeDocument() throws Exception {
    // given/* w w  w. j  a v a2 s  .com*/
    final JsonHomeController controller = jsonHomeController(
            ControllerWithRequestMappingAndLinkRelationTypeAtClassLevel.class, "http://example.org/");
    // when
    final MockHttpServletResponse response = new MockHttpServletResponse();
    final Map<String, ?> resourcesMap = controller.getAsApplicationJsonHome(response);
    // then
    assertEquals(response.getHeader("Cache-Control"), "max-age=3600");
    assertEquals(resourcesMap.size(), 1);
    assertTrue(resourcesMap.containsKey("resources"));
    @SuppressWarnings("unchecked")
    final Map<String, Map<String, ?>> resources = (Map<String, Map<String, ?>>) resourcesMap.get("resources");
    final Map<String, Object> expected = new HashMap<String, Object>();
    expected.put("representations", asList("text/html"));
    expected.put("allow", of(GET));
    final Object hints = resources.get("http://example.org/rel/foo").get("hints");
    assertEquals(hints, expected);
}

From source file:example.WebIntegrationTests.java

@Test
public void executeConditionalGetRequests() throws Exception {

    // TODO: 02 - Executing conditional requests

    Customer customer = customers.findAll().iterator().next();
    URI uri = new UriTemplate("/customers/{id}").expand(customer.getId());

    MockHttpServletResponse response = mvc.perform(get(uri)).//
            andExpect(header().string(ETAG, is(notNullValue()))).//
            andExpect(header().string(LAST_MODIFIED, is(notNullValue()))).//
            andReturn().getResponse();//from   w w  w  .  j  a v a  2  s .co m

    // ETag-based

    response = mvc.perform(get(uri).header(IF_NONE_MATCH, response.getHeader(ETAG))).//
            andExpect(status().isNotModified()).//
            andExpect(header().string(ETAG, is(notNullValue()))).//
            andExpect(header().string(LAST_MODIFIED, is(notNullValue()))).//
            andDo(document("if-none-match")).//
            andReturn().getResponse();

    // Last-modified-based

    mvc.perform(get(uri).header(IF_MODIFIED_SINCE, response.getHeader(LAST_MODIFIED))).//
            andExpect(status().isNotModified()).//
            andExpect(header().string(ETAG, is(notNullValue()))).//
            andExpect(header().string(LAST_MODIFIED, is(notNullValue()))).//
            andDo(document("if-modified-since"));
}

From source file:org.openmrs.ui.framework.IntegrationTest.java

@Test
public void integrationTest() throws Exception {
    MockHttpSession httpSession = new MockHttpSession();
    Session session = sessionFactory.getSession(httpSession);
    MockHttpServletResponse response = new MockHttpServletResponse();
    PageRequest req = new PageRequest("uiframework", "home", new MockHttpServletRequest(), response, session);

    String html = pageFactory.handle(req);
    System.out.println("Result = " + html);

    // should not be cached
    assertThat((String) response.getHeader("Cache-Control"), is("no-cache,no-store,must-revalidate"));
}