mirror of https://github.com/jacekkow/keycloak-protocol-cas

Matthias Piepkorn
2017-11-14 1cf376b3768c36548320e52b0d08391d75833af0
commit | author | age
5ba0b0 1 package org.keycloak.protocol.cas;
MP 2
3 import org.jboss.resteasy.mock.MockHttpRequest;
4 import org.jboss.resteasy.mock.MockHttpResponse;
5 import org.jboss.resteasy.specimpl.RequestImpl;
6 import org.junit.Test;
7 import org.keycloak.protocol.cas.utils.ContentTypeHelper;
8
9 import javax.ws.rs.core.HttpHeaders;
10 import javax.ws.rs.core.MediaType;
11
12 import static org.junit.Assert.assertEquals;
13
14 public class ContentTypeHelperTest {
15     @Test
16     public void test() throws Exception {
17         assertEquals(MediaType.APPLICATION_XML_TYPE, get("http://example.com/", null).selectResponseType());
18         assertEquals(MediaType.APPLICATION_JSON_TYPE, get("http://example.com/?format=json", null).selectResponseType());
19         assertEquals(MediaType.APPLICATION_XML_TYPE, get("http://example.com/?format=xml", null).selectResponseType());
20         assertEquals(MediaType.APPLICATION_JSON_TYPE, get("http://example.com/?format=JSON", null).selectResponseType());
21         assertEquals(MediaType.APPLICATION_XML_TYPE, get("http://example.com/?format=XML", null).selectResponseType());
22
23         assertEquals(MediaType.APPLICATION_XML_TYPE, get("http://example.com/", MediaType.APPLICATION_XML).selectResponseType());
24         assertEquals(MediaType.APPLICATION_JSON_TYPE, get("http://example.com/?format=json", MediaType.APPLICATION_XML).selectResponseType());
25         assertEquals(MediaType.APPLICATION_XML_TYPE, get("http://example.com/?format=xml", MediaType.APPLICATION_XML).selectResponseType());
26
27         assertEquals(MediaType.APPLICATION_JSON_TYPE, get("http://example.com/", MediaType.APPLICATION_JSON).selectResponseType());
28         assertEquals(MediaType.APPLICATION_JSON_TYPE, get("http://example.com/?format=json", MediaType.APPLICATION_JSON).selectResponseType());
29         assertEquals(MediaType.APPLICATION_XML_TYPE, get("http://example.com/?format=xml", MediaType.APPLICATION_JSON).selectResponseType());
30
31         assertEquals(MediaType.APPLICATION_XML_TYPE, get("http://example.com/", MediaType.TEXT_PLAIN).selectResponseType());
de7d25 32         assertEquals(MediaType.APPLICATION_XML_TYPE, get("http://example.com/", "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2").selectResponseType());
5ba0b0 33     }
MP 34
35     private ContentTypeHelper get(String uri, String acceptHeader) throws Exception {
36         MockHttpRequest req = MockHttpRequest.get(uri);
37         MockHttpResponse res = new MockHttpResponse();
38         RequestImpl restReq = new RequestImpl(req, res);
39
40         if (acceptHeader != null) {
41             req = req.header(HttpHeaders.ACCEPT, acceptHeader);
42         }
43
44         return new ContentTypeHelper(req, restReq, req.getUri());
45     }
46 }