/thirdparty/liblastfm2/tests/TestUrlBuilder.h

http://github.com/tomahawk-player/tomahawk · C Header · 80 lines · 59 code · 16 blank · 5 comment · 2 complexity · 3d1a8ca41a8b55e837ec66ed745e4535 MD5 · raw file

  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. #include <QtTest>
  7. #include <QtNetwork>
  8. #include <QEventLoop>
  9. #include <lastfm/UrlBuilder>
  10. #include <lastfm/global.h>
  11. static inline int getResponseCode( const QUrl& url )
  12. {
  13. QNetworkAccessManager nam;
  14. QNetworkReply* reply = nam.head( QNetworkRequest(url) );
  15. QEventLoop loop;
  16. loop.connect( reply, SIGNAL(finished()), SLOT(quit()) );
  17. loop.exec();
  18. int const code = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute ).toInt();
  19. if (reply->error() != QNetworkReply::NoError)
  20. qDebug() << url << lastfm::qMetaEnumString<QNetworkReply>( reply->error(), "NetworkError" ) << code;
  21. return code;
  22. }
  23. class TestUrlBuilder : public QObject
  24. {
  25. Q_OBJECT
  26. private slots:
  27. void encode() /** @author <jono@last.fm> */
  28. {
  29. QFETCH( QString, input );
  30. QFETCH( QString, output );
  31. QCOMPARE( lastfm::UrlBuilder::encode( input ), output.toAscii() );
  32. }
  33. void encode_data() /** @author <jono@last.fm> */
  34. {
  35. QTest::addColumn<QString>("input");
  36. QTest::addColumn<QString>("output");
  37. QTest::newRow( "ascii" ) << "Metallica" << "Metallica";
  38. QTest::newRow( "ascii alphanumeric" ) << "Apollo 440" << "Apollo+440";
  39. QTest::newRow( "ascii with symbols" ) << "some track [original version]" << "some+track+%5Boriginal+version%5D";
  40. QTest::newRow( "ascii with last.fm-special symbols" ) << "Survivalism [Revision #1]" << "Survivalism%2B%255BRevision%2B%25231%255D";
  41. }
  42. void no404() /** @author <max@last.fm> */
  43. {
  44. QFETCH( QString, artist );
  45. QFETCH( QString, track );
  46. QUrl url = lastfm::UrlBuilder( "music" ).slash( artist ).slash( "_" ).slash( track ).url();
  47. QCOMPARE( getResponseCode( url ), 200 );
  48. }
  49. void no404_data() /** @author <max@last.fm> */
  50. {
  51. QTest::addColumn<QString>("artist");
  52. QTest::addColumn<QString>("track");
  53. #define NEW_ROW( x, y ) QTest::newRow( x " - " y ) << x << y;
  54. NEW_ROW( "Air", "Radio #1" );
  55. NEW_ROW( "Pink Floyd", "Speak to Me / Breathe" );
  56. NEW_ROW( "Radiohead", "2 + 2 = 5" );
  57. NEW_ROW( "Above & Beyond", "World On Fire (Maor Levi Remix)" );
  58. #undef NEW_ROW
  59. }
  60. void test404() /** @author <max@last.fm> */
  61. {
  62. QCOMPARE( getResponseCode( QUrl("http://www.last.fm/404") ), 404 );
  63. }
  64. };