HtmlFormParserTest.swift 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //
  2. // HtmlFormParserTest.swift
  3. // ShaarliOSTests
  4. //
  5. // Created by Marcus Rohrmoser on 09.06.19.
  6. // Copyright © 2019-2022 Marcus Rohrmoser mobile Software http://mro.name/me. All rights reserved.
  7. //
  8. // This program is free software: you can redistribute it and/or modify
  9. // it under the terms of the GNU General Public License as published by
  10. // the Free Software Foundation, either version 3 of the License, or
  11. // (at your option) any later version.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU General Public License
  19. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. //
  21. import XCTest
  22. class HtmlFormParserTest: XCTestCase {
  23. override func setUp() {
  24. // Put setup code here. This method is called before the invocation of each test method in the class.
  25. }
  26. override func tearDown() {
  27. // Put teardown code here. This method is called after the invocation of each test method in the class.
  28. }
  29. func testEncodings() {
  30. XCTAssertEqual("<Unicode (UTF-8)>", "<\(String.Encoding.utf8)>")
  31. XCTAssertEqual("<UTF8>", "<\(UTF8.self)>")
  32. // You can convert this string to a CFStringEncoding value by calling CFStringConvertIANACharSetNameToEncoding(_:).
  33. // You can subsequently convert that value to an NSStringEncoding value by calling CFStringConvertEncodingToNSStringEncoding(_:).
  34. }
  35. func testAtt2dict() {
  36. let ar0:[String?] = [nil]
  37. let di0 = atts2dict({ ar0[$0] })
  38. XCTAssertEqual([:], di0)
  39. let ar1 = ["name", "lf_tags", "value", "opensource software", "foo", nil, nil]
  40. let di1 = atts2dict({ ar1[$0] })
  41. XCTAssertEqual("lf_tags", di1["name"])
  42. XCTAssertEqual("opensource software", di1["value"])
  43. let ar2 = ["type", "checkbox", "name", "Kenntnisse_in", "checked", nil, "value", "HTML", nil]
  44. let di2 = atts2dict({ ar2[$0] })
  45. XCTAssertEqual("Kenntnisse_in", di2["name"])
  46. XCTAssertEqual("HTML", di2["value"])
  47. XCTAssertEqual("checked", di2["checked"])
  48. }
  49. func testLoadfile() {
  50. let d = dataWithContentsOfFixture(me:self, fileName: "login.0", extensio:"html")
  51. XCTAssertEqual(2509, d.count)
  52. }
  53. func testFindForms() {
  54. let raw = dataWithContentsOfFixture(me:self, fileName: "login.0", extensio:"html")
  55. let frms = findHtmlForms(raw, "utf-8")
  56. XCTAssertEqual(1, frms.count)
  57. let frm = frms["loginform"]!
  58. XCTAssertEqual(3, frm.count)
  59. XCTAssertNil(frm["login"])
  60. XCTAssertNil(frm["password"])
  61. XCTAssertEqual("Login", frm[""])
  62. XCTAssertEqual("20119241badf78a3dcfa55ae58eab429a5d24bad", frm["token"])
  63. XCTAssertNil(frm["longlastingsession"])
  64. XCTAssertEqual("http://links.mro.name/", frm["returnurl"])
  65. }
  66. func testLinkForm() {
  67. let raw = dataWithContentsOfFixture(me:self, fileName: "linkform.0", extensio:"html")
  68. let frms = findHtmlForms(raw, "utf-8")
  69. XCTAssertEqual(2, frms.count)
  70. let frm = frms["linkform"]!
  71. XCTAssertEqual(9, frm.count)
  72. XCTAssertEqual("06767bf39b3202f0c32d2dad3249742260c721b2", frm["token"], "token")
  73. XCTAssertEqual("1", frm["lf_id"], "lf_id")
  74. XCTAssertEqual("Apply Changes", frm["save_edit"], "save_edit")
  75. XCTAssertEqual("opensource software", frm["lf_tags"], "lf_tags")
  76. XCTAssertEqual("Welcome to Shaarli! This is your first public bookmark. To edit or delete me, you must first login.\n\nTo learn how to use Shaarli, consult the link \"Documentation\" at the bottom of this page.\n\nYou use the community supported version of the original Shaarli project, by Sebastien Sauvage.", frm["lf_description"], "lf_description")
  77. XCTAssertEqual("https://demo.shaarli.org/?", frm["returnurl"], "returnurl")
  78. XCTAssertEqual("20190701_010131", frm["lf_linkdate"], "lf_linkdate")
  79. XCTAssertEqual("https://shaarli.readthedocs.io", frm["lf_url"], "lf_url")
  80. XCTAssertEqual("The personal, minimalist, super-fast, database free, bookmarking service", frm["lf_title"], "lf_title")
  81. XCTAssertNil(frm["lf_private"], "value nil is treated like non-existing")
  82. }
  83. func testLinkForm1() {
  84. let raw = dataWithContentsOfFixture(me:self, fileName: "linkform.1", extensio:"html")
  85. let frms = findHtmlForms(raw, "utf-8")
  86. XCTAssertEqual(1, frms.count)
  87. let frm = frms["linkform"]!
  88. XCTAssertEqual(1, frm.count)
  89. XCTAssertEqual("opensource software", frm["lf_tags"], "lf_tags")
  90. }
  91. func testConfigForm0() {
  92. let raw = dataWithContentsOfFixture(me:self, fileName: "configform.0", extensio:"html")
  93. let frms = findHtmlForms(raw, "utf-8")
  94. XCTAssertEqual(1, frms.count)
  95. let frm = frms["configform"]!
  96. XCTAssertEqual(7, frm.count)
  97. XCTAssertEqual("Uhu", frm["title"], "title")
  98. XCTAssertEqual("0e6d94f5ccd8a2e0c0111706a26a3e9158db705f", frm["token"], "token")
  99. XCTAssertEqual("checked", frm["privateLinkByDefault"], "privateLinkByDefault")
  100. XCTAssertEqual("Europe", frm["continent"], "continent")
  101. XCTAssertEqual("Amsterdam", frm["city"], "city")
  102. let tz = TimeZone(identifier: "Europe/Amsterdam")
  103. // XCTAssertEqual("GMT+2", tz?.abbreviation())
  104. XCTAssertEqual("Europe/Amsterdam", tz?.identifier)
  105. }
  106. func testConfigForm1() {
  107. let raw = dataWithContentsOfFixture(me:self, fileName: "configform.1", extensio:"html")
  108. let frms = findHtmlForms(raw, "utf-8")
  109. XCTAssertEqual(1, frms.count)
  110. let frm = frms["configform"]!
  111. XCTAssertEqual(3, frm.count)
  112. XCTAssertEqual("🚀 Uhu", frm["title"], "title")
  113. XCTAssertEqual(nil, frm["continent"], "continent")
  114. XCTAssertEqual(nil, frm["city"], "city")
  115. }
  116. func testAlgebraicSumType() {
  117. // https://www.metaltoad.com/blog/sum-algebraic-data-types-haskell-and-swift
  118. // For simplicity sake dictionaries are good enough. Instead https://developer.apple.com/documentation/swift/keyvaluepairs
  119. enum FieldValue : Equatable {
  120. case text(String)
  121. case onoff(Bool)
  122. case option([String:Bool])
  123. }
  124. typealias FieldName = String
  125. typealias Frm = [FieldName:FieldValue]
  126. let f : Frm = [
  127. "foo":FieldValue.text("Foo"),
  128. "bar":FieldValue.onoff(false),
  129. "baz":FieldValue.option(["Europe":true, "Africa":false]),
  130. ]
  131. XCTAssertEqual(3, f.count, "count")
  132. XCTAssertEqual(FieldValue.text("Foo"), f["foo"], "text")
  133. XCTAssertEqual(FieldValue.onoff(false), f["bar"], "bool")
  134. switch f["foo"]! {
  135. case .text(let x): XCTAssertEqual("Foo", x)
  136. default: XCTFail()
  137. }
  138. switch f["bar"]! {
  139. case .onoff(let x): XCTAssertEqual(false, x)
  140. default: XCTFail()
  141. }
  142. switch f["baz"]! {
  143. case .option(let x):
  144. XCTAssertEqual(2, x.count)
  145. XCTAssertEqual(true, x["Europe"])
  146. XCTAssertEqual(false, x["Africa"])
  147. default: XCTFail()
  148. }
  149. }
  150. }