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

/src/fitnesse/http/ChunkedResponseTest.java

http://github.com/unclebob/fitnesse
Java | 164 lines | 136 code | 26 blank | 2 comment | 0 complexity | 4db05f07b257e008f6489a6afaf5726a MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, GPL-2.0
  1. // Copyright (C) 2003-2009 by Object Mentor, Inc. All rights reserved.
  2. // Released under the terms of the CPL Common Public License version 1.0.
  3. package fitnesse.http;
  4. import static junit.framework.Assert.assertEquals;
  5. import static junit.framework.Assert.assertFalse;
  6. import static junit.framework.Assert.fail;
  7. import static org.junit.Assert.assertTrue;
  8. import static util.RegexTestCase.assertHasRegexp;
  9. import static util.RegexTestCase.assertSubString;
  10. import java.io.UnsupportedEncodingException;
  11. import java.net.Socket;
  12. import org.junit.After;
  13. import org.junit.Before;
  14. import org.junit.Test;
  15. public class ChunkedResponseTest implements ResponseSender {
  16. private ChunkedResponse response;
  17. private boolean closed = false;
  18. public StringBuffer buffer;
  19. @Override
  20. public void send(byte[] bytes) {
  21. try {
  22. buffer.append(new String(bytes, "UTF-8"));
  23. } catch (UnsupportedEncodingException e) {
  24. throw new RuntimeException("Error in encoding", e);
  25. }
  26. }
  27. @Override
  28. public void close() {
  29. closed = true;
  30. }
  31. @Override
  32. public Socket getSocket() {
  33. return null;
  34. }
  35. @Before
  36. public void setUp() throws Exception {
  37. buffer = new StringBuffer();
  38. response = new ChunkedResponse("html", new MockChunkedDataProvider());
  39. response.sendTo(this);
  40. }
  41. @After
  42. public void tearDown() throws Exception {
  43. response.closeAll();
  44. }
  45. @Test
  46. public void testHeaders() throws Exception {
  47. String text = buffer.toString();
  48. assertHasRegexp("Transfer-Encoding: chunked", text);
  49. assertTrue(text.startsWith("HTTP/1.1 200 OK\r\n"));
  50. assertHasRegexp("Content-Type: text/html", text);
  51. }
  52. @Test
  53. public void xmlHeaders() throws Exception {
  54. response = new ChunkedResponse("xml", new MockChunkedDataProvider());
  55. response.sendTo(this);
  56. String text = buffer.toString();
  57. assertHasRegexp("Transfer-Encoding: chunked", text);
  58. assertTrue(text.startsWith("HTTP/1.1 200 OK\r\n"));
  59. assertHasRegexp("Content-Type: text/xml", text);
  60. }
  61. @Test
  62. public void testOneChunk() throws Exception {
  63. buffer = new StringBuffer();
  64. response.add("some more text");
  65. String text = buffer.toString();
  66. assertEquals("e\r\nsome more text\r\n", text);
  67. }
  68. @Test
  69. public void testTwoChunks() throws Exception {
  70. buffer = new StringBuffer();
  71. response.add("one");
  72. response.add("two");
  73. String text = buffer.toString();
  74. assertEquals("3\r\none\r\n3\r\ntwo\r\n", text);
  75. }
  76. @Test
  77. public void testSimpleClosing() throws Exception {
  78. assertFalse(closed);
  79. buffer = new StringBuffer();
  80. response.closeAll();
  81. String text = buffer.toString();
  82. assertEquals("0\r\n\r\n", text);
  83. assertTrue(closed);
  84. }
  85. @Test
  86. public void testClosingInSteps() throws Exception {
  87. assertFalse(closed);
  88. buffer = new StringBuffer();
  89. response.closeChunks();
  90. assertEquals("0\r\n", buffer.toString());
  91. assertFalse(closed);
  92. buffer = new StringBuffer();
  93. response.closeTrailer();
  94. assertEquals("\r\n", buffer.toString());
  95. assertFalse(closed);
  96. response.close();
  97. assertTrue(closed);
  98. }
  99. @Test
  100. public void testContentSize() throws Exception {
  101. response.add("12345");
  102. response.closeAll();
  103. assertEquals(5, response.getContentSize());
  104. }
  105. @Test
  106. public void testNoNullPointerException() throws Exception {
  107. String s = null;
  108. try {
  109. response.add(s);
  110. }
  111. catch (Exception e) {
  112. fail("should not throw exception");
  113. }
  114. }
  115. @Test
  116. public void testTrailingHeaders() throws Exception {
  117. response.closeChunks();
  118. buffer = new StringBuffer();
  119. response.addTrailingHeader("Some-Header", "someValue");
  120. assertEquals("Some-Header: someValue\r\n", buffer.toString());
  121. response.closeTrailer();
  122. response.close();
  123. assertTrue(closed);
  124. }
  125. @Test
  126. public void testCantAddZeroLengthBytes() throws Exception {
  127. int originalLength = buffer.length();
  128. response.add("");
  129. assertEquals(originalLength, buffer.length());
  130. response.closeAll();
  131. }
  132. @Test
  133. public void testUnicodeCharacters() throws Exception {
  134. response.add("\uba80\uba81\uba82\uba83");
  135. response.closeAll();
  136. assertSubString("\uba80\uba81\uba82\uba83", buffer.toString());
  137. }
  138. }