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()); |
|
32 |
} |
|
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 |
} |