SwifterTestsHttpParser.swift 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. //
  2. // SwifterTests.swift
  3. // SwifterTests
  4. //
  5. // Copyright © 2016 Damian Kołakowski. All rights reserved.
  6. //
  7. import XCTest
  8. class SwifterTestsHttpParser: XCTestCase {
  9. func testRandomStuff() {
  10. do {
  11. let data = [UInt8]("1231245".utf8)
  12. try HttpIncomingDataPorcessor(0, { item in
  13. XCTAssert(false, "Http processor should not return a request object for invalid data .")
  14. }).process(data[0..<data.count])
  15. } catch {
  16. XCTAssert(false, "No exception")
  17. }
  18. }
  19. func testInvalidStatusLineChunk() {
  20. do {
  21. let data = [UInt8]("GET HTTP/1.0".utf8)
  22. try HttpIncomingDataPorcessor(0, { item in
  23. XCTAssert(false, "Http processor should not return a request object for invalid status line.")
  24. }).process(data[0..<data.count])
  25. } catch {
  26. XCTAssert(false, "No exception")
  27. }
  28. }
  29. func testValidStatusLineChunk() {
  30. do {
  31. let data = [UInt8]("GET / HTTP/1.0".utf8)
  32. try HttpIncomingDataPorcessor(0, { item in
  33. XCTAssert(false, "Http processor should not return a request object for valid status line only.")
  34. }).process(data[0..<data.count])
  35. } catch {
  36. XCTAssert(false, "No exception")
  37. }
  38. }
  39. func testStatusLineWithSingleNextLine() {
  40. do {
  41. let data = [UInt8]("GET / HTTP/1.0\r\n".utf8)
  42. try HttpIncomingDataPorcessor(0, { item in
  43. XCTAssert(false, "Http processor should not return if there is no double next line symbol.")
  44. }).process(data[0..<data.count])
  45. } catch {
  46. XCTAssert(false, "No exception")
  47. }
  48. }
  49. func testStatusLineWithDoubleNextLine() {
  50. do {
  51. var request: Request? = nil
  52. let data = [UInt8]("GET / HTTP/1.0\r\n\r\n".utf8)
  53. try HttpIncomingDataPorcessor(0, { item in
  54. request = item
  55. }).process(data[0..<data.count])
  56. XCTAssertEqual(request?.path, "/")
  57. XCTAssertEqual(request?.method, "GET")
  58. XCTAssertEqual(request?.httpVersion, .http10)
  59. } catch {
  60. XCTAssert(false, "There should be no crash for valid http request.")
  61. }
  62. }
  63. func testContentLengthZero() {
  64. do {
  65. var request: Request? = nil
  66. let data = [UInt8]("GET / HTTP/1.0\r\nContent-Length: 0\r\n\r\n".utf8)
  67. try HttpIncomingDataPorcessor(0, { item in
  68. request = item
  69. }).process(data[0..<data.count])
  70. XCTAssertEqual(request?.path, "/")
  71. XCTAssertEqual(request?.method, "GET")
  72. XCTAssertEqual(request?.body.count, 0)
  73. XCTAssertEqual(request?.httpVersion, .http10)
  74. } catch {
  75. XCTAssert(false, "No exception")
  76. }
  77. }
  78. func testContentLengthNonZero() {
  79. do {
  80. var request: Request? = nil
  81. let data = [UInt8]("GET / HTTP/1.0\r\nContent-Length: 5\r\n\r\n12345".utf8)
  82. try HttpIncomingDataPorcessor(0, { item in
  83. request = item
  84. }).process(data[0..<data.count])
  85. XCTAssertEqual(request?.path, "/")
  86. XCTAssertEqual(request?.method, "GET")
  87. XCTAssertEqual(request?.body.count, 5)
  88. XCTAssertEqual((request?.body)!, [49, 50, 51, 52, 53])
  89. XCTAssertEqual(request?.httpVersion, .http10)
  90. } catch {
  91. XCTAssert(false, "No exception")
  92. }
  93. }
  94. func testContentLengthWithBodyChunk() {
  95. do {
  96. var request: Request? = nil
  97. let data = [UInt8]("GET / HTTP/1.0\r\nContent-Length: 10\r\n\r\n123".utf8)
  98. try HttpIncomingDataPorcessor(0, { item in
  99. request = item
  100. }).process(data[0..<data.count])
  101. XCTAssertNil(request)
  102. } catch {
  103. XCTAssert(false, "No exception")
  104. }
  105. }
  106. func testContentProcessedInChunks() {
  107. do {
  108. var request: Request? = nil
  109. let processor = HttpIncomingDataPorcessor(0, { item in
  110. request = item
  111. })
  112. let chunk1 = [UInt8]("GET /chunk HTTP/1.0\r\nContent-Length: 20\r\n\r\n123".utf8)
  113. try processor.process(chunk1[0..<chunk1.count])
  114. XCTAssertNil(request)
  115. let chunk2 = [UInt8]("1234567890".utf8)
  116. try processor.process(chunk2[0..<chunk2.count])
  117. XCTAssertNil(request)
  118. let chunk3 = [UInt8]("1234567".utf8)
  119. try processor.process(chunk3[0..<chunk3.count])
  120. XCTAssertEqual(request?.path, "/chunk")
  121. XCTAssertEqual(request?.method, "GET")
  122. XCTAssertEqual(request?.body.count, 20)
  123. XCTAssertEqual(request?.httpVersion, .http10)
  124. } catch {
  125. XCTAssert(false, "No exception")
  126. }
  127. }
  128. func testHeaders() {
  129. do {
  130. var request: Request? = nil
  131. let data = [UInt8]("GET / HTTP/1.0\r\na: b\r\nc: d\r\nContent-Length: 0\r\n\r\n".utf8)
  132. try HttpIncomingDataPorcessor(0, { item in
  133. request = item
  134. }).process(data[0..<data.count])
  135. XCTAssertNotNil(request)
  136. XCTAssertEqual(request?.headers.first?.0, "a")
  137. XCTAssertEqual(request?.headers.first?.1, "b")
  138. XCTAssertEqual(request?.headers[1].0, "c")
  139. XCTAssertEqual(request?.headers[1].1, "d")
  140. } catch {
  141. XCTAssert(false, "No exception")
  142. }
  143. }
  144. func testPath() {
  145. do {
  146. var request: Request? = nil
  147. let data = [UInt8]("GET /a/b/c/d?1345678=1231 HTTP/1.0\r\nContent-Length: 0\r\n\r\n".utf8)
  148. try HttpIncomingDataPorcessor(0, { item in
  149. request = item
  150. }).process(data[0..<data.count])
  151. XCTAssertEqual(request?.path, "/a/b/c/d")
  152. XCTAssertEqual(request?.method, "GET")
  153. XCTAssertEqual(request?.body.count, 0)
  154. XCTAssertEqual(request?.httpVersion, .http10)
  155. } catch {
  156. XCTAssert(false, "No exception")
  157. }
  158. }
  159. func testPathWithComplexQuery() {
  160. do {
  161. var request: Request? = nil
  162. let data = [UInt8]("GET /a/b/c/d?key=value1?&key=???s HTTP/1.0\r\nContent-Length: 0\r\n\r\n".utf8)
  163. try HttpIncomingDataPorcessor(0, { item in
  164. request = item
  165. }).process(data[0..<data.count])
  166. XCTAssertEqual(request?.path, "/a/b/c/d")
  167. XCTAssertEqual(request?.method, "GET")
  168. XCTAssertEqual(request?.query[0].0, "key")
  169. XCTAssertEqual(request?.query[0].1, "value1?")
  170. XCTAssertEqual(request?.query[1].0, "key")
  171. XCTAssertEqual(request?.query[1].1, "???s")
  172. XCTAssertEqual(request?.body.count, 0)
  173. XCTAssertEqual(request?.httpVersion, .http10)
  174. } catch {
  175. XCTAssert(false, "No exception")
  176. }
  177. }
  178. }