2010-03-24 00:11:46 +01:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-03-24 00:11:46 +01:00
|
|
|
|
|
|
|
Clementine is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Clementine is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2010-03-03 20:14:14 +01:00
|
|
|
#include "mock_networkaccessmanager.h"
|
|
|
|
|
|
|
|
#include <QtDebug>
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
using std::min;
|
|
|
|
|
|
|
|
using ::testing::MakeMatcher;
|
|
|
|
using ::testing::Matcher;
|
|
|
|
using ::testing::MatcherInterface;
|
2010-07-27 15:21:28 +02:00
|
|
|
using ::testing::MatchResultListener;
|
2010-03-03 20:14:14 +01:00
|
|
|
using ::testing::Return;
|
|
|
|
|
|
|
|
class RequestForUrlMatcher : public MatcherInterface<const QNetworkRequest&> {
|
|
|
|
public:
|
|
|
|
RequestForUrlMatcher(const QString& contains,
|
|
|
|
const QMap<QString, QString>& expected_params)
|
|
|
|
: contains_(contains),
|
|
|
|
expected_params_(expected_params) {
|
|
|
|
}
|
|
|
|
virtual ~RequestForUrlMatcher() {}
|
|
|
|
|
|
|
|
virtual bool Matches(const QNetworkRequest& req) const {
|
|
|
|
const QUrl& url = req.url();
|
|
|
|
|
|
|
|
if (!url.toString().contains(contains_)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-03-03 21:35:19 +01:00
|
|
|
for (QMap<QString, QString>::const_iterator it = expected_params_.constBegin();
|
|
|
|
it != expected_params_.constEnd(); ++it) {
|
2010-03-03 20:14:14 +01:00
|
|
|
if (!url.hasQueryItem(it.key()) ||
|
|
|
|
url.queryItemValue(it.key()) != it.value()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-07-27 15:21:28 +02:00
|
|
|
virtual bool MatchAndExplain(const QNetworkRequest& req, MatchResultListener* listener) const {
|
|
|
|
*listener << "which is " << req.url().toString().toUtf8().constData();
|
|
|
|
return Matches(req);
|
|
|
|
}
|
|
|
|
|
2010-03-03 20:14:14 +01:00
|
|
|
virtual void DescribeTo(::std::ostream* os) const {
|
|
|
|
*os << "matches url";
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
QString contains_;
|
|
|
|
QMap<QString, QString> expected_params_;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline Matcher<const QNetworkRequest&> RequestForUrl(
|
|
|
|
const QString& contains,
|
|
|
|
const QMap<QString, QString>& params) {
|
|
|
|
return MakeMatcher(new RequestForUrlMatcher(contains, params));
|
|
|
|
}
|
|
|
|
|
|
|
|
MockNetworkReply* MockNetworkAccessManager::ExpectGet(
|
|
|
|
const QString& contains,
|
|
|
|
const QMap<QString, QString>& expected_params,
|
|
|
|
int status,
|
2010-03-03 21:35:19 +01:00
|
|
|
const QByteArray& data) {
|
2010-03-03 20:14:14 +01:00
|
|
|
MockNetworkReply* reply = new MockNetworkReply(data);
|
2010-03-03 21:35:19 +01:00
|
|
|
reply->setAttribute(QNetworkRequest::HttpStatusCodeAttribute, status);
|
2010-03-03 20:14:14 +01:00
|
|
|
|
|
|
|
EXPECT_CALL(*this, createRequest(
|
2014-02-21 17:24:49 +01:00
|
|
|
GetOperation, RequestForUrl(contains, expected_params), nullptr)).
|
2010-03-03 20:14:14 +01:00
|
|
|
WillOnce(Return(reply));
|
|
|
|
|
|
|
|
return reply;
|
|
|
|
}
|
|
|
|
|
|
|
|
MockNetworkReply::MockNetworkReply()
|
2014-02-21 17:24:49 +01:00
|
|
|
: data_(nullptr) {
|
2010-03-03 20:14:14 +01:00
|
|
|
}
|
|
|
|
|
2010-03-03 21:35:19 +01:00
|
|
|
MockNetworkReply::MockNetworkReply(const QByteArray& data)
|
2010-03-03 20:14:14 +01:00
|
|
|
: data_(data),
|
|
|
|
pos_(0) {
|
|
|
|
}
|
|
|
|
|
2010-03-03 21:35:19 +01:00
|
|
|
void MockNetworkReply::SetData(const QByteArray& data) {
|
2010-03-03 20:14:14 +01:00
|
|
|
data_ = data;
|
|
|
|
pos_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
qint64 MockNetworkReply::readData(char* data, qint64 size) {
|
2010-03-03 21:35:19 +01:00
|
|
|
if (data_.size() == pos_) {
|
2010-03-03 20:14:14 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2010-03-03 21:35:19 +01:00
|
|
|
qint64 bytes_to_read = min(data_.size() - pos_, size);
|
|
|
|
memcpy(data, data_.constData() + pos_, bytes_to_read);
|
2010-03-03 20:14:14 +01:00
|
|
|
pos_ += bytes_to_read;
|
|
|
|
return bytes_to_read;
|
|
|
|
}
|
|
|
|
|
|
|
|
qint64 MockNetworkReply::writeData(const char* data, qint64) {
|
|
|
|
ADD_FAILURE() << "Something tried to write to a QNetworkReply";
|
2010-07-27 15:21:28 +02:00
|
|
|
return -1;
|
2010-03-03 20:14:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MockNetworkReply::Done() {
|
|
|
|
setOpenMode(QIODevice::ReadOnly);
|
|
|
|
emit finished();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MockNetworkReply::setAttribute(QNetworkRequest::Attribute code, const QVariant& value) {
|
|
|
|
QNetworkReply::setAttribute(code, value);
|
|
|
|
}
|