Debian Codegraph @

tests/TestUrlBuilder.h

raw symbols SCIP
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 #ifndef LASTFM_TESTURLBUILDER_H
8 #define LASTFM_TESTURLBUILDER_H
9
10 #include <UrlBuilder.h>
11 #include <global.h>
12
13 #include <QtTest>
14 #include <QtNetwork>
15 #include <QEventLoop>
16
17
18 static inline int getResponseCode( const QUrl& url )
19 {
20 QNetworkAccessManager nam;
21 QNetworkReply* reply = nam.head( QNetworkRequest(url) );
22
23 QEventLoop loop;
24 loop.connect( reply, SIGNAL(finished()), SLOT(quit()) );
25 loop.exec();
26
27 int const code = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute ).toInt();
28
29 if (reply->error() != QNetworkReply::NoError)
30 qDebug() << url << lastfm::qMetaEnumString<QNetworkReply>( reply->error(), "NetworkError" ) << code;
31
32 return code;
33 }
34
35
36 class TestUrlBuilder : public QObject
37 {
38 Q_OBJECT
39
40 private Q_SLOTS:
41 void encode() /** @author <[email protected]> */
42 {
43 QFETCH( QString, input );
44 QFETCH( QString, output );
45 QCOMPARE( lastfm::UrlBuilder::encode( input ), output.toLatin1() );
46 }
47
48 void encode_data() /** @author <[email protected]> */
49 {
50 QTest::addColumn<QString>("input");
51 QTest::addColumn<QString>("output");
52
53 QTest::newRow( "ascii" ) << "Metallica" << "Metallica";
54 QTest::newRow( "ascii alphanumeric" ) << "Apollo 440" << "Apollo+440";
55 QTest::newRow( "ascii with symbols" ) << "Would?" << "Would%3F";
56 QTest::newRow( "ascii with last.fm-special symbols" ) << "Survivalism [Revision #1]" << "Survivalism+%5BRevision+%231%5D";
57 }
58
59 void no404() /** @author <[email protected]> */
60 {
61 QFETCH( QString, artist );
62 QFETCH( QString, track );
63
64 QUrl url = lastfm::UrlBuilder( "music" ).slash( artist ).slash( "_" ).slash( track ).url();
65
66 QCOMPARE( getResponseCode( url ), 200 );
67 }
68
69 void no404_data() /** @author <[email protected]> */
70 {
71 QTest::addColumn<QString>("artist");
72 QTest::addColumn<QString>("track");
73
74 #define NEW_ROW( x, y ) QTest::newRow( x " - " y ) << x << y;
75 NEW_ROW( "Air", "Radio #1" );
76 NEW_ROW( "Pink Floyd", "Speak to Me / Breathe" );
77 NEW_ROW( "Radiohead", "2 + 2 = 5" );
78 NEW_ROW( "Above & Beyond", "World On Fire (Maor Levi Remix)" );
79 NEW_ROW( "Kreş", "Portret Nieświadomości" );
80 NEW_ROW( "자우림", "스물다섯, 스물하나" );
81 #undef NEW_ROW
82 }
83
84 void test404() /** @author <[email protected]> */
85 {
86 QCOMPARE( getResponseCode( QUrl("https://www.last.fm/404") ), 404 );
87 }
88 };
89
90 #endif