/* This software is in the public domain, furnished "as is", without technical support, and with no warranty, express or implied, as to its usefulness for any purpose. */ #ifndef LASTFM_TESTURLBUILDER_H #define LASTFM_TESTURLBUILDER_H #include #include #include #include #include static inline int getResponseCode( const QUrl& url ) { QNetworkAccessManager nam; QNetworkReply* reply = nam.head( QNetworkRequest(url) ); QEventLoop loop; loop.connect( reply, SIGNAL(finished()), SLOT(quit()) ); loop.exec(); int const code = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute ).toInt(); if (reply->error() != QNetworkReply::NoError) qDebug() << url << lastfm::qMetaEnumString( reply->error(), "NetworkError" ) << code; return code; } class TestUrlBuilder : public QObject { Q_OBJECT private Q_SLOTS: void encode() /** @author */ { QFETCH( QString, input ); QFETCH( QString, output ); QCOMPARE( lastfm::UrlBuilder::encode( input ), output.toLatin1() ); } void encode_data() /** @author */ { QTest::addColumn("input"); QTest::addColumn("output"); QTest::newRow( "ascii" ) << "Metallica" << "Metallica"; QTest::newRow( "ascii alphanumeric" ) << "Apollo 440" << "Apollo+440"; QTest::newRow( "ascii with symbols" ) << "Would?" << "Would%3F"; QTest::newRow( "ascii with last.fm-special symbols" ) << "Survivalism [Revision #1]" << "Survivalism+%5BRevision+%231%5D"; } void no404() /** @author */ { QFETCH( QString, artist ); QFETCH( QString, track ); QUrl url = lastfm::UrlBuilder( "music" ).slash( artist ).slash( "_" ).slash( track ).url(); QCOMPARE( getResponseCode( url ), 200 ); } void no404_data() /** @author */ { QTest::addColumn("artist"); QTest::addColumn("track"); #define NEW_ROW( x, y ) QTest::newRow( x " - " y ) << x << y; NEW_ROW( "Air", "Radio #1" ); NEW_ROW( "Pink Floyd", "Speak to Me / Breathe" ); NEW_ROW( "Radiohead", "2 + 2 = 5" ); NEW_ROW( "Above & Beyond", "World On Fire (Maor Levi Remix)" ); NEW_ROW( "Kreş", "Portret Nieświadomości" ); NEW_ROW( "자우림", "스물다섯, 스물하나" ); #undef NEW_ROW } void test404() /** @author */ { QCOMPARE( getResponseCode( QUrl("https://www.last.fm/404") ), 404 ); } }; #endif