@ -24,6 +24,7 @@ import java.util.TimeZone;
import org.junit.Before ;
import org.junit.Before ;
import org.junit.Test ;
import org.junit.Test ;
import org.springframework.http.HttpHeaders ;
import org.springframework.http.ResponseEntity ;
import org.springframework.http.ResponseEntity ;
import org.springframework.stereotype.Controller ;
import org.springframework.stereotype.Controller ;
import org.springframework.test.web.Person ;
import org.springframework.test.web.Person ;
@ -56,20 +57,19 @@ public class HeaderAssertionTests {
private String minuteAgo ;
private String minuteAgo ;
private String secondLater ;
private MockMvc mockMvc ;
private MockMvc mockMvc ;
private final long currentTime = System . currentTimeMillis ( ) ;
private final long currentTime = System . currentTimeMillis ( ) ;
private SimpleDateFormat dateFormat ;
@Before
@Before
public void setup ( ) {
public void setup ( ) {
SimpleDateFormat dateFormat = new SimpleDateFormat ( "EEE, dd MMM yyyy HH:mm:ss zzz" , Locale . US ) ;
this . dateFormat = new SimpleDateFormat ( "EEE, dd MMM yyyy HH:mm:ss zzz" , Locale . US ) ;
dateFormat . setTimeZone ( TimeZone . getTimeZone ( "GMT" ) ) ;
this . dateFormat . setTimeZone ( TimeZone . getTimeZone ( "GMT" ) ) ;
this . now = dateFormat . format ( new Date ( this . currentTime ) ) ;
this . now = dateFormat . format ( new Date ( this . currentTime ) ) ;
this . minuteAgo = dateFormat . format ( new Date ( this . currentTime - ( 1000 * 60 ) ) ) ;
this . minuteAgo = dateFormat . format ( new Date ( this . currentTime - ( 1000 * 60 ) ) ) ;
this . secondLater = dateFormat . format ( new Date ( this . currentTime + 1000 ) ) ;
PersonController controller = new PersonController ( ) ;
PersonController controller = new PersonController ( ) ;
controller . setStubTimestamp ( this . currentTime ) ;
controller . setStubTimestamp ( this . currentTime ) ;
@ -164,29 +164,24 @@ public class HeaderAssertionTests {
this . mockMvc . perform ( get ( "/persons/1" ) ) . andExpect ( header ( ) . doesNotExist ( LAST_MODIFIED ) ) ;
this . mockMvc . perform ( get ( "/persons/1" ) ) . andExpect ( header ( ) . doesNotExist ( LAST_MODIFIED ) ) ;
}
}
@Test
public void stringWithIncorrectResponseHeaderValue ( ) throws Exception {
assertIncorrectResponseHeader ( header ( ) . string ( LAST_MODIFIED , secondLater ) , secondLater ) ;
}
@Test
public void stringWithMatcherAndIncorrectResponseHeaderValue ( ) throws Exception {
assertIncorrectResponseHeader ( header ( ) . string ( LAST_MODIFIED , equalTo ( secondLater ) ) , secondLater ) ;
}
@Test
public void dateValueWithIncorrectResponseHeaderValue ( ) throws Exception {
long unexpected = this . currentTime + 1000 ;
assertIncorrectResponseHeader ( header ( ) . dateValue ( LAST_MODIFIED , unexpected ) , secondLater ) ;
}
@Test ( expected = AssertionError . class )
@Test ( expected = AssertionError . class )
public void longValueWithIncorrectResponseHeaderValue ( ) throws Exception {
public void longValueWithIncorrectResponseHeaderValue ( ) throws Exception {
this . mockMvc . perform ( get ( "/persons/1" ) ) . andExpect ( header ( ) . longValue ( "X-Rate-Limiting" , 1 ) ) ;
this . mockMvc . perform ( get ( "/persons/1" ) ) . andExpect ( header ( ) . longValue ( "X-Rate-Limiting" , 1 ) ) ;
}
}
@Test
public void stringWithMatcherAndIncorrectResponseHeaderValue ( ) throws Exception {
long secondLater = this . currentTime + 1000 ;
String expected = this . dateFormat . format ( new Date ( secondLater ) ) ;
assertIncorrectResponseHeader ( header ( ) . string ( LAST_MODIFIED , expected ) , expected ) ;
assertIncorrectResponseHeader ( header ( ) . string ( LAST_MODIFIED , equalTo ( expected ) ) , expected ) ;
// Comparison by date uses HttpHeaders to format the date in the error message.
HttpHeaders headers = new HttpHeaders ( ) ;
headers . setDate ( "expected" , secondLater ) ;
assertIncorrectResponseHeader ( header ( ) . dateValue ( LAST_MODIFIED , secondLater ) , headers . getFirst ( "expected" ) ) ;
}
private void assertIncorrectResponseHeader ( ResultMatcher matcher , String unexpected ) throws Exception {
private void assertIncorrectResponseHeader ( ResultMatcher matcher , String expected ) throws Exception {
try {
try {
this . mockMvc . perform ( get ( "/persons/1" )
this . mockMvc . perform ( get ( "/persons/1" )
. header ( IF_MODIFIED_SINCE , minuteAgo ) )
. header ( IF_MODIFIED_SINCE , minuteAgo ) )
@ -201,15 +196,14 @@ public class HeaderAssertionTests {
// SPR-10659: ensure header name is in the message
// SPR-10659: ensure header name is in the message
// Unfortunately, we can't control formatting from JUnit or Hamcrest.
// Unfortunately, we can't control formatting from JUnit or Hamcrest.
assertMessageContains ( err , "Response header '" + LAST_MODIFIED + "'" ) ;
assertMessageContains ( err , "Response header '" + LAST_MODIFIED + "'" ) ;
assertMessageContains ( err , un expected) ;
assertMessageContains ( err , expected ) ;
assertMessageContains ( err , now ) ;
assertMessageContains ( err , this . now ) ;
}
}
}
}
private void assertMessageContains ( AssertionError error , String expected ) {
private void assertMessageContains ( AssertionError error , String expected ) {
String message = error . getMessage ( ) ;
assertTrue ( "Failure message should contain [" + expected + "], actual is [" + error . getMessage ( ) + "]" ,
assertTrue ( "Failure message should contain [" + expected + "], actual is [" + message + "]" ,
error . getMessage ( ) . contains ( expected ) ) ;
message . contains ( expected ) ) ;
}
}
@ -226,15 +220,11 @@ public class HeaderAssertionTests {
public ResponseEntity < Person > showEntity ( @PathVariable long id , WebRequest request ) {
public ResponseEntity < Person > showEntity ( @PathVariable long id , WebRequest request ) {
return ResponseEntity
return ResponseEntity
. ok ( )
. ok ( )
. lastModified ( calculateLastModified ( id ) )
. lastModified ( this . timestamp )
. header ( "X-Rate-Limiting" , "42" )
. header ( "X-Rate-Limiting" , "42" )
. header ( "Vary" , "foo" , "bar" )
. header ( "Vary" , "foo" , "bar" )
. body ( new Person ( "Jason" ) ) ;
. body ( new Person ( "Jason" ) ) ;
}
}
private long calculateLastModified ( long id ) {
return this . timestamp ;
}
}
}
}
}