HTTPVersionParserTests.swift 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright 2020, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. @testable import GRPC
  17. import NIO
  18. import XCTest
  19. class HTTPVersionParserTests: GRPCTestCase {
  20. private let preface = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
  21. func testHTTP2ExactlyTheRightBytes() {
  22. let buffer = ByteBuffer(string: self.preface)
  23. XCTAssertTrue(HTTPVersionParser.prefixedWithHTTP2ConnectionPreface(buffer))
  24. }
  25. func testHTTP2TheRightBytesAndMore() {
  26. var buffer = ByteBuffer(string: self.preface)
  27. buffer.writeRepeatingByte(42, count: 1024)
  28. XCTAssertTrue(HTTPVersionParser.prefixedWithHTTP2ConnectionPreface(buffer))
  29. }
  30. func testHTTP2NoBytes() {
  31. let empty = ByteBuffer()
  32. XCTAssertFalse(HTTPVersionParser.prefixedWithHTTP2ConnectionPreface(empty))
  33. }
  34. func testHTTP2NotEnoughBytes() {
  35. var buffer = ByteBuffer(string: self.preface)
  36. buffer.moveWriterIndex(to: buffer.writerIndex - 1)
  37. XCTAssertFalse(HTTPVersionParser.prefixedWithHTTP2ConnectionPreface(buffer))
  38. }
  39. func testHTTP2EnoughOfTheWrongBytes() {
  40. let buffer = ByteBuffer(string: String(self.preface.reversed()))
  41. XCTAssertFalse(HTTPVersionParser.prefixedWithHTTP2ConnectionPreface(buffer))
  42. }
  43. func testHTTP1RequestLine() {
  44. let buffer = ByteBuffer(staticString: "GET https://grpc.io/index.html HTTP/1.1\r\n")
  45. XCTAssertTrue(HTTPVersionParser.prefixedWithHTTP1RequestLine(buffer))
  46. }
  47. func testHTTP1RequestLineAndMore() {
  48. let buffer = ByteBuffer(staticString: "GET https://grpc.io/index.html HTTP/1.1\r\nMore")
  49. XCTAssertTrue(HTTPVersionParser.prefixedWithHTTP1RequestLine(buffer))
  50. }
  51. func testHTTP1RequestLineWithoutCRLF() {
  52. let buffer = ByteBuffer(staticString: "GET https://grpc.io/index.html HTTP/1.1")
  53. XCTAssertFalse(HTTPVersionParser.prefixedWithHTTP1RequestLine(buffer))
  54. }
  55. func testHTTP1NoBytes() {
  56. let empty = ByteBuffer()
  57. XCTAssertFalse(HTTPVersionParser.prefixedWithHTTP1RequestLine(empty))
  58. }
  59. func testHTTP1IncompleteRequestLine() {
  60. let buffer = ByteBuffer(staticString: "GET https://grpc.io/index.html")
  61. XCTAssertFalse(HTTPVersionParser.prefixedWithHTTP1RequestLine(buffer))
  62. }
  63. func testHTTP1MalformedVersion() {
  64. let buffer = ByteBuffer(staticString: "GET https://grpc.io/index.html ptth/1.1\r\n")
  65. XCTAssertFalse(HTTPVersionParser.prefixedWithHTTP1RequestLine(buffer))
  66. }
  67. }