testconcaturl.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * This software is in the public domain, furnished "as is", without technical
  3. * support, and with no warranty, express or implied, as to its usefulness for
  4. * any purpose.
  5. *
  6. */
  7. #include <QtTest>
  8. #include <QUrl>
  9. #include <QString>
  10. #include "account.h"
  11. using namespace OCC;
  12. using QueryItems = QList<QPair<QString, QString>>;
  13. Q_DECLARE_METATYPE(QueryItems)
  14. static QueryItems make()
  15. {
  16. return QueryItems();
  17. }
  18. static QueryItems make(QString key, QString value)
  19. {
  20. QueryItems q;
  21. q.append(qMakePair(key, value));
  22. return q;
  23. }
  24. static QueryItems make(QString key1, QString value1,
  25. QString key2, QString value2)
  26. {
  27. QueryItems q;
  28. q.append(qMakePair(key1, value1));
  29. q.append(qMakePair(key2, value2));
  30. return q;
  31. }
  32. class TestConcatUrl: public QObject
  33. {
  34. Q_OBJECT
  35. private slots:
  36. void testFolder()
  37. {
  38. QFETCH(QString, base);
  39. QFETCH(QString, concat);
  40. QFETCH(QueryItems, query);
  41. QFETCH(QString, expected);
  42. QUrl baseUrl("http://example.com" + base);
  43. QUrlQuery urlQuery;
  44. urlQuery.setQueryItems(query);
  45. QUrl resultUrl = Utility::concatUrlPath(baseUrl, concat, urlQuery);
  46. QString result = QString::fromUtf8(resultUrl.toEncoded());
  47. QString expectedFull = "http://example.com" + expected;
  48. QCOMPARE(result, expectedFull);
  49. }
  50. void testFolder_data()
  51. {
  52. QTest::addColumn<QString>("base");
  53. QTest::addColumn<QString>("concat");
  54. QTest::addColumn<QueryItems>("query");
  55. QTest::addColumn<QString>("expected");
  56. // Tests about slashes
  57. QTest::newRow("noslash1") << "/baa" << "foo" << make() << "/baa/foo";
  58. QTest::newRow("noslash2") << "" << "foo" << make() << "/foo";
  59. QTest::newRow("noslash3") << "/foo" << "" << make() << "/foo";
  60. QTest::newRow("noslash4") << "" << "" << make() << "";
  61. QTest::newRow("oneslash1") << "/bar/" << "foo" << make() << "/bar/foo";
  62. QTest::newRow("oneslash2") << "/" << "foo" << make() << "/foo";
  63. QTest::newRow("oneslash3") << "/foo" << "/" << make() << "/foo/";
  64. QTest::newRow("oneslash4") << "" << "/" << make() << "/";
  65. QTest::newRow("twoslash1") << "/bar/" << "/foo" << make() << "/bar/foo";
  66. QTest::newRow("twoslash2") << "/" << "/foo" << make() << "/foo";
  67. QTest::newRow("twoslash3") << "/foo/" << "/" << make() << "/foo/";
  68. QTest::newRow("twoslash4") << "/" << "/" << make() << "/";
  69. // Tests about path encoding
  70. QTest::newRow("encodepath")
  71. << "/a f/b"
  72. << "/a f/c"
  73. << make()
  74. << "/a%20f/b/a%20f/c";
  75. // Tests about query args
  76. QTest::newRow("query1")
  77. << "/baa"
  78. << "/foo"
  79. << make("a=a", "b=b",
  80. "c", "d")
  81. << "/baa/foo?a%3Da=b%3Db&c=d";
  82. QTest::newRow("query2")
  83. << ""
  84. << ""
  85. << make("foo", "bar")
  86. << "?foo=bar";
  87. }
  88. };
  89. QTEST_APPLESS_MAIN(TestConcatUrl)
  90. #include "testconcaturl.moc"