diff options
author | Simon Hausmann <simon.hausmann@theqtcompany.com> | 2015-04-22 17:15:30 +0200 |
---|---|---|
committer | Frederik Gladhorn <frederik.gladhorn@theqtcompany.com> | 2015-04-25 12:02:55 +0000 |
commit | 79d56651edc05133ebae13bb93733373b10380f9 (patch) | |
tree | d60393857c0f1b28f9e34c55f4461e2f25c99a9e /tests/auto/shared/testhttpserver.cpp | |
parent | bfd596f36724029441d54095a126d25168e105c1 (diff) |
Prospective fix for flakey "network" related QML testsv5.4.2
Replace hard-coded server ports with dynamically allocated ports.
Change-Id: Iab8f9a88343a9f2c49af3cd700c954c13c3bf121
Reviewed-by: Frederik Gladhorn <frederik.gladhorn@theqtcompany.com>
Diffstat (limited to 'tests/auto/shared/testhttpserver.cpp')
-rw-r--r-- | tests/auto/shared/testhttpserver.cpp | 39 |
1 files changed, 35 insertions, 4 deletions
diff --git a/tests/auto/shared/testhttpserver.cpp b/tests/auto/shared/testhttpserver.cpp index ff7bb54a64..eeffc27f51 100644 --- a/tests/auto/shared/testhttpserver.cpp +++ b/tests/auto/shared/testhttpserver.cpp @@ -87,9 +87,28 @@ TestHTTPServer::TestHTTPServer() } -bool TestHTTPServer::listen(quint16 port) +bool TestHTTPServer::listen() { - return server.listen(QHostAddress::LocalHost, port); + return server.listen(QHostAddress::LocalHost, 0); +} + +QUrl TestHTTPServer::baseUrl() const +{ + QUrl url; + url.setScheme(QStringLiteral("http")); + url.setHost(QStringLiteral("127.0.0.1")); + url.setPort(server.serverPort()); + return url; +} + +QUrl TestHTTPServer::url(const QString &documentPath) const +{ + return baseUrl().resolved(documentPath); +} + +QString TestHTTPServer::urlString(const QString &documentPath) const +{ + return url(documentPath).toString(); } QString TestHTTPServer::errorString() const @@ -117,6 +136,11 @@ void TestHTTPServer::addRedirect(const QString &filename, const QString &redirec redirects.insert(filename, redirectName); } +void TestHTTPServer::registerFileNameForContentSubstitution(const QString &fileName) +{ + contentSubstitutedFileNames.insert(fileName); +} + bool TestHTTPServer::wait(const QUrl &expect, const QUrl &reply, const QUrl &body) { m_state = AwaitingHeader; @@ -135,6 +159,8 @@ bool TestHTTPServer::wait(const QUrl &expect, const QUrl &reply, const QUrl &bod bodyData = bodyFile.readAll(); } + const QByteArray serverHostUrl = QByteArrayLiteral("127.0.0.1:") + QByteArray::number(server.serverPort()); + QByteArray line; bool headers_done = false; while (!(line = expectFile.readLine()).isEmpty()) { @@ -143,10 +169,12 @@ bool TestHTTPServer::wait(const QUrl &expect, const QUrl &reply, const QUrl &bod headers_done = true; continue; } - if (headers_done) + if (headers_done) { waitData.body.append(line); - else + } else { + line.replace("{{ServerHostUrl}}", serverHostUrl); waitData.headers.append(line); + } } /* while (waitData.endsWith('\n')) @@ -277,6 +305,9 @@ bool TestHTTPServer::reply(QTcpSocket *socket, const QByteArray &fileName) return true; QByteArray data = file.readAll(); + if (contentSubstitutedFileNames.contains("/" + fileName)) { + data.replace(QByteArrayLiteral("{{ServerBaseUrl}}"), baseUrl().toString().toUtf8()); + } QByteArray response = "HTTP/1.0 200 OK\r\nContent-type: text/html; charset=UTF-8\r\nContent-length: "; response += QByteArray::number(data.count()); |