HtmlFormParserTest.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // HtmlFormParserTest.swift
  3. // ShaarliOSTests
  4. //
  5. // Created by Marcus Rohrmoser on 09.06.19.
  6. // Copyright © 2019-2020 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 testAtt2dict() {
  30. var ar0:[String?] = [nil]
  31. let di0 = atts2dict({ ar0[$0] })
  32. XCTAssertEqual([:], di0)
  33. var ar1 = ["name", "lf_tags", "value", "opensource software", "foo", nil, nil]
  34. let di1 = atts2dict({ ar1[$0] })
  35. XCTAssertEqual("lf_tags", di1["name"])
  36. XCTAssertEqual("opensource software", di1["value"])
  37. var ar2 = ["type", "checkbox", "name", "Kenntnisse_in", "checked", nil, "value", "HTML", nil]
  38. let di2 = atts2dict({ ar2[$0] })
  39. XCTAssertEqual("Kenntnisse_in", di2["name"])
  40. XCTAssertEqual("HTML", di2["value"])
  41. XCTAssertEqual("checked", di2["checked"])
  42. }
  43. func testLoadfile() {
  44. let d = dataWithContentsOfFixture(me:self, fileName: "login.0", extensio:"html")
  45. XCTAssertEqual(2509, d.count)
  46. }
  47. func testFindForms() {
  48. let raw = dataWithContentsOfFixture(me:self, fileName: "login.0", extensio:"html")
  49. let frms = findHtmlForms(raw, "utf-8")
  50. XCTAssertEqual(1, frms.count)
  51. let frm = frms["loginform"]!
  52. XCTAssertEqual(3, frm.count)
  53. XCTAssertNil(frm["login"])
  54. XCTAssertNil(frm["password"])
  55. XCTAssertEqual("Login", frm[""])
  56. XCTAssertEqual("20119241badf78a3dcfa55ae58eab429a5d24bad", frm["token"])
  57. XCTAssertNil(frm["longlastingsession"])
  58. XCTAssertEqual("http://links.mro.name/", frm["returnurl"])
  59. }
  60. func testLinkForm() {
  61. let raw = dataWithContentsOfFixture(me:self, fileName: "linkform.0", extensio:"html")
  62. let frms = findHtmlForms(raw, "utf-8")
  63. XCTAssertEqual(2, frms.count)
  64. let frm = frms["linkform"]!
  65. XCTAssertEqual(9, frm.count)
  66. XCTAssertEqual("06767bf39b3202f0c32d2dad3249742260c721b2", frm["token"], "token")
  67. XCTAssertEqual("1", frm["lf_id"], "lf_id")
  68. XCTAssertEqual("Apply Changes", frm["save_edit"], "save_edit")
  69. XCTAssertEqual("opensource software", frm["lf_tags"], "lf_tags")
  70. 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")
  71. XCTAssertEqual("https://demo.shaarli.org/?", frm["returnurl"], "returnurl")
  72. XCTAssertEqual("20190701_010131", frm["lf_linkdate"], "lf_linkdate")
  73. XCTAssertEqual("https://shaarli.readthedocs.io", frm["lf_url"], "lf_url")
  74. XCTAssertEqual("The personal, minimalist, super-fast, database free, bookmarking service", frm["lf_title"], "lf_title")
  75. XCTAssertNil(frm["lf_private"], "value nil is treated like non-existing")
  76. }
  77. func testLinkForm1() {
  78. let raw = dataWithContentsOfFixture(me:self, fileName: "linkform.1", extensio:"html")
  79. let frms = findHtmlForms(raw, "utf-8")
  80. XCTAssertEqual(1, frms.count)
  81. let frm = frms["linkform"]!
  82. XCTAssertEqual(1, frm.count)
  83. XCTAssertEqual("opensource software", frm["lf_tags"], "lf_tags")
  84. }
  85. func testCinfigForm0() {
  86. let raw = dataWithContentsOfFixture(me:self, fileName: "configform.0", extensio:"html")
  87. let frms = findHtmlForms(raw, "utf-8")
  88. XCTAssertEqual(1, frms.count)
  89. let frm = frms["configform"]!
  90. XCTAssertEqual(3, frm.count)
  91. XCTAssertEqual("🚀 Uhu", frm["title"], "title")
  92. }
  93. }