View Javadoc

1   package it.com.atlassian.rest.error;
2   
3   import com.atlassian.rest.jersey.client.WebResourceFactory;
4   
5   import com.sun.jersey.api.client.ClientResponse;
6   
7   import org.junit.Test;
8   
9   import static org.junit.Assert.assertEquals;
10  import static org.junit.Assert.assertFalse;
11  import static org.junit.Assert.assertTrue;
12  
13  public class UncaughtErrorTest
14  {
15      private static final String MESSAGE = "my error detail string";
16      private static final String JSON_ERROR = "{\"message\":\"" + MESSAGE + "\",\"status-code\":500}";
17      private static final String XML_ERROR = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
18          + "<status><status-code>500</status-code><message>" + MESSAGE + "</message></status>";
19      
20      @Test
21      public void testUncaughtErrorResponseIsReturnedInHtmlByDefault()
22      {
23          final ClientResponse response = WebResourceFactory.anonymous()
24              .path("errors/uncaughtInternalError")
25              .queryParam("message", MESSAGE)
26              .get(ClientResponse.class);
27          assertEquals(500, response.getStatus());
28          assertTrue(response.getType().toString().startsWith("text/html"));
29          // The default HTML version of the error response does not include any error details;
30          // it's just the Tomcat "internal error" page.  This is probably desirable behavior
31          // since hitting a REST resource from a browser is not an intended use case.
32          assertFalse(response.getEntity(String.class).contains(MESSAGE));
33      }
34      
35      @Test
36      public void testUncaughtErrorResponseIsReturnedInXMLWhenXMLIsRequested()
37      {
38          final ClientResponse response = WebResourceFactory.anonymous()
39              .path("errors/uncaughtInternalError")
40              .queryParam("message", MESSAGE)
41              .accept("application/xml")
42              .get(ClientResponse.class);
43          assertEquals(500, response.getStatus());
44          assertEquals("application/xml", response.getType().toString());
45          assertEquals(XML_ERROR, response.getEntity(String.class));
46      }
47      
48      @Test
49      public void testUncaughtErrorResponseIsReturnedInJSONWhenJSONIsRequested()
50      {
51          final ClientResponse response = WebResourceFactory.anonymous()
52              .path("errors/uncaughtInternalError")
53              .queryParam("message", MESSAGE)
54              .accept("application/json")
55              .get(ClientResponse.class);
56          assertEquals(500, response.getStatus());
57          assertEquals("application/json", response.getType().toString());
58          assertEquals(JSON_ERROR, response.getEntity(String.class));
59      }
60  }