Raise RestClientException for unknown status codes

HttpStatus cannot be created with an unknown status code. If a server
returns a status code that's not in the HttpStatus enum values, an
IllegalArgumentException is raised. Rather than allowing it to
propagate as such, this change ensures the actual exception raised is
a RestClientException.

Issue: SPR-9406
Backport-Issue: SPR-9502
This commit is contained in:
Rossen Stoyanchev
2012-06-12 14:54:16 -04:00
parent 1af7575bde
commit 24c30eac49
6 changed files with 179 additions and 76 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2012 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.
@@ -19,19 +19,23 @@ package org.springframework.web.client;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import org.junit.Before;
import org.junit.Test;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import org.junit.Before;
import org.junit.Test;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/** @author Arjen Poutsma */
import static org.junit.Assert.*;
/**
* Unit tests for {@link DefaultResponseErrorHandler}.
*
* @author Arjen Poutsma
*/
public class DefaultResponseErrorHandlerTests {
private DefaultResponseErrorHandler handler;
@@ -64,7 +68,7 @@ public class DefaultResponseErrorHandlerTests {
verify(response);
}
@Test(expected = HttpClientErrorException.class)
@Test
public void handleError() throws Exception {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.TEXT_PLAIN);
@@ -76,7 +80,13 @@ public class DefaultResponseErrorHandlerTests {
replay(response);
handler.handleError(response);
try {
handler.handleError(response);
fail("expected HttpClientErrorException");
}
catch (HttpClientErrorException e) {
assertSame(headers, e.getResponseHeaders());
}
verify(response);
}
@@ -114,4 +124,16 @@ public class DefaultResponseErrorHandlerTests {
verify(response);
}
// SPR-9406
@Test(expected=RestClientException.class)
public void unknownStatusCode() throws Exception {
expect(response.getStatusCode()).andThrow(new IllegalArgumentException("No matching constant for 999"));
expect(response.getRawStatusCode()).andReturn(999);
replay(response);
handler.handleError(response);
}
}