Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: isTextBody #11977

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/libsync/httplogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@ Q_LOGGING_CATEGORY(lcNetworkHttp, "sync.httplogger", QtWarningMsg)

const qint64 PeekSize = 1024 * 1024;

bool isTextBody(const QString &s)
{
static const QRegularExpression regexp(QStringLiteral("^(text/.*?|(application/(xml|.*?json|x-www-form-urlencoded)(;|$)))"));
return regexp.match(s).hasMatch();
}

struct HttpContext
{
HttpContext(const QNetworkRequest &request)
Expand Down Expand Up @@ -106,7 +100,7 @@ void logHttp(const QByteArray &verb, HttpContext *ctx, QJsonObject &&header, QIO
QJsonObject body = {{QStringLiteral("length"), contentLength}};
if (contentLength > 0) {
const QString contentType = header.value(QStringLiteral("Content-Type")).toString();
if (isTextBody(contentType)) {
if (OCC::HttpLogger::isTextBody(contentType)) {
if (!device->isOpen()) {
Q_ASSERT(dynamic_cast<QBuffer *>(device));
// should we close it again?
Expand Down Expand Up @@ -205,4 +199,9 @@ QByteArray HttpLogger::requestVerb(QNetworkAccessManager::Operation operation, c
Q_UNREACHABLE();
}

bool HttpLogger::isTextBody(const QString &s)
{
static const QRegularExpression regexp(QStringLiteral("^(text/.*?|(application/(xml|.*?json|x-www-form-urlencoded)(;|$)))"));
return regexp.match(s).hasMatch();
}
}
2 changes: 2 additions & 0 deletions src/libsync/httplogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@ namespace HttpLogger {
{
return requestVerb(reply.operation(), reply.request());
}

bool OWNCLOUDSYNC_EXPORT isTextBody(const QString &s);
}
}
7 changes: 7 additions & 0 deletions test/testutility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "common/filesystembase.h"
#include "common/utility.h"
#include "libsync/httplogger.h"

using namespace std::chrono_literals;

Expand Down Expand Up @@ -322,6 +323,12 @@ private Q_SLOTS:
QVERIFY(Tags::remove(fn, testKey));
QVERIFY(!Tags::get(fn, testKey).has_value());
}

void testHttpContentTypeIsText()
{
QVERIFY(OCC::HttpLogger::isTextBody(QStringLiteral("application/json; charset=utf-8")));
QVERIFY(OCC::HttpLogger::isTextBody(QStringLiteral("application/json")));
}
};

QTEST_GUILESS_MAIN(TestUtility)
Expand Down