PageRenderTime 58ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/client/WebClientTest.java

https://github.com/nsaputro/cxf
Java | 241 lines | 188 code | 33 blank | 20 comment | 3 complexity | 13ebc6d0c34c0d8d0910b6842b5a7ca2 MD5 | raw file
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package org.apache.cxf.jaxrs.client;
  20. import java.net.URI;
  21. import org.apache.cxf.jaxrs.resources.BookInterface;
  22. import org.apache.cxf.jaxrs.resources.BookStore;
  23. import org.junit.Assert;
  24. import org.junit.Test;
  25. public class WebClientTest extends Assert {
  26. @Test
  27. public void testEncoding() {
  28. URI u = WebClient.create("http://foo").path("bar+ %2B").matrix("a", "value+ ")
  29. .query("b", "bv+ %2B").getCurrentURI();
  30. assertEquals("http://foo/bar+%20%2B;a=value+%20?b=bv%2B+%2B", u.toString());
  31. }
  32. @Test
  33. public void testExistingAsteriscs() {
  34. URI u = WebClient.create("http://foo/*").getCurrentURI();
  35. assertEquals("http://foo/*", u.toString());
  36. }
  37. @Test
  38. public void testAsteriscs() {
  39. URI u = WebClient.create("http://foo").path("*").getCurrentURI();
  40. assertEquals("http://foo/*", u.toString());
  41. }
  42. @Test
  43. public void testDoubleAsteriscs() {
  44. URI u = WebClient.create("http://foo").path("**").getCurrentURI();
  45. assertEquals("http://foo/**", u.toString());
  46. }
  47. @Test
  48. public void testBaseCurrentPath() {
  49. assertEquals(URI.create("http://foo"), WebClient.create("http://foo").getBaseURI());
  50. assertEquals(URI.create("http://foo"), WebClient.create("http://foo").getCurrentURI());
  51. }
  52. @Test
  53. public void testNewBaseCurrentPath() {
  54. WebClient wc = WebClient.create("http://foo");
  55. assertEquals(URI.create("http://foo"), wc.getBaseURI());
  56. assertEquals(URI.create("http://foo"), wc.getCurrentURI());
  57. wc.to("http://bar", false);
  58. assertEquals(URI.create("http://bar"), wc.getBaseURI());
  59. assertEquals(URI.create("http://bar"), wc.getCurrentURI());
  60. }
  61. @Test
  62. public void testForward() {
  63. WebClient wc = WebClient.create("http://foo");
  64. wc.to("http://foo/bar", true);
  65. assertEquals(URI.create("http://foo"), wc.getBaseURI());
  66. assertEquals(URI.create("http://foo/bar"), wc.getCurrentURI());
  67. }
  68. @Test
  69. public void testCompositePath() {
  70. WebClient wc = WebClient.create("http://foo");
  71. wc.path("/bar/baz/");
  72. assertEquals(URI.create("http://foo"), wc.getBaseURI());
  73. assertEquals(URI.create("http://foo/bar/baz/"), wc.getCurrentURI());
  74. }
  75. @Test(expected = IllegalArgumentException.class)
  76. public void testWrongForward() {
  77. WebClient wc = WebClient.create("http://foo");
  78. wc.to("http://bar", true);
  79. }
  80. @Test
  81. public void testBaseCurrentPathAfterChange() {
  82. WebClient wc = WebClient.create(URI.create("http://foo"));
  83. wc.path("bar").path("baz").matrix("m1", "m1value").query("q1", "q1value");
  84. assertEquals(URI.create("http://foo"), wc.getBaseURI());
  85. assertEquals(URI.create("http://foo/bar/baz;m1=m1value?q1=q1value"), wc.getCurrentURI());
  86. }
  87. @Test
  88. public void testBaseCurrentPathAfterCopy() {
  89. WebClient wc = WebClient.create(URI.create("http://foo"));
  90. wc.path("bar").path("baz").matrix("m1", "m1value").query("q1", "q1value");
  91. WebClient wc1 = WebClient.fromClient(wc);
  92. assertEquals(URI.create("http://foo/bar/baz;m1=m1value?q1=q1value"), wc1.getBaseURI());
  93. assertEquals(URI.create("http://foo/bar/baz;m1=m1value?q1=q1value"), wc1.getCurrentURI());
  94. }
  95. @Test
  96. public void testHeaders() {
  97. WebClient wc = WebClient.create(URI.create("http://foo"));
  98. wc.header("h1", "h1value").header("h2", "h2value");
  99. assertEquals(1, wc.getHeaders().get("h1").size());
  100. assertEquals("h1value", wc.getHeaders().getFirst("h1"));
  101. assertEquals(1, wc.getHeaders().get("h2").size());
  102. assertEquals("h2value", wc.getHeaders().getFirst("h2"));
  103. wc.getHeaders().clear();
  104. assertEquals(1, wc.getHeaders().get("h1").size());
  105. assertEquals("h1value", wc.getHeaders().getFirst("h1"));
  106. assertEquals(1, wc.getHeaders().get("h2").size());
  107. assertEquals("h2value", wc.getHeaders().getFirst("h2"));
  108. wc.reset();
  109. assertTrue(wc.getHeaders().isEmpty());
  110. }
  111. @Test
  112. public void testBackFast() {
  113. WebClient wc = WebClient.create(URI.create("http://foo"));
  114. wc.path("bar").path("baz").matrix("m1", "m1value");
  115. assertEquals(URI.create("http://foo"), wc.getBaseURI());
  116. assertEquals(URI.create("http://foo/bar/baz;m1=m1value"), wc.getCurrentURI());
  117. wc.back(true);
  118. assertEquals(URI.create("http://foo"), wc.getCurrentURI());
  119. }
  120. @Test
  121. public void testBack() {
  122. WebClient wc = WebClient.create(URI.create("http://foo"));
  123. wc.path("bar").path("baz");
  124. assertEquals(URI.create("http://foo"), wc.getBaseURI());
  125. assertEquals(URI.create("http://foo/bar/baz"), wc.getCurrentURI());
  126. wc.back(false);
  127. assertEquals(URI.create("http://foo/bar"), wc.getCurrentURI());
  128. wc.back(false);
  129. assertEquals(URI.create("http://foo"), wc.getCurrentURI());
  130. wc.back(false);
  131. assertEquals(URI.create("http://foo"), wc.getCurrentURI());
  132. }
  133. @Test
  134. public void testResetQueryAndBack() {
  135. WebClient wc = WebClient.create(URI.create("http://foo"));
  136. wc.path("bar").path("baz").query("foo", "bar");
  137. assertEquals(URI.create("http://foo"), wc.getBaseURI());
  138. assertEquals(URI.create("http://foo/bar/baz?foo=bar"), wc.getCurrentURI());
  139. wc.resetQuery().back(false);
  140. assertEquals(URI.create("http://foo/bar"), wc.getCurrentURI());
  141. }
  142. @Test
  143. public void testReplaceQuery() {
  144. WebClient wc = WebClient.create(URI.create("http://foo"));
  145. wc.path("bar").path("baz").query("foo", "bar");
  146. assertEquals(URI.create("http://foo"), wc.getBaseURI());
  147. assertEquals(URI.create("http://foo/bar/baz?foo=bar"), wc.getCurrentURI());
  148. wc.replaceQuery("foo1=bar1");
  149. assertEquals(URI.create("http://foo/bar/baz?foo1=bar1"), wc.getCurrentURI());
  150. }
  151. @Test
  152. public void testReplaceQueryParam() {
  153. WebClient wc = WebClient.create(URI.create("http://foo"));
  154. wc.path("bar").path("baz").query("foo", "bar").query("foo1", "bar1");
  155. assertEquals(URI.create("http://foo"), wc.getBaseURI());
  156. assertEquals(URI.create("http://foo/bar/baz?foo=bar&foo1=bar1"), wc.getCurrentURI());
  157. wc.replaceQueryParam("foo1", "baz");
  158. assertEquals(URI.create("http://foo/bar/baz?foo=bar&foo1=baz"), wc.getCurrentURI());
  159. }
  160. @Test
  161. public void testReplacePathAll() {
  162. WebClient wc = WebClient.create(URI.create("http://foo"));
  163. wc.path("bar").path("baz");
  164. assertEquals(URI.create("http://foo"), wc.getBaseURI());
  165. assertEquals(URI.create("http://foo/bar/baz"), wc.getCurrentURI());
  166. wc.replacePath("/new");
  167. assertEquals(URI.create("http://foo/new"), wc.getCurrentURI());
  168. }
  169. @Test
  170. public void testReplacePathLastSegment() {
  171. WebClient wc = WebClient.create(URI.create("http://foo"));
  172. wc.path("bar").path("baz");
  173. assertEquals(URI.create("http://foo"), wc.getBaseURI());
  174. assertEquals(URI.create("http://foo/bar/baz"), wc.getCurrentURI());
  175. wc.replacePath("new");
  176. assertEquals(URI.create("http://foo/bar/new"), wc.getCurrentURI());
  177. }
  178. @Test
  179. public void testFragment() {
  180. WebClient wc = WebClient.create(URI.create("http://foo"));
  181. wc.path("bar").path("baz").query("foo", "bar").fragment("1");
  182. assertEquals(URI.create("http://foo"), wc.getBaseURI());
  183. assertEquals(URI.create("http://foo/bar/baz?foo=bar#1"), wc.getCurrentURI());
  184. }
  185. @Test
  186. public void testPathWithTemplates() {
  187. WebClient wc = WebClient.create(URI.create("http://foo"));
  188. assertEquals(URI.create("http://foo"), wc.getBaseURI());
  189. assertEquals(URI.create("http://foo"), wc.getCurrentURI());
  190. wc.path("{bar}/{foo}", 1, 2);
  191. assertEquals(URI.create("http://foo"), wc.getBaseURI());
  192. assertEquals(URI.create("http://foo/1/2"), wc.getCurrentURI());
  193. }
  194. @Test
  195. public void testWebClientConfiguration() {
  196. WebClient wc = WebClient.create(URI.create("http://foo"));
  197. assertNotNull(WebClient.getConfig(wc) != null);
  198. }
  199. @Test
  200. public void testProxyConfiguration() {
  201. // interface
  202. BookInterface proxy = JAXRSClientFactory.create("http://foo", BookInterface.class);
  203. assertNotNull(WebClient.getConfig(proxy) != null);
  204. // cglib
  205. BookStore proxy2 = JAXRSClientFactory.create("http://foo", BookStore.class);
  206. assertNotNull(WebClient.getConfig(proxy2) != null);
  207. }
  208. }