コード例 #1
0
  public void shouldNotExecuteInHeaderMode() throws Exception {
    // given
    ModelAndView modelAndView = new ModelAndView("test-view");
    interceptor.setMode(RepInterceptor.RepMode.HEADER);

    // when
    interceptor.postHandle(mockRequest, mockResponse, mockHandlerMethod, modelAndView);

    // then
    assertTrue("Model should be empty", modelAndView.getModel().isEmpty());
    verifyZeroInteractions(mockMetaFormatter, mockCombinatorStrategy);
  }
コード例 #2
0
  @Test(expectedExceptions = IllegalArgumentException.class)
  public void shouldThrowExceptionIfHandlerIsNotHandlerMethod() throws Exception {
    // given

    // when
    interceptor.preHandle(mockRequest, mockResponse, new Object());

    // then
  }
コード例 #3
0
  public void shouldNotExecuteIfModelAndViewIsNull() throws Exception {
    // given

    // when
    interceptor.postHandle(mockRequest, mockResponse, mockHandlerMethod, null);

    // then
    verifyZeroInteractions(mockMetaFormatter, mockCombinatorStrategy);
  }
コード例 #4
0
  public void shouldWriteAttributeNotHeadersInMetaMode() throws Exception {
    // given
    interceptor.setMode(RepInterceptor.RepMode.META);
    when(mockCombinatorStrategy.combine(testMethod))
        .thenReturn(Lists.newArrayList(new RobotInfo("ua")));

    // when
    final boolean result = interceptor.preHandle(mockRequest, mockResponse, mockHandlerMethod);

    // then
    assertTrue("Interceptor should always return true", result);

    final Object attribute = mockRequest.getAttribute(RepInterceptor.ROBOTS_REQUEST_ATTRIBUTE);
    assertNotNull("Request attribute should be set", attribute);

    final List<String> headers = mockResponse.getHeaders(RepInterceptor.ROBOTS_HEADER_NAME);
    assertTrue("No headers should be written", headers.isEmpty());

    verify(mockCombinatorStrategy).combine(testMethod);
    verifyZeroInteractions(mockHeaderFormatter);
  }
コード例 #5
0
  public void shouldWriteAttributeAndHeadersInHeaderMode() throws Exception {
    // given
    interceptor.setMode(RepInterceptor.RepMode.HEADER);
    when(mockCombinatorStrategy.combine(testMethod))
        .thenReturn(Lists.newArrayList(new RobotInfo("ua")));

    // when
    final boolean result = interceptor.preHandle(mockRequest, mockResponse, mockHandlerMethod);

    // then
    assertTrue("Interceptor should always return true", result);

    final Object attribute = mockRequest.getAttribute(RepInterceptor.ROBOTS_REQUEST_ATTRIBUTE);
    assertNotNull("Request attribute should be set", attribute);

    final List<String> headers = mockResponse.getHeaders(RepInterceptor.ROBOTS_HEADER_NAME);
    assertEquals(1, headers.size());
    assertEquals("header-value", headers.get(0));

    verify(mockCombinatorStrategy).combine(testMethod);
    verify(mockHeaderFormatter).format(any(RobotInfo.class));
  }
コード例 #6
0
  public void shouldImmediatelyReturnIfNoRobotInfoFound() throws Exception {
    // given
    when(mockCombinatorStrategy.combine(testMethod)).thenReturn(Collections.<RobotInfo>emptyList());

    // when
    final boolean result = interceptor.preHandle(mockRequest, mockResponse, mockHandlerMethod);

    // then
    assertTrue("Interceptor should always return true", result);
    assertNull(
        "Request attribute should not be set",
        mockRequest.getAttribute(RepInterceptor.ROBOTS_REQUEST_ATTRIBUTE));
    assertFalse("No headers should be added", mockRequest.getHeaderNames().hasMoreElements());

    verify(mockCombinatorStrategy).combine(testMethod);
  }
コード例 #7
0
  public void shouldAddModelDataInMetaMode() throws Exception {
    // given
    ModelAndView modelAndView = new ModelAndView("test-view");
    mockRequest.setAttribute(
        RepInterceptor.ROBOTS_REQUEST_ATTRIBUTE, Lists.newArrayList(new RobotInfo("ua")));

    // when
    interceptor.postHandle(mockRequest, mockResponse, mockHandlerMethod, modelAndView);

    // then
    List<MetaTag> metaTags =
        ModelAndViewAssert.assertAndReturnModelAttributeOfType(
            modelAndView, RepInterceptor.ROBOTS_META_TAGS_ATTRIBUTE_NAME, List.class);
    assertEquals(1, metaTags.size());
    assertEquals("meta-name", metaTags.get(0).getName());
    assertEquals("meta-content", metaTags.get(0).getContent());
  }