File: tst_qdjangohttprequest.cpp

package info (click to toggle)
qdjango 0.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 908 kB
  • ctags: 959
  • sloc: cpp: 8,233; python: 51; sh: 32; makefile: 13
file content (106 lines) | stat: -rw-r--r-- 3,771 bytes parent folder | download
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
/*
 * Copyright (C) 2010-2014 Jeremy Lainé
 * Contact: https://github.com/jlaine/qdjango
 *
 * This file is part of the QDjango Library.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

#include <QtTest>

#include "QDjangoHttpRequest.h"
#include "QDjangoHttpRequest_p.h"

/** Test QDjangoHttpServer class.
 */
class tst_QDjangoHttpRequest : public QObject
{
    Q_OBJECT

private slots:
    void testBody();
    void testGet();
    void testPost();
};

void tst_QDjangoHttpRequest::testBody()
{
    QDjangoHttpRequest request;

    QCOMPARE(request.body(), QByteArray());

    request.d->buffer = QByteArray("foo=bar");
    QCOMPARE(request.body(), QByteArray("foo=bar"));
}

void tst_QDjangoHttpRequest::testGet()
{
    QDjangoHttpRequest request;

    // plain
    request.d->meta.insert("QUERY_STRING", "foo=bar&baz=qux");
    QCOMPARE(request.get(QLatin1String("foo")), QLatin1String("bar"));
    QCOMPARE(request.get(QLatin1String("baz")), QLatin1String("qux"));

    // space encoded as plus
    request.d->meta.insert("QUERY_STRING", "foo=bar+more&baz=qux");
    QCOMPARE(request.get(QLatin1String("foo")), QLatin1String("bar more"));
    QCOMPARE(request.get(QLatin1String("baz")), QLatin1String("qux"));

    // plus encoded as %2B
    request.d->meta.insert("QUERY_STRING", "foo=bar%2Bmore&baz=qux");
    QCOMPARE(request.get(QLatin1String("foo")), QLatin1String("bar+more"));
    QCOMPARE(request.get(QLatin1String("baz")), QLatin1String("qux"));

    // plus encoded as %2b
    request.d->meta.insert("QUERY_STRING", "foo=bar%2bmore&baz=qux");
    QCOMPARE(request.get(QLatin1String("foo")), QLatin1String("bar+more"));
    QCOMPARE(request.get(QLatin1String("baz")), QLatin1String("qux"));

    // at encoded as %40
    request.d->meta.insert("QUERY_STRING", "foo=bar%40example.com&baz=qux");
    QCOMPARE(request.get(QLatin1String("foo")), QLatin1String("bar@example.com"));
    QCOMPARE(request.get(QLatin1String("baz")), QLatin1String("qux"));
}

void tst_QDjangoHttpRequest::testPost()
{
    QDjangoHttpRequest request;

    // plain
    request.d->buffer = QByteArray("foo=bar&baz=qux");
    QCOMPARE(request.post(QLatin1String("foo")), QLatin1String("bar"));
    QCOMPARE(request.post(QLatin1String("baz")), QLatin1String("qux"));

    // space encoded as plus
    request.d->buffer = QByteArray("foo=bar+more&baz=qux");
    QCOMPARE(request.post(QLatin1String("foo")), QLatin1String("bar more"));
    QCOMPARE(request.post(QLatin1String("baz")), QLatin1String("qux"));

    // plus encoded as %2B
    request.d->buffer = QByteArray("foo=bar%2Bmore&baz=qux");
    QCOMPARE(request.post(QLatin1String("foo")), QLatin1String("bar+more"));
    QCOMPARE(request.post(QLatin1String("baz")), QLatin1String("qux"));

    // plus encoded as %2b
    request.d->buffer = QByteArray("foo=bar%2bmore&baz=qux");
    QCOMPARE(request.post(QLatin1String("foo")), QLatin1String("bar+more"));
    QCOMPARE(request.post(QLatin1String("baz")), QLatin1String("qux"));

    // at encoded as %40
    request.d->buffer = QByteArray("foo=bar%40example.com&baz=qux");
    QCOMPARE(request.post(QLatin1String("foo")), QLatin1String("bar@example.com"));
    QCOMPARE(request.post(QLatin1String("baz")), QLatin1String("qux"));
}

QTEST_MAIN(tst_QDjangoHttpRequest)
#include "tst_qdjangohttprequest.moc"