-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from ugnelis/added-tests
Added tests
- Loading branch information
Showing
12 changed files
with
388 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#include "customnetworkreply.h" | ||
|
||
CustomNetworkReply::CustomNetworkReply(QObject *parent) | ||
: QNetworkReply(parent), content(nullptr), offset(0) { | ||
} | ||
|
||
CustomNetworkReply::~CustomNetworkReply() { | ||
} | ||
|
||
void CustomNetworkReply::setHttpStatusCode(int code, const QByteArray &statusText) { | ||
setAttribute(QNetworkRequest::HttpStatusCodeAttribute, code); | ||
if (statusText.isNull()) | ||
return; | ||
|
||
setAttribute(QNetworkRequest::HttpReasonPhraseAttribute, statusText); | ||
} | ||
|
||
void CustomNetworkReply::setHeader(QNetworkRequest::KnownHeaders header, const QVariant &value) { | ||
QNetworkReply::setHeader(header, value); | ||
} | ||
|
||
void CustomNetworkReply::setContentType(const QByteArray &contentType) { | ||
setHeader(QNetworkRequest::ContentTypeHeader, contentType); | ||
} | ||
|
||
void CustomNetworkReply::setContent(const QString &content) { | ||
setContent(content.toUtf8()); | ||
} | ||
|
||
void CustomNetworkReply::setContent(const QByteArray &content) { | ||
this->content = content; | ||
offset = 0; | ||
|
||
open(ReadOnly | Unbuffered); | ||
setHeader(QNetworkRequest::ContentLengthHeader, QVariant(content.size())); | ||
|
||
QTimer::singleShot(0, this, SIGNAL(readyRead())); | ||
QTimer::singleShot(0, this, SIGNAL(finished())); | ||
} | ||
|
||
void CustomNetworkReply::abort() { | ||
// No operation. | ||
} | ||
|
||
qint64 CustomNetworkReply::bytesAvailable() const { | ||
return content.size() - offset + QIODevice::bytesAvailable(); | ||
} | ||
|
||
bool CustomNetworkReply::isSequential() const { | ||
return true; | ||
} | ||
|
||
qint64 CustomNetworkReply::readData(char *data, qint64 maxSize) { | ||
if (offset >= content.size()) | ||
return -1; | ||
|
||
qint64 number = qMin(maxSize, content.size() - offset); | ||
memcpy(data, content.constData() + offset, number); | ||
offset += number; | ||
|
||
return number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#ifndef INSTANT_TRANSLATOR_CUSTOMNETWORKREPLY_H | ||
#define INSTANT_TRANSLATOR_CUSTOMNETWORKREPLY_H | ||
|
||
#include <QNetworkAccessManager> | ||
#include <QNetworkReply> | ||
#include <QBuffer> | ||
#include <QTimer> | ||
|
||
/** | ||
* Custom QNetworkReply class. | ||
*/ | ||
class CustomNetworkReply : public QNetworkReply { | ||
Q_OBJECT | ||
|
||
public: | ||
/** | ||
* Constructs an object with parent object parent. | ||
* @param parent Parent of an object may be viewed as the object's owner. | ||
*/ | ||
explicit CustomNetworkReply(QObject *parent = nullptr); | ||
|
||
/** | ||
* Destructor. | ||
*/ | ||
~CustomNetworkReply() override; | ||
|
||
/** | ||
* | ||
* @param code | ||
* @param statusText | ||
*/ | ||
void setHttpStatusCode(int code, const QByteArray &statusText = QByteArray()); | ||
|
||
/** | ||
* Sets the value of the known header header to be | ||
* value, overriding any previously set headers. | ||
* @param header Header. | ||
* @param value Value. | ||
*/ | ||
void setHeader(QNetworkRequest::KnownHeaders header, const QVariant &value); | ||
|
||
/** | ||
* Set content type, e.g., "text/html". | ||
* @param contentType Content type. | ||
*/ | ||
void setContentType(const QByteArray &contentType); | ||
|
||
/** | ||
* Set content. | ||
* @param content Content. | ||
*/ | ||
void setContent(const QString &content); | ||
|
||
/** | ||
* Set content. | ||
* @param content Content. | ||
*/ | ||
void setContent(const QByteArray &content); | ||
|
||
/** | ||
* Aborts the operation immediately and close | ||
* down any network connections still open. | ||
*/ | ||
void abort() override; | ||
|
||
/** | ||
* Returns the number of bytes that are available for reading. | ||
* @return Bytes that are available for reading. | ||
*/ | ||
qint64 bytesAvailable() const override; | ||
|
||
/** | ||
* Returns true if this device is sequential; otherwise returns false. | ||
* @return Returns true if this device is sequential. | ||
*/ | ||
bool isSequential() const override; | ||
|
||
protected: | ||
/** | ||
* Reads up to maxSize bytes from the device into data, and | ||
* returns the number of bytes read or -1 if an error occurred. | ||
* @param data Data. | ||
* @param maxSize Max size. | ||
* @return returns the number of bytes read or -1 if an error occurred. | ||
*/ | ||
qint64 readData(char *data, qint64 maxSize) override; | ||
|
||
private: | ||
QByteArray content; | ||
qint64 offset; | ||
}; | ||
|
||
#endif //INSTANT_TRANSLATOR_CUSTOMNETWORKREPLY_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#include <gtest/gtest.h> | ||
#include <gmock/gmock.h> | ||
#include <QList> | ||
#include <QNetworkReply> | ||
#include "utils/customnetworkreply.h" | ||
|
||
TEST(CustomNetworkReplyTests, setContentType_CheckContentType_True) { | ||
CustomNetworkReply reply; | ||
reply.setContentType("application/json"); | ||
QList<QNetworkReply::RawHeaderPair> rawHeaderList = reply.rawHeaderPairs(); | ||
|
||
ASSERT_EQ("application/json", rawHeaderList.at(0).second); | ||
} | ||
|
||
TEST(CustomNetworkReplyTests, setContent_GiveQStringAsAValue_True) { | ||
CustomNetworkReply reply; | ||
reply.setContent(QString("This is test!")); | ||
QByteArray content = reply.readAll(); | ||
|
||
ASSERT_EQ(QString("This is test!"), content); | ||
} | ||
|
||
TEST(CustomNetworkReplyTests, setContent_GiveQByteArrayAsAValue_True) { | ||
CustomNetworkReply reply; | ||
reply.setContent(QByteArray("This is test!")); | ||
QByteArray content = reply.readAll(); | ||
|
||
ASSERT_EQ(QString("This is test!"), content); | ||
} | ||
|
||
TEST(CustomNetworkReplyTests, abort_DoNotDoAnything_True) { | ||
CustomNetworkReply reply; | ||
reply.setContent(QByteArray("")); | ||
QByteArray content = reply.readAll(); | ||
reply.abort(); | ||
|
||
ASSERT_EQ(QString(""), content); | ||
} | ||
|
||
TEST(CustomNetworkReplyTests, bytesAvailable_CheckBytes_True) { | ||
CustomNetworkReply reply; | ||
reply.setContent(QByteArray("This is test!")); | ||
|
||
qint64 bytesAvailable = reply.bytesAvailable(); | ||
|
||
ASSERT_EQ(13, bytesAvailable); | ||
} | ||
|
||
TEST(CustomNetworkReplyTests, isSequential_CheckIfItIsSequeantial_True) { | ||
CustomNetworkReply reply; | ||
bool isSequential = reply.isSequential(); | ||
|
||
ASSERT_EQ(true, isSequential); | ||
} |
Oops, something went wrong.