summaryrefslogtreecommitdiffstats
path: root/rbutil/rbutilqt/test/test-httpget.cpp
blob: a062203e70ef4fcac58aabdf897a0d74713f990f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 *
 * Copyright (C) 2013 Dominik Riebeling
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include <QtTest/QtTest>
#include <QtCore/QObject>
#include "httpget.h"

#define TEST_USER_AGENT "TestAgent/2.3"
#define TEST_HTTP_TIMEOUT 1000
#define TEST_BINARY_BLOB "\x01\x10\x20\x30\x40\x50\x60\x70" \
                         "\x80\x90\xff\xee\xdd\xcc\xbb\xaa"

 // HttpDaemon is the the class that implements the simple HTTP server.
 class HttpDaemon : public QTcpServer
{
    Q_OBJECT
    public:
        HttpDaemon(quint16 port, QObject* parent = 0) : QTcpServer(parent)
        {
            listen(QHostAddress::Any, port);
        }

#if QT_VERSION < 0x050000
        void incomingConnection(int socket)
#else
        // Qt 5 uses a different prototype for this function!
        void incomingConnection(qintptr socket)
#endif
        {
            // When a new client connects, the server constructs a QTcpSocket and all
            // communication with the client is done over this QTcpSocket. QTcpSocket
            // works asynchronously, this means that all the communication is done
            // in the two slots readClient() and discardClient().
            QTcpSocket* s = new QTcpSocket(this);
            connect(s, SIGNAL(readyRead()), this, SLOT(readClient()));
            connect(s, SIGNAL(disconnected()), this, SLOT(discardClient()));
            s->setSocketDescriptor(socket);
        }
        QList<QString> lastRequestData(void)
        {
            return m_lastRequestData;
        }
        void setResponsesToSend(QList<QByteArray> response)
        {
            m_requestNumber = 0;
            m_responsesToSend = response;
        }
        void reset(void)
        {
            m_requestNumber = 0;
            m_lastRequestData.clear();
            QString now =
                QDateTime::currentDateTime().toString("ddd, d MMM yyyy hh:mm:ss");
            m_defaultResponse = QByteArray(
                "HTTP/1.1 404 Not Found\r\n"
                "Date: " + now.toLatin1() + "\r\n"
                "Last-Modified: " + now.toLatin1() + "\r\n"
                "Connection: close\r\n"
                "\r\n");
        }

    private slots:
        void readClient()
        {
            // This slot is called when the client sent data to the server.
            QTcpSocket* socket = (QTcpSocket*)sender();
            // read whole request
            QString request;
            while(socket->canReadLine()) {
                QString line = socket->readLine();
                request.append(line);
                if(request.endsWith("\r\n\r\n")) {
                    m_lastRequestData.append(request);

                        if(m_requestNumber < m_responsesToSend.size())
                            socket->write(m_responsesToSend.at(m_requestNumber));
                        else
                            socket->write(m_defaultResponse);
                    socket->close();
                    m_requestNumber++;
                }
                if (socket->state() == QTcpSocket::UnconnectedState)
                    delete socket;
            }
        }
        void discardClient()
        {
            QTcpSocket* socket = (QTcpSocket*)sender();
            socket->deleteLater();
        }

    private:
        int m_requestNumber;
        QList<QByteArray> m_responsesToSend;
        QList<QString> m_lastRequestData;
        QByteArray m_defaultResponse;
};


class TestHttpGet : public QObject
{
    Q_OBJECT
    private slots:
        void testFileUrlRequest(void);
        void testCachedRequest(void);
        void testUncachedRepeatedRequest(void);
        void testUncachedMovedRequest(void);
        void testUserAgent(void);
        void testResponseCode(void);
        void testContentToBuffer(void);
        void testContentToFile(void);
        void testNoServer(void);
        void testServerTimestamp(void);
        void testMovedQuery(void);
        void init(void);
        void cleanup(void);

