SwifterTestsHttpParser.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // SwifterTests.swift
  3. // SwifterTests
  4. //
  5. // Copyright © 2016 Damian Kołakowski. All rights reserved.
  6. //
  7. import XCTest
  8. import Swifter
  9. class SwifterTestsHttpParser: XCTestCase {
  10. class TestSocket: Socket {
  11. var content = [UInt8]()
  12. var offset = 0
  13. init(_ content: String) {
  14. super.init(socketFileDescriptor: -1)
  15. self.content.append(contentsOf: [UInt8](content.utf8))
  16. }
  17. override func read() throws -> UInt8 {
  18. if offset < content.count {
  19. let value = self.content[offset]
  20. offset = offset + 1
  21. return value
  22. }
  23. throw SocketError.recvFailed("")
  24. }
  25. }
  26. func testParser() {
  27. let parser = HttpParser()
  28. do {
  29. let _ = try parser.readHttpRequest(TestSocket(""))
  30. XCTAssert(false, "Parser should throw an error if socket is empty.")
  31. } catch { }
  32. do {
  33. let _ = try parser.readHttpRequest(TestSocket("12345678"))
  34. XCTAssert(false, "Parser should throw an error if status line has single token.")
  35. } catch { }
  36. do {
  37. let _ = try parser.readHttpRequest(TestSocket("GET HTTP/1.0"))
  38. XCTAssert(false, "Parser should throw an error if status line has not enough tokens.")
  39. } catch { }
  40. do {
  41. let _ = try parser.readHttpRequest(TestSocket("GET / HTTP/1.0"))
  42. XCTAssert(false, "Parser should throw an error if there is no next line symbol.")
  43. } catch { }
  44. do {
  45. let _ = try parser.readHttpRequest(TestSocket("GET / HTTP/1.0"))
  46. XCTAssert(false, "Parser should throw an error if there is no next line symbol.")
  47. } catch { }
  48. do {
  49. let _ = try parser.readHttpRequest(TestSocket("GET / HTTP/1.0\r"))
  50. XCTAssert(false, "Parser should throw an error if there is no next line symbol.")
  51. } catch { }
  52. do {
  53. let _ = try parser.readHttpRequest(TestSocket("GET / HTTP/1.0\n"))
  54. XCTAssert(false, "Parser should throw an error if there is no 'Content-Length' header.")
  55. } catch { }
  56. do {
  57. let _ = try parser.readHttpRequest(TestSocket("GET / HTTP/1.0\r\nContent-Length: 0\r\n\r\n"))
  58. } catch {
  59. XCTAssert(false, "Parser should not throw any errors if there is a valid 'Content-Length' header.")
  60. }
  61. do {
  62. let _ = try parser.readHttpRequest(TestSocket("GET / HTTP/1.0\nContent-Length: 0\r\n\n"))
  63. } catch {
  64. XCTAssert(false, "Parser should not throw any errors if there is a valid 'Content-Length' header.")
  65. }
  66. do {
  67. let _ = try parser.readHttpRequest(TestSocket("GET / HTTP/1.0\nContent-Length: 5\n\n12345"))
  68. } catch {
  69. XCTAssert(false, "Parser should not throw any errors if there is a valid 'Content-Length' header.")
  70. }
  71. do {
  72. let _ = try parser.readHttpRequest(TestSocket("GET / HTTP/1.0\nContent-Length: 10\r\n\n"))
  73. XCTAssert(false, "Parser should throw an error if request' body is too short.")
  74. } catch { }
  75. var r = try? parser.readHttpRequest(TestSocket("GET / HTTP/1.0\nContent-Length: 10\n\n1234567890"))
  76. XCTAssert(r?.method == "GET", "Parser should extract HTTP method name from the status line.")
  77. XCTAssert(r?.path == "/", "Parser should extract HTTP path value from the status line.")
  78. XCTAssert(r?.headers["content-length"] == "10", "Parser should extract Content-Length header value.")
  79. r = try? parser.readHttpRequest(TestSocket("POST / HTTP/1.0\nContent-Length: 10\n\n1234567890"))
  80. XCTAssert(r?.method == "POST", "Parser should extract HTTP method name from the status line.")
  81. r = try? parser.readHttpRequest(TestSocket("GET / HTTP/1.0\nHeader1: 1\nHeader2: 2\nContent-Length: 0\n\n"))
  82. XCTAssert(r?.headers["header1"] == "1", "Parser should extract multiple headers from the request.")
  83. XCTAssert(r?.headers["header2"] == "2", "Parser should extract multiple headers from the request.")
  84. }
  85. }