1 | // Copyright (C) 2016 The Qt Company Ltd. |
---|---|
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #ifndef QTEST_NETWORK_H |
5 | #define QTEST_NETWORK_H |
6 | |
7 | #include <QtTest/qtest.h> |
8 | |
9 | // enable NETWORK features |
10 | #ifndef QT_NETWORK_LIB |
11 | #define QT_NETWORK_LIB |
12 | #endif |
13 | |
14 | #if 0 |
15 | #pragma qt_class(QtTestNetwork) |
16 | #endif |
17 | |
18 | #include <QtNetwork/QHostAddress> |
19 | #include <QtNetwork/QNetworkCookie> |
20 | #include <QtNetwork/QNetworkReply> |
21 | |
22 | #if 0 |
23 | // inform syncqt |
24 | #pragma qt_no_master_include |
25 | #endif |
26 | |
27 | QT_BEGIN_NAMESPACE |
28 | |
29 | namespace QTest |
30 | { |
31 | template<> |
32 | inline char *toString<QHostAddress>(const QHostAddress &addr) |
33 | { |
34 | switch (addr.protocol()) { |
35 | case QAbstractSocket::UnknownNetworkLayerProtocol: |
36 | return qstrdup("<unknown address (parse error)>"); |
37 | case QAbstractSocket::AnyIPProtocol: |
38 | return qstrdup("QHostAddress::Any"); |
39 | case QAbstractSocket::IPv4Protocol: |
40 | case QAbstractSocket::IPv6Protocol: |
41 | break; |
42 | } |
43 | |
44 | return toString(str: addr.toString()); |
45 | } |
46 | |
47 | inline char *toString(QNetworkReply::NetworkError code) |
48 | { |
49 | const QMetaObject *mo = &QNetworkReply::staticMetaObject; |
50 | int index = mo->indexOfEnumerator(name: "NetworkError"); |
51 | if (index == -1) |
52 | return qstrdup(""); |
53 | |
54 | QMetaEnum qme = mo->enumerator(index); |
55 | return qstrdup(qme.valueToKey(value: code)); |
56 | } |
57 | |
58 | inline char *toString(const QNetworkCookie &cookie) |
59 | { |
60 | return toString(ba: cookie.toRawForm()); |
61 | } |
62 | |
63 | inline char *toString(const QList<QNetworkCookie> &list) |
64 | { |
65 | QByteArray result = "QList("; |
66 | if (!list.isEmpty()) { |
67 | for (const QNetworkCookie &cookie : list) |
68 | result += "QNetworkCookie("+ cookie.toRawForm() + "), "; |
69 | result.chop(n: 2); // remove trailing ", " |
70 | } |
71 | result.append(c: ')'); |
72 | return toString(ba: result); |
73 | } |
74 | |
75 | } // namespace QTest |
76 | |
77 | QT_END_NAMESPACE |
78 | |
79 | #endif |
80 |