    public slots:
        void waitTimeout(void) { m_waitTimeoutOccured = true; }
        QDir temporaryFolder(void)
        {
            // Qt unfortunately doesn't support creating temporary folders so
            // we need to do that ourselves.
            QString tempdir;
            for(int i = 0; i < 100000; i++) {
                tempdir = QDir::tempPath() + QString("/qttest-temp-%1").arg(i);
                if(!QFileInfo(tempdir).exists()) break;
            }
            QDir().mkpath(tempdir);
            return QDir(tempdir);
        }
        void rmTree(QString folder)
        {
            // no function in Qt to recursively delete a folder :(
            QDir dir(folder);
            Q_FOREACH(QFileInfo info, dir.entryInfoList(QDir::NoDotAndDotDot
                        | QDir::System | QDir::Hidden | QDir::AllDirs
                        | QDir::Files, QDir::DirsFirst)) {
                if(info.isDir()) rmTree(info.absoluteFilePath());
                else QFile::remove(info.absoluteFilePath());
            }
            dir.rmdir(folder);
        }
    private:
        HttpDaemon *m_daemon;
        bool m_waitTimeoutOccured;
        QString m_now;
        QDir m_cachedir;
        HttpGet *m_getter;
        QSignalSpy *m_doneSpy;
};


void TestHttpGet::init(void)
{
    m_now = QDateTime::currentDateTime().toString("ddd, d MMM yyyy hh:mm:ss");
    m_daemon = new HttpDaemon(8080, this);
    m_daemon->reset();
    m_cachedir = temporaryFolder();
    m_getter = new HttpGet(this);
    m_doneSpy = new QSignalSpy(m_getter, SIGNAL(done(bool)));
    m_waitTimeoutOccured = false;
}

void TestHttpGet::cleanup(void)
{
    rmTree(m_cachedir.absolutePath());
    if(m_getter) {
        m_getter->abort(); delete m_getter;
    }
    if(m_daemon) delete m_daemon;
    if(m_doneSpy) delete m_doneSpy;
}

void TestHttpGet::testFileUrlRequest(void)
{
    QTimer::singleShot(TEST_HTTP_TIMEOUT, this, SLOT(waitTimeout(void)));

    QString teststring = "The quick brown fox jumps over the lazy dog.";
    QTemporaryFile datafile;
    datafile.open();
    datafile.write(teststring.toLatin1());
    m_getter->getFile("file://" + datafile.fileName());
    datafile.close();
    while(m_doneSpy->count() == 0 && m_waitTimeoutOccured == false)
        QCoreApplication::processEvents();

    QCOMPARE(m_doneSpy->count(), 1);
    QCOMPARE(m_waitTimeoutOccured, false);
    QCOMPARE(m_daemon->lastRequestData().size(), 0);
    QCOMPARE(m_getter->readAll(), teststring.toLatin1());
    QCOMPARE(m_getter->httpResponse(), 200);
}


/* On uncached requests, HttpGet is supposed to sent a GET request only.
 */
void TestHttpGet::testUncachedRepeatedRequest(void)
{
    QList<QByteArray> responses;
    responses << QByteArray(
        "HTTP/1.1 200 OK\r\n"
        "Date: " + m_now.toLatin1() + "\r\n"
        "Last-Modified: " + m_now.toLatin1() + "\r\n"
        "\r\n\r\n");
    responses << QByteArray(
        "HTTP/1.1 200 OK\r\n"
        "Last-Modified: " + m_now.toLatin1() + "\r\n"
        "Date: " + m_now.toLatin1() + "\r\n"
        "\r\n"
        "<html></html>\r\n\r\n");
    m_daemon->setResponsesToSend(responses);

    QTimer::singleShot(TEST_HTTP_TIMEOUT, this, SLOT(waitTimeout(void)));

    m_getter->getFile(QUrl("http://localhost:8080/test1.txt"));
    while(m_doneSpy->count() == 0 && m_waitTimeoutOccured == false)
        QCoreApplication::processEvents();

    QCOMPARE(m_doneSpy->count(), 1);
    QCOMPARE(m_waitTimeoutOccured, false);
    QCOMPARE(m_daemon->lastRequestData().size(), 1);
    QCOMPARE(m_daemon->lastRequestData().at(0).startsWith("GET"), true);

    // request second time
    m_getter->getFile(QUrl("http://localhost:8080/test1.txt"));
    while(m_doneSpy->count() < 2 && m_waitTimeoutOccured == false)
        QCoreApplication::processEvents();
    QCOMPARE(m_doneSpy->count(), 2);
    QCOMPARE(m_waitTimeoutOccured, false);
    QCOMPARE(m_daemon->lastRequestData().size(), 2);
    QCOMPARE(m_daemon->lastRequestData().at(1).startsWith("GET"), true);
    QCOMPARE(m_getter->httpResponse(), 200);
}

/* With enabled cache HttpGet is supposed to check the server file using a HEAD
 * request first, then request the file using GET if the server file is newer
 * than the cached one (or the file does not exist in the cache)
 */
