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

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