View Javadoc

1   package it.com.atlassian.rest.json;
2   
3   import com.atlassian.rest.jersey.client.WebResourceFactory;
4   import com.sun.jersey.api.client.WebResource;
5   import com.sun.jersey.core.util.MultivaluedMapImpl;
6   import org.junit.Before;
7   import org.junit.Test;
8   
9   import javax.ws.rs.core.MediaType;
10  import javax.ws.rs.core.MultivaluedMap;
11  
12  import static com.atlassian.rest.jersey.client.WebResourceFactory.REST_VERSION;
13  import static it.com.atlassian.rest.test.ProjectsTest.JSONP_CALLBACK_PARAMETER_NAME;
14  import static it.com.atlassian.rest.test.ProjectsTest.JSONP_CALLBACK_TEST_FUNCTION_NAME;
15  import static org.junit.Assert.assertEquals;
16  import static org.junit.Assert.assertFalse;
17  
18  /**
19   * Json with Padding must be disabled in the REST plugin module for these tests to pass.
20   * Currently this is done by using the following system property {@link com.atlassian.plugins.rest.module.json.JsonWithPaddingResponseFilter#ATLASSIAN_ALLOW_JSONP}
21   * to 'false', or making sure that the property is not set, since 'false' is the default behaviour.
22   */
23  public class JsonWithPaddingTest {
24      private WebResource webResourceWithVersion;
25  
26      @Before
27      public void setUp() {
28          webResourceWithVersion = WebResourceFactory.authenticated(REST_VERSION).path("projects");
29      }
30  
31      @Test
32      public void testGetProjectsJsonPWithXmlAcceptHeader() {
33          final MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl();
34          queryParams.putSingle(JSONP_CALLBACK_PARAMETER_NAME, JSONP_CALLBACK_TEST_FUNCTION_NAME);
35  
36          final String projectsJsonP = webResourceWithVersion.queryParams(queryParams).accept(MediaType.APPLICATION_XML_TYPE).get(String.class);
37  
38          assertFalse(projectsJsonP.startsWith(JSONP_CALLBACK_TEST_FUNCTION_NAME));
39      }
40  
41      @Test
42      public void testGetProjectsJsonPWithJsonAcceptHeader() {
43          final String projectsJson = webResourceWithVersion.accept(MediaType.APPLICATION_JSON_TYPE).get(String.class);
44  
45          final MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl();
46          queryParams.putSingle(JSONP_CALLBACK_PARAMETER_NAME, JSONP_CALLBACK_TEST_FUNCTION_NAME);
47  
48          final String projectsJsonP = webResourceWithVersion.queryParams(queryParams).accept(MediaType.APPLICATION_JSON_TYPE).get(String.class);
49  
50          assertEquals(JSONP_CALLBACK_TEST_FUNCTION_NAME + "(" + projectsJson + ");", projectsJsonP);
51      }
52  
53  }