CallPathTests.swift 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 XCTest
  18. class CallPathTests: GRPCTestCase {
  19. func testSplitPathNormal() {
  20. let path = "/server/method"
  21. let parsedPath = CallPath(requestURI: path)
  22. let splitPath = path.split(separator: "/")
  23. XCTAssertEqual(splitPath[0], String.SubSequence(parsedPath!.service))
  24. XCTAssertEqual(splitPath[1], String.SubSequence(parsedPath!.method))
  25. }
  26. func testSplitPathTooShort() {
  27. let path = "/badPath"
  28. let parsedPath = CallPath(requestURI: path)
  29. XCTAssertNil(parsedPath)
  30. }
  31. func testSplitPathTooLong() {
  32. let path = "/server/method/discard"
  33. let parsedPath = CallPath(requestURI: path)
  34. let splitPath = path.split(separator: "/")
  35. XCTAssertEqual(splitPath[0], String.SubSequence(parsedPath!.service))
  36. XCTAssertEqual(splitPath[1], String.SubSequence(parsedPath!.method))
  37. }
  38. func testTrimPrefixEmpty() {
  39. var toSplit = "".utf8[...]
  40. let head = toSplit.trimPrefix(to: UInt8(ascii: "/"))
  41. XCTAssertNil(head)
  42. XCTAssertEqual(toSplit.count, 0)
  43. }
  44. func testTrimPrefixAll() {
  45. let source = "words"
  46. var toSplit = source.utf8[...]
  47. let head = toSplit.trimPrefix(to: UInt8(ascii: "/"))
  48. XCTAssertEqual(head?.count, source.utf8.count)
  49. XCTAssertEqual(toSplit.count, 0)
  50. }
  51. func testTrimPrefixAndRest() {
  52. let source = "words/moreWords"
  53. var toSplit = source.utf8[...]
  54. let head = toSplit.trimPrefix(to: UInt8(ascii: "/"))
  55. XCTAssertEqual(head?.count, "words".utf8.count)
  56. XCTAssertEqual(toSplit.count, "moreWords".utf8.count)
  57. }
  58. }