void TestHttpGet::testCachedRequest(void)
{
    QList<QByteArray> responses;
    responses << QByteArray(
        "HTTP/1.1 302 Found\r\n"
        "Location: http://localhost:8080/test2.txt\r\n"
        "Date: " + m_now.toLatin1() + "\r\n"
        "Last-Modified: " + m_now.toLatin1() + "\r\n"
        "\r\n");
    responses << QByteArray(
        "HTTP/1.1 200 OK\r\n"
        "Last-Modified: " + m_now.toLatin1() + "\r\n"
        "Date: " + m_now.toLatin1() + "\r\n"
        "\r\n"
        "<html></html>\r\n\r\n");
    responses << QByteArray(
        "HTTP/1.1 200 OK\r\n"
        "Last-Modified: 1 Jan 2000 00:00:00\r\n"
        "Date: " + m_now.toLatin1() + "\r\n"
        "\r\n");
    m_daemon->setResponsesToSend(responses);

    QTimer::singleShot(TEST_HTTP_TIMEOUT, this, SLOT(waitTimeout(void)));

    m_getter->setCache(m_cachedir);
    m_getter->getFile(QUrl("http://localhost:8080/test1.txt"));
    while(m_doneSpy->count() == 0 && m_waitTimeoutOccured == false)
        QCoreApplication::processEvents();

    QList<QString> requests = m_daemon->lastRequestData();
    QCOMPARE(m_doneSpy->count(), 1);
    QCOMPARE(m_doneSpy->at(0).at(0).toBool(), false);
    QCOMPARE(m_waitTimeoutOccured, false);
    QCOMPARE(requests.size(), 2);
    QCOMPARE(requests.at(0).startsWith("GET"), true);
    QCOMPARE(requests.at(1).startsWith("GET"), true);
    QCOMPARE(m_getter->httpResponse(), 200);

    // request real file, this time the response should come from cache.
    m_getter->getFile(QUrl("http://localhost:8080/test2.txt"));
    while(m_doneSpy->count() < 2 && m_waitTimeoutOccured == false)
        QCoreApplication::processEvents();
    QCOMPARE(m_doneSpy->count(), 2);  // 2 requests, 2 times done()
    QCOMPARE(m_doneSpy->at(1).at(0).toBool(), false);
    QCOMPARE(m_waitTimeoutOccured, false);
    QCOMPARE(m_daemon->lastRequestData().size(), 3);
    // redirect will not cache as the redirection target file.
    QCOMPARE(m_daemon->lastRequestData().at(2).startsWith("GET"), true);
    QCOMPARE(m_getter->httpResponse(), 200);
}

/* When a custom user agent is set all requests are supposed to contain it.
 * Enable cache to make HttpGet performs a HEAD request. Answer with 302, so
 * HttpGet follows and sends another HEAD request before finally doing a GET.
 */
void TestHttpGet::testUserAgent(void)
{
    QList<QByteArray> responses;
    responses << QByteArray(
        "HTTP/1.1 200 OK\r\n"
        "Date: " + m_now.toLatin1() + "\r\n"
        "Last-Modified: " + m_now.toLatin1() + "\r\n"
        "\r\n\r\n");
    responses << QByteArray(
        "HTTP/1.1 200 OK\r\n"
        "Last-Modified: " + m_now.toLatin1() + "\r\n"
        "Date: " + m_now.toLatin1() + "\r\n"
        "\r\n"
        "<html></html>\r\n\r\n");
    m_daemon->setResponsesToSend(responses);

    QTimer::singleShot(TEST_HTTP_TIMEOUT, this, SLOT(waitTimeout(void)));

    m_getter->setGlobalUserAgent(TEST_USER_AGENT);
    m_getter->setCache(m_cachedir);
    m_getter->getFile(QUrl("http://localhost:8080/test1.txt"));
    while(m_doneSpy->count() == 0 && m_waitTimeoutOccured == false)
        QCoreApplication::processEvents();

    QList<QString> requests = m_daemon->lastRequestData();
    QCOMPARE(m_doneSpy->count(), 1);
    QCOMPARE(m_waitTimeoutOccured, false);
    QCOMPARE(requests.size(), 1);
    QCOMPARE(requests.at(0).startsWith("GET"), true);

    for(int i = 0; i < requests.size(); ++i) {
        QRegExp rx("User-Agent:[\t ]+([a-zA-Z0-9\\./]+)");
        bool userAgentFound = rx.indexIn(requests.at(i)) > 0 ? true : false;
        QCOMPARE(userAgentFound, true);
        QString userAgentString = rx.cap(1);
        QCOMPARE(userAgentString, QString(TEST_USER_AGENT));
    }
}

void TestHttpGet::testUncachedMovedRequest(void)
{
    QList<QByteArray> responses;
    responses << QByteArray(
        "HTTP/1.1 302 Found\r\n"
        "Location: http://localhost:8080/test2.txt\r\n"
        "Date: " + m_now.toLatin1() + "\r\n"
        "Last-Modified: " + m_now.toLatin1() + "\r\n"
        "\r\n");
    responses << QByteArray(
        "HTTP/1.1 200 OK\r\n"
        "Last-Modified: " + m_now.toLatin1() + "\r\n"
        "Date: " + m_now.toLatin1() + "\r\n"
        "\r\n"
        "<html></html>\r\n\r\n");
    m_daemon->setResponsesToSend(responses);

    QTimer::singleShot(TEST_HTTP_TIMEOUT, this, SLOT(waitTimeout(void)));

    m_getter->getFile(QUrl("http://localhost:8080/test1.php?var=1&b=foo"));
    while(m_doneSpy->count() == 0 && m_waitTimeoutOccured == false)
        QCoreApplication::processEvents();

    QCOMPARE(m_doneSpy->count(), 1);
    QCOMPARE(m_waitTimeoutOccured, false);
    QCOMPARE(m_daemon->lastRequestData().size(), 2);
    QCOMPARE(m_daemon->lastRequestData().at(0).startsWith("GET"), true);
    QCOMPARE(m_daemon->lastRequestData().at(1).startsWith("GET"), true);
}

void TestHttpGet::testResponseCode(void)
{
    QTimer::singleShot(TEST_HTTP_TIMEOUT, this, SLOT(waitTimeout(void)));

    m_getter->getFile(QUrl("http://localhost:8080/test1.txt"));
    while(m_doneSpy->count() == 0 && m_waitTimeoutOccured == false)
        QCoreApplication::processEvents();

    QCOMPARE(m_doneSpy->count(), 1);
    QCOMPARE(m_doneSpy->at(0).at(0).toBool(), true);
    QCOMPARE(m_waitTimeoutOccured, false);
    QCOMPARE(m_daemon->lastRequestData().size(), 1);
    QCOMPARE(m_daemon->lastRequestData().at(0).startsWith("GET"), true);
    QCOMPARE(m_getter->httpResponse(), 404);
}

void TestHttpGet::testContentToBuffer(void)
{
    QList<QByteArray> responses;
    responses << QByteArray(
        "HTTP/1.1 200 OK\r\n"
        "Last-Modified: " + m_now.toLatin1() + "\r\n"
        "Date: " + m_now.toLatin1() + "\r\n"
        "\r\n"
        TEST_BINARY_BLOB);
    m_daemon->setResponsesToSend(responses);

    QTimer::singleShot(TEST_HTTP_TIMEOUT, this, SLOT(waitTimeout(void)));

    m_getter->getFile(QUrl("http://localhost:8080/test1.txt"));
    while(m_doneSpy->count() == 0 && m_waitTimeoutOccured == false)
        QCoreApplication::processEvents();

    QCOMPARE(m_doneSpy->count(), 1);
    QCOMPARE(m_waitTimeoutOccured, false);
    QCOMPARE(m_getter->readAll(), QByteArray(TEST_BINARY_BLOB));
    // sizeof(TEST_BINARY_BLOB) will include an additional terminating NULL.
    QCOMPARE((unsigned long)m_getter->readAll().size(), sizeof(TEST_BINARY_BLOB) - 1);
}

void TestHttpGet::testContentToFile(void)
{
    QTemporaryFile tf(this);
    QList<QByteArray> responses;
    responses << QByteArray(
        "HTTP/1.1 200 OK\r\n"
        "Last-Modified: " + m_now.toLatin1() + "\r\n"
        "Date: " + m_now.toLatin1() + "\r\n"
        "\r\n"
        TEST_BINARY_BLOB);
    m_daemon->setResponsesToSend(responses);

    QTimer::singleShot(TEST_HTTP_TIMEOUT, this, SLOT(waitTimeout(void)));

    m_getter->setFile(&tf);
    m_getter->getFile(QUrl("http://localhost:8080/test1.txt"));
    while(m_doneSpy->count() == 0 && m_waitTimeoutOccured == false)
        QCoreApplication::processEvents();

    QCOMPARE(m_doneSpy->count(), 1);
    QCOMPARE(m_waitTimeoutOccured, false);

    tf.open();
    QByteArray data = tf.readAll();
    QCOMPARE(data, QByteArray(TEST_BINARY_BLOB));
    QCOMPARE((unsigned long)data.size(), sizeof(TEST_BINARY_BLOB) - 1);
    tf.close();
}

void TestHttpGet::testNoServer(void)
{
    QTimer::singleShot(TEST_HTTP_TIMEOUT, this, SLOT(waitTimeout(void)));
    m_getter->getFile(QUrl("http://localhost:8081/test1.txt"));
    while(m_doneSpy->count() == 0 && m_waitTimeoutOccured == false)
        QCoreApplication::processEvents();

    QCOMPARE(m_doneSpy->count(), 1);
    QCOMPARE(m_doneSpy->at(0).at(0).toBool(), true);
    QCOMPARE(m_waitTimeoutOccured, false);
}

void TestHttpGet::testServerTimestamp(void)
{
    QList<QByteArray> responses;
    responses << QByteArray(
        "HTTP/1.1 200 OK\r\n"
        "Last-Modified: Wed, 20 Jan 2010 10:20:30\r\n" // RFC 822
        "Date: Wed, 20 Jan 2010 10:20:30\r\n"
        "\r\n"
        "\r\n");
    responses << QByteArray(
        "HTTP/1.1 200 OK\r\n"
        "Last-Modified: Sat Feb 19 09:08:07 2011\r\n" // asctime
        "Date: Sat Feb 19 09:08:07 2011\r\n"
        "\r\n"
        "\r\n");

    QList<QDateTime> times;
    times << QDateTime::fromString("2010-01-20T11:20:30", Qt::ISODate);
    times << QDateTime::fromString("2011-02-19T10:08:07", Qt::ISODate);

    m_daemon->setResponsesToSend(responses);

    QTimer::singleShot(TEST_HTTP_TIMEOUT, this, SLOT(waitTimeout(void)));

    int count = m_doneSpy->count();
    for(int i = 0; i < responses.size(); ++i) {
        m_getter->getFile(QUrl("http://localhost:8080/test1.txt"));
        while(m_doneSpy->count() == count && m_waitTimeoutOccured == false)
            QCoreApplication::processEvents();
        count = m_doneSpy->count();
        QCOMPARE(m_getter->timestamp(), times.at(i));
    }
}

void TestHttpGet::testMovedQuery(void)
{
    QList<QByteArray> responses;
    responses << QByteArray(
        "HTTP/1.1 302 Found\r\n"
        "Location: http://localhost:8080/test2.php\r\n"
        "Date: " + m_now.toLatin1() + "\r\n"
        "Last-Modified: " + m_now.toLatin1() + "\r\n"
        "\r\n");
    responses << QByteArray(
        "HTTP/1.1 200 OK\r\n"
        "Last-Modified: " + m_now.toLatin1() + "\r\n"
        "Date: " + m_now.toLatin1() + "\r\n"
        "\r\n"
        "<html></html>\r\n\r\n");
    m_daemon->setResponsesToSend(responses);

    QTimer::singleShot(TEST_HTTP_TIMEOUT, this, SLOT(waitTimeout(void)));

    m_getter->getFile(QUrl("http://localhost:8080/test1.php?var=1&b=foo"));
    while(m_doneSpy->count() == 0 && m_waitTimeoutOccured == false)
        QCoreApplication::processEvents();

    QCOMPARE(m_doneSpy->count(), 1);
    QCOMPARE(m_waitTimeoutOccured, false);
    QCOMPARE(m_getter->httpResponse(), 200);
    QCOMPARE(m_daemon->lastRequestData().size(), 2);
    QCOMPARE(m_daemon->lastRequestData().at(0).startsWith("GET"), true);
    QCOMPARE(m_daemon->lastRequestData().at(1).startsWith("GET"), true);
    // current implementation keeps order of query items.
    QCOMPARE((bool)m_daemon->lastRequestData().at(1).contains("/test2.php?var=1&b=foo"), true);
}

QTEST_MAIN(TestHttpGet)

// this include is needed because we don't use a separate header file for the
// test class. It also needs to be at the end.
#include "test-httpget.moc"