From 7a015f06667ee7be9864874440d0580df5aae783 Mon Sep 17 00:00:00 2001 From: Patrick Sean Klein Date: Wed, 10 May 2023 21:30:42 +0100 Subject: [PATCH] Test Content-Type extraction. --- src/core/NetworkRequest.cpp | 2 +- tests/TestNetworkRequest.cpp | 32 +++++++++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/core/NetworkRequest.cpp b/src/core/NetworkRequest.cpp index e14a7e736..dfa853f05 100644 --- a/src/core/NetworkRequest.cpp +++ b/src/core/NetworkRequest.cpp @@ -27,7 +27,7 @@ namespace contentType = tokens[0].trimmed(); for (int i = 1; i < tokens.size(); ++i) { auto parameterTokens = tokens[i].split("="); - contentTypeParameters[parameterTokens[0]] = parameterTokens[1]; + contentTypeParameters[parameterTokens[0].trimmed()] = parameterTokens[1].trimmed(); } return {contentType, contentTypeParameters}; } diff --git a/tests/TestNetworkRequest.cpp b/tests/TestNetworkRequest.cpp index b024cf562..bef5db909 100644 --- a/tests/TestNetworkRequest.cpp +++ b/tests/TestNetworkRequest.cpp @@ -7,10 +7,16 @@ QTEST_GUILESS_MAIN(TestNetworkRequest) +using ContentTypeParameters_t = QHash; +Q_DECLARE_METATYPE(ContentTypeParameters_t); + void TestNetworkRequest::testNetworkRequest() { QFETCH(QUrl, requestedURL); QFETCH(QByteArray, expectedContent); + QFETCH(QString, responseContentType); + QFETCH(QString, expectedContentType); + QFETCH(ContentTypeParameters_t, expectedContentTypeParameters); QFETCH(QString, expectedUserAgent); QFETCH(bool, expectError); QFETCH(QNetworkReply::NetworkError, error); @@ -26,7 +32,7 @@ void TestNetworkRequest::testNetworkRequest() QRegularExpression(expectedUserAgent))) .reply(); if (!expectError) { - reply.withBody(expectedContent); + reply.withBody(expectedContent).withHeader(QNetworkRequest::ContentTypeHeader, responseContentType); } else { reply.withError(error); } @@ -55,8 +61,11 @@ void TestNetworkRequest::testNetworkRequest() QCOMPARE(request.URL(), requestedURL); if(!expectError) { QCOMPARE(actualContent, expectedContent); + QCOMPARE(request.ContentType(), expectedContentType); + QCOMPARE(request.ContentTypeParameters(), expectedContentTypeParameters); QCOMPARE(didSucceed, true); QCOMPARE(didError, false); + QCOMPARE(request.Reply()->isFinished(), true); } else { QCOMPARE(didSucceed, false); QCOMPARE(didError, true); @@ -66,6 +75,9 @@ void TestNetworkRequest::testNetworkRequest_data() { QTest::addColumn("requestedURL"); QTest::addColumn("expectedContent"); + QTest::addColumn("responseContentType"); + QTest::addColumn("expectedContentType"); + QTest::addColumn("expectedContentTypeParameters"); QTest::addColumn("expectedUserAgent"); QTest::addColumn("expectError"); QTest::addColumn("error"); @@ -75,15 +87,29 @@ void TestNetworkRequest::testNetworkRequest_data() const QUrl& exampleURL = QUrl{"https://example.com"}; const QByteArray& exampleContent = QString{"test-content"}.toUtf8(); - QTest::newRow("successful request") << exampleURL << exampleContent << defaultUserAgent - << false << QNetworkReply::NetworkError::NoError; + QTest::newRow("successful request") << exampleURL << exampleContent << "text/plain" + << "text/plain" << ContentTypeParameters_t{} + << defaultUserAgent << false << QNetworkReply::NetworkError::NoError; + QTest::newRow("content type") << exampleURL << exampleContent << "application/test-content-type" + << "application/test-content-type" << ContentTypeParameters_t{} + << defaultUserAgent << false << QNetworkReply::NetworkError::NoError; + QTest::newRow("content type parameters") << exampleURL << exampleContent << "application/test-content-type;test-param=test-value" + << "application/test-content-type" << ContentTypeParameters_t {{"test-param", "test-value"}} + << defaultUserAgent << false << QNetworkReply::NetworkError::NoError; + QTest::newRow("content type parameters trimmed") << exampleURL << exampleContent << "application/test-content-type; test-param = test-value" + << "application/test-content-type" << ContentTypeParameters_t {{"test-param", "test-value"}} + << defaultUserAgent << false << QNetworkReply::NetworkError::NoError; } void TestNetworkRequest::testNetworkRequestTimeout() { + // Timeout should work for single request + // Timeout should capture entire duration, including redirects // TODO } void TestNetworkRequest::testNetworkRequestRedirects() { + // Should respect max number of redirects + // Headers, Reply, etc. should reflect final request // TODO }