PageRenderTime 103ms CodeModel.GetById 51ms app.highlight 6ms RepoModel.GetById 24ms app.codeStats 0ms

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