HttpEntity and its subclasses insist on same target type for equality

Issue: SPR-12910
This commit is contained in:
Juergen Hoeller
2015-04-15 14:58:16 +02:00
parent 8b2d9951e0
commit af272c2124
6 changed files with 47 additions and 17 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
package org.springframework.http;
import java.net.URI;
import org.junit.Test;
import org.springframework.util.LinkedMultiValueMap;
@@ -86,12 +88,39 @@ public class HttpEntityTests {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.TEXT_PLAIN);
String body = "foo";
ResponseEntity<String> entity = new ResponseEntity<String>(body, headers, HttpStatus.OK);
assertEquals(body, entity.getBody());
assertEquals(MediaType.TEXT_PLAIN, entity.getHeaders().getContentType());
assertEquals("text/plain", entity.getHeaders().getFirst("Content-Type"));
assertEquals("text/plain", entity.getHeaders().getFirst("Content-Type"));
HttpEntity<String> httpEntity = new HttpEntity<String>(body, headers);
ResponseEntity<String> responseEntity = new ResponseEntity<String>(body, headers, HttpStatus.OK);
ResponseEntity<String> responseEntity2 = new ResponseEntity<String>(body, headers, HttpStatus.OK);
assertEquals(body, responseEntity.getBody());
assertEquals(MediaType.TEXT_PLAIN, responseEntity.getHeaders().getContentType());
assertEquals("text/plain", responseEntity.getHeaders().getFirst("Content-Type"));
assertEquals("text/plain", responseEntity.getHeaders().getFirst("Content-Type"));
assertFalse(httpEntity.equals(responseEntity));
assertFalse(responseEntity.equals(httpEntity));
assertTrue(responseEntity.equals(responseEntity2));
assertTrue(responseEntity2.equals(responseEntity));
}
@Test
public void requestEntity() throws Exception {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.TEXT_PLAIN);
String body = "foo";
HttpEntity<String> httpEntity = new HttpEntity<String>(body, headers);
RequestEntity<String> requestEntity = new RequestEntity<String>(body, headers, HttpMethod.GET, new URI("/"));
RequestEntity<String> requestEntity2 = new RequestEntity<String>(body, headers, HttpMethod.GET, new URI("/"));
assertEquals(body, requestEntity.getBody());
assertEquals(MediaType.TEXT_PLAIN, requestEntity.getHeaders().getContentType());
assertEquals("text/plain", requestEntity.getHeaders().getFirst("Content-Type"));
assertEquals("text/plain", requestEntity.getHeaders().getFirst("Content-Type"));
assertFalse(httpEntity.equals(requestEntity));
assertFalse(requestEntity.equals(httpEntity));
assertTrue(requestEntity.equals(requestEntity2));
assertTrue(requestEntity2.equals(requestEntity));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -148,4 +148,4 @@ public class RequestEntityTests {
}
}
}

View File

@@ -26,7 +26,6 @@ import org.junit.Test;
import static org.junit.Assert.*;
/**
* @author Arjen Poutsma
* @author Marcel Overdijk
@@ -210,7 +209,6 @@ public class ResponseEntityTests {
@Test
public void cacheControl() {
Integer entity = new Integer(42);
ResponseEntity<Integer> responseEntity =
@@ -229,7 +227,6 @@ public class ResponseEntityTests {
@Test
public void cacheControlNoCache() {
Integer entity = new Integer(42);
ResponseEntity<Integer> responseEntity =