/jetty-spdy/spdy-core/src/test/java/org/eclipse/jetty/spdy/api/ClientUsageTest.java

https://bitbucket.org/beginnerjyh/jetty.project · Java · 260 lines · 194 code · 26 blank · 40 comment · 1 complexity · 447bf6cc98fcb11f346f6a653270bd31 MD5 · raw file

  1. //
  2. // ========================================================================
  3. // Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
  4. // ------------------------------------------------------------------------
  5. // All rights reserved. This program and the accompanying materials
  6. // are made available under the terms of the Eclipse Public License v1.0
  7. // and Apache License v2.0 which accompanies this distribution.
  8. //
  9. // The Eclipse Public License is available at
  10. // http://www.eclipse.org/legal/epl-v10.html
  11. //
  12. // The Apache License v2.0 is available at
  13. // http://www.opensource.org/licenses/apache2.0.php
  14. //
  15. // You may elect to redistribute this code under either of these licenses.
  16. // ========================================================================
  17. //
  18. package org.eclipse.jetty.spdy.api;
  19. import java.nio.charset.Charset;
  20. import java.util.concurrent.ExecutionException;
  21. import java.util.concurrent.TimeUnit;
  22. import java.util.concurrent.TimeoutException;
  23. import org.eclipse.jetty.spdy.StandardSession;
  24. import org.eclipse.jetty.util.Callback;
  25. import org.eclipse.jetty.util.Fields;
  26. import org.eclipse.jetty.util.Promise;
  27. import org.junit.Ignore;
  28. import org.junit.Test;
  29. @Ignore
  30. public class ClientUsageTest
  31. {
  32. @Test
  33. public void testClientRequestResponseNoBody() throws Exception
  34. {
  35. Session session = new StandardSession(SPDY.V2, null, null, null, null, null, null, 1, null, null, null);
  36. session.syn(new SynInfo(new Fields(), true), new StreamFrameListener.Adapter()
  37. {
  38. @Override
  39. public void onReply(Stream stream, ReplyInfo replyInfo)
  40. {
  41. // Do something with the response
  42. replyInfo.getHeaders().get("host");
  43. // Then issue another similar request
  44. try
  45. {
  46. stream.getSession().syn(new SynInfo(new Fields(), true), this);
  47. }
  48. catch (ExecutionException | InterruptedException | TimeoutException e)
  49. {
  50. throw new IllegalStateException(e);
  51. }
  52. }
  53. });
  54. }
  55. @Test
  56. public void testClientReceivesPush1() throws InterruptedException, ExecutionException, TimeoutException
  57. {
  58. Session session = new StandardSession(SPDY.V2, null, null, null, null, null, null, 1, null, null, null);
  59. session.syn(new SynInfo(new Fields(), true), new StreamFrameListener.Adapter()
  60. {
  61. public StreamFrameListener onPush(Stream stream, PushInfo pushInfo)
  62. {
  63. return new Adapter()
  64. {
  65. @Override
  66. public void onData(Stream stream, DataInfo dataInfo)
  67. {
  68. }
  69. };
  70. };
  71. @Override
  72. public void onReply(Stream stream, ReplyInfo replyInfo)
  73. {
  74. // Do something with the response
  75. replyInfo.getHeaders().get("host");
  76. // Then issue another similar request
  77. try
  78. {
  79. stream.getSession().syn(new SynInfo(new Fields(), true), this);
  80. }
  81. catch (ExecutionException | InterruptedException | TimeoutException e)
  82. {
  83. throw new IllegalStateException(e);
  84. }
  85. }
  86. });
  87. }
  88. @Test
  89. public void testClientReceivesPush2() throws InterruptedException, ExecutionException, TimeoutException
  90. {
  91. Session session = new StandardSession(SPDY.V2, null, null, null, null, null, null, 1, new SessionFrameListener.Adapter()
  92. {
  93. public StreamFrameListener onPush(Stream stream, PushInfo pushInfo)
  94. {
  95. return new StreamFrameListener.Adapter()
  96. {
  97. @Override
  98. public void onData(Stream stream, DataInfo dataInfo)
  99. {
  100. }
  101. };
  102. }
  103. }, null, null);
  104. session.syn(new SynInfo(new Fields(), true), new StreamFrameListener.Adapter()
  105. {
  106. @Override
  107. public void onReply(Stream stream, ReplyInfo replyInfo)
  108. {
  109. // Do something with the response
  110. replyInfo.getHeaders().get("host");
  111. // Then issue another similar request
  112. try
  113. {
  114. stream.getSession().syn(new SynInfo(new Fields(), true), this);
  115. }
  116. catch (ExecutionException | InterruptedException | TimeoutException e)
  117. {
  118. throw new IllegalStateException(e);
  119. }
  120. }
  121. });
  122. }
  123. @Test
  124. public void testClientRequestWithBodyResponseNoBody() throws Exception
  125. {
  126. Session session = new StandardSession(SPDY.V2, null, null, null, null, null, null, 1, null, null, null);
  127. Stream stream = session.syn(new SynInfo(5, TimeUnit.SECONDS, new Fields(), false, (byte)0),
  128. new StreamFrameListener.Adapter()
  129. {
  130. @Override
  131. public void onReply(Stream stream, ReplyInfo replyInfo)
  132. {
  133. // Do something with the response
  134. replyInfo.getHeaders().get("host");
  135. // Then issue another similar request
  136. try
  137. {
  138. stream.getSession().syn(new SynInfo(new Fields(), true), this);
  139. }
  140. catch (ExecutionException | InterruptedException | TimeoutException e)
  141. {
  142. throw new IllegalStateException(e);
  143. }
  144. }
  145. });
  146. // Send-and-forget the data
  147. stream.data(new StringDataInfo("data", true));
  148. }
  149. @Test
  150. public void testAsyncClientRequestWithBodyResponseNoBody() throws Exception
  151. {
  152. Session session = new StandardSession(SPDY.V2, null, null, null, null, null, null, 1, null, null, null);
  153. final String context = "context";
  154. session.syn(new SynInfo(new Fields(), false), new StreamFrameListener.Adapter()
  155. {
  156. @Override
  157. public void onReply(Stream stream, ReplyInfo replyInfo)
  158. {
  159. // Do something with the response
  160. replyInfo.getHeaders().get("host");
  161. // Then issue another similar request
  162. try
  163. {
  164. stream.getSession().syn(new SynInfo(new Fields(), true), this);
  165. }
  166. catch (ExecutionException | InterruptedException | TimeoutException e)
  167. {
  168. throw new IllegalStateException(e);
  169. }
  170. }
  171. }, new Promise.Adapter<Stream>()
  172. {
  173. @Override
  174. public void succeeded(Stream stream)
  175. {
  176. // Differently from JDK 7 AIO, there is no need to
  177. // have an explicit parameter for the context since
  178. // that is captured while the handler is created anyway,
  179. // and it is used only by the handler as parameter
  180. // The style below is fire-and-forget, since
  181. // we do not pass the handler nor we call get()
  182. // to wait for the data to be sent
  183. stream.data(new StringDataInfo(context, true), new Callback.Adapter());
  184. }
  185. }
  186. );
  187. }
  188. @Test
  189. public void testAsyncClientRequestWithBodyAndResponseWithBody() throws Exception
  190. {
  191. Session session = new StandardSession(SPDY.V2, null, null, null, null, null, null, 1, null, null, null);
  192. session.syn(new SynInfo(new Fields(), false), new StreamFrameListener.Adapter()
  193. {
  194. // The good of passing the listener to push() is that applications can safely
  195. // accumulate info from the reply headers to be used in the data callback,
  196. // e.g. content-type, charset, etc.
  197. @Override
  198. public void onReply(Stream stream, ReplyInfo replyInfo)
  199. {
  200. // Do something with the response
  201. Fields headers = replyInfo.getHeaders();
  202. int contentLength = headers.get("content-length").valueAsInt();
  203. stream.setAttribute("content-length", contentLength);
  204. if (!replyInfo.isClose())
  205. stream.setAttribute("builder", new StringBuilder());
  206. // May issue another similar request while waiting for data
  207. try
  208. {
  209. stream.getSession().syn(new SynInfo(new Fields(), true), this);
  210. }
  211. catch (ExecutionException | InterruptedException | TimeoutException e)
  212. {
  213. throw new IllegalStateException(e);
  214. }
  215. }
  216. @Override
  217. public void onData(Stream stream, DataInfo dataInfo)
  218. {
  219. StringBuilder builder = (StringBuilder)stream.getAttribute("builder");
  220. builder.append(dataInfo.asString("UTF-8", true));
  221. }
  222. }, new Promise.Adapter<Stream>()
  223. {
  224. @Override
  225. public void succeeded(Stream stream)
  226. {
  227. stream.data(new BytesDataInfo("wee".getBytes(Charset.forName("UTF-8")), false), new Callback.Adapter());
  228. stream.data(new StringDataInfo("foo", false), new Callback.Adapter());
  229. stream.data(new ByteBufferDataInfo(Charset.forName("UTF-8").encode("bar"), true), new Callback.Adapter());
  230. }
  231. }
  232. );
  233. }
  234. }