|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the test suite of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 |
|
43 #include <QDomDocument> |
|
44 #include <QHttp> |
|
45 #include <QTcpServer> |
|
46 #include <QTcpSocket> |
|
47 #include <QTimer> |
|
48 #include <QtDebug> |
|
49 #include <QtTest/QtTest> |
|
50 #include <QXmlDefaultHandler> |
|
51 #include <QXmlInputSource> |
|
52 #include <QXmlSimpleReader> |
|
53 |
|
54 //TESTED_CLASS= |
|
55 //TESTED_FILES= |
|
56 |
|
57 class tst_QXmlInputSource : public QObject |
|
58 { |
|
59 Q_OBJECT |
|
60 |
|
61 private slots: |
|
62 void reset() const; |
|
63 void resetSimplified() const; |
|
64 void waitForReadyIODevice() const; |
|
65 }; |
|
66 |
|
67 /*! |
|
68 \internal |
|
69 \since 4.4 |
|
70 |
|
71 See task 166278. |
|
72 */ |
|
73 void tst_QXmlInputSource::reset() const |
|
74 { |
|
75 const QString input(QString::fromLatin1("<element attribute1='value1' attribute2='value2'/>")); |
|
76 |
|
77 QXmlSimpleReader reader; |
|
78 QXmlDefaultHandler handler; |
|
79 reader.setContentHandler(&handler); |
|
80 |
|
81 QXmlInputSource source; |
|
82 source.setData(input); |
|
83 |
|
84 QCOMPARE(source.data(), input); |
|
85 |
|
86 source.reset(); |
|
87 QCOMPARE(source.data(), input); |
|
88 |
|
89 source.reset(); |
|
90 QVERIFY(reader.parse(source)); |
|
91 source.reset(); |
|
92 QCOMPARE(source.data(), input); |
|
93 } |
|
94 |
|
95 /*! |
|
96 \internal |
|
97 \since 4.4 |
|
98 |
|
99 See task 166278. |
|
100 */ |
|
101 void tst_QXmlInputSource::resetSimplified() const |
|
102 { |
|
103 const QString input(QString::fromLatin1("<element/>")); |
|
104 |
|
105 QXmlSimpleReader reader; |
|
106 |
|
107 QXmlInputSource source; |
|
108 source.setData(input); |
|
109 |
|
110 QVERIFY(reader.parse(source)); |
|
111 source.reset(); |
|
112 QCOMPARE(source.data(), input); |
|
113 } |
|
114 |
|
115 class ServerAndClient : public QObject |
|
116 { |
|
117 Q_OBJECT |
|
118 |
|
119 public: |
|
120 ServerAndClient(QEventLoop &ev) : success(false) |
|
121 , eventLoop(ev) |
|
122 { |
|
123 setObjectName("serverAndClient"); |
|
124 tcpServer = new QTcpServer(this); |
|
125 connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newConnection())); |
|
126 tcpServer->listen(QHostAddress::LocalHost, 1088); |
|
127 httpClient = new QHttp(this); |
|
128 connect(httpClient, SIGNAL(requestFinished(int, bool)), SLOT(requestFinished(int, bool))); |
|
129 } |
|
130 |
|
131 bool success; |
|
132 QEventLoop &eventLoop; |
|
133 |
|
134 public slots: |
|
135 void doIt() |
|
136 { |
|
137 QUrl url("http://127.0.0.1:1088"); |
|
138 httpClient->setHost( url.host(), 1088); |
|
139 QHttpRequestHeader req_head("POST", url.path()); |
|
140 req_head.setValue("host", url.host()); |
|
141 req_head.setValue("user-agent", "xml-test"); |
|
142 req_head.setValue("keep-alive", "false"); |
|
143 |
|
144 QByteArray xmlrpc("<methodCall>\r\n\ |
|
145 <methodName>SFD.GetVersion</methodName>\r\n\ |
|
146 <params/>\r\n\ |
|
147 </methodCall>"); |
|
148 req_head.setContentLength(xmlrpc.size()); |
|
149 req_head.setContentType("text/xml"); |
|
150 |
|
151 httpClient->request(req_head, xmlrpc); |
|
152 } |
|
153 |
|
154 void requestFinished(int, bool isError) |
|
155 { |
|
156 QVERIFY(!isError); |
|
157 } |
|
158 |
|
159 private slots: |
|
160 void newConnection() |
|
161 { |
|
162 QTcpSocket *const s = tcpServer->nextPendingConnection(); |
|
163 |
|
164 if(s) |
|
165 connect(s, SIGNAL(readyRead()), this, SLOT(readyRead())); |
|
166 } |
|
167 |
|
168 void readyRead() |
|
169 { |
|
170 QTcpSocket *const s = static_cast<QTcpSocket *>(sender()); |
|
171 int bodyLength = -1; |
|
172 |
|
173 while(s->canReadLine()) |
|
174 { |
|
175 const QString line(s->readLine()); |
|
176 |
|
177 if(line.startsWith("content-length:")) |
|
178 bodyLength = line.mid(15).toInt(); |
|
179 |
|
180 if(line == "\r\n") |
|
181 { |
|
182 if(bodyLength == -1) |
|
183 { |
|
184 Q_ASSERT_X(false, Q_FUNC_INFO, |
|
185 "No length was specified in the header."); |
|
186 return; |
|
187 } |
|
188 |
|
189 QDomDocument domDoc; |
|
190 success = domDoc.setContent(s->read(bodyLength)); |
|
191 eventLoop.exit(); |
|
192 } |
|
193 } |
|
194 } |
|
195 |
|
196 private: |
|
197 QTcpServer *tcpServer; |
|
198 QHttp* httpClient; |
|
199 }; |
|
200 |
|
201 void tst_QXmlInputSource::waitForReadyIODevice() const |
|
202 { |
|
203 QEventLoop el; |
|
204 ServerAndClient sv(el); |
|
205 QTimer::singleShot(1, &sv, SLOT(doIt())); |
|
206 |
|
207 el.exec(); |
|
208 QVERIFY(sv.success); |
|
209 } |
|
210 |
|
211 QTEST_MAIN(tst_QXmlInputSource) |
|
212 #include "tst_qxmlinputsource.moc" |