UserAgentTests.swift 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //
  2. // UserAgentTests.swift
  3. // Nextcloud
  4. //
  5. // Created by Henrik Storch on 03.05.22.
  6. // Copyright © 2022 Henrik Storch. All rights reserved.
  7. //
  8. // Author Henrik Storch <henrik.storch@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. @testable import Nextcloud
  24. import XCTest
  25. class UserAgentTests: XCTestCase {
  26. // https://github.com/nextcloud/server/blob/fc826e98115b510313ddacbf6fef4ce8d041e373/lib/public/IRequest.php#L83
  27. let ncServerUARegex = "^Mozilla\\/5\\.0 \\(iOS\\) (ownCloud|Nextcloud)\\-iOS.*$"
  28. // https://github.com/ProseMirror/prosemirror-view/blob/427d278aaaacde422ed1f2b8c84bb53337162775/src/browser.js#L18-L22
  29. let proseMirrorWebKitUARegex = "\\bAppleWebKit\\/(\\d+)"
  30. let proseMirroriOSUARegex = "Mobile\\/\\w+"
  31. func testDefaultUserAgent() throws {
  32. let userAgent: String = CCUtility.getUserAgent()
  33. let match = try matches(for: ncServerUARegex, in: userAgent).first
  34. XCTAssertNotNil(match)
  35. }
  36. func testTextUserAgent() throws {
  37. let userAgent: String = NCUtility.shared.getCustomUserAgentNCText()
  38. let match = try matches(for: ncServerUARegex, in: userAgent).first
  39. XCTAssertNotNil(match)
  40. let iOSMatch = try matches(for: proseMirroriOSUARegex, in: userAgent).first
  41. XCTAssertNotNil(iOSMatch)
  42. // https://github.com/ProseMirror/prosemirror-view/blob/8f246f320801f8e3cac92c97f71ac91e3e327f2f/src/input.js#L521-L522
  43. let webKitMatch = try matches(for: proseMirrorWebKitUARegex, in: userAgent).first
  44. XCTAssertNotNil(webKitMatch)
  45. XCTAssertEqual(webKitMatch!.numberOfRanges, 2)
  46. let versionRange = webKitMatch!.range(at: 1)
  47. let versionString = userAgent[Range(versionRange, in: userAgent)!]
  48. let webkitVersion = Int(versionString) ?? 0
  49. XCTAssertGreaterThanOrEqual(webkitVersion, 604)
  50. }
  51. func matches(for regex: String, in text: String) throws -> [NSTextCheckingResult] {
  52. let range = NSRange(location: 0, length: text.utf16.count)
  53. let regex = try NSRegularExpression(pattern: regex)
  54. return regex.matches(in: text, range: range)
  55. }
  56. }