93 lines
3.7 KiB
C++
93 lines
3.7 KiB
C++
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
|
* *
|
|
* Copyright (C) 2015 Simon Stuerz <simon.stuerz@guh.guru> *
|
|
* *
|
|
* This file is part of guh. *
|
|
* *
|
|
* Guh 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, version 2 of the License. *
|
|
* *
|
|
* Guh 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 guh. If not, see <http://www.gnu.org/licenses/>. *
|
|
* *
|
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
|
|
|
#include "guhtestbase.h"
|
|
#include "guhcore.h"
|
|
#include "devicemanager.h"
|
|
#include "mocktcpserver.h"
|
|
#include "webserver.h"
|
|
|
|
#include <QtTest/QtTest>
|
|
#include <QCoreApplication>
|
|
#include <QTcpSocket>
|
|
#include <QNetworkAccessManager>
|
|
#include <QNetworkRequest>
|
|
#include <QNetworkReply>
|
|
#include <QCoreApplication>
|
|
#include <QJsonDocument>
|
|
#include <QHttpPart>
|
|
#include <QMetaType>
|
|
|
|
using namespace guhserver;
|
|
|
|
class TestRestVendors: public GuhTestBase
|
|
{
|
|
Q_OBJECT
|
|
|
|
private slots:
|
|
void getVendors();
|
|
};
|
|
|
|
void TestRestVendors::getVendors()
|
|
{
|
|
QNetworkAccessManager *nam = new QNetworkAccessManager(this);
|
|
QSignalSpy clientSpy(nam, SIGNAL(finished(QNetworkReply*)));
|
|
|
|
// Get all vendors
|
|
QNetworkRequest request;
|
|
request.setUrl(QUrl("http://localhost:3000/api/v1/vendors"));
|
|
QNetworkReply *reply = nam->get(request);
|
|
clientSpy.wait();
|
|
QVERIFY2(clientSpy.count() == 1, "expected exactly 1 response from webserver");
|
|
int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
|
QCOMPARE(statusCode, 200);
|
|
QByteArray data = reply->readAll();
|
|
reply->deleteLater();
|
|
|
|
QJsonParseError error;
|
|
QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &error);
|
|
QCOMPARE(error.error, QJsonParseError::NoError);
|
|
QVariantList vendorList = jsonDoc.toVariant().toList();
|
|
QVERIFY2(vendorList.count() >= 1, "there should be at least one vendor.");
|
|
|
|
|
|
// Get each of thouse vendors individualy
|
|
foreach (const QVariant &vendor, vendorList) {
|
|
QVariantMap vendorMap = vendor.toMap();
|
|
QNetworkRequest request(QUrl(QString("http://localhost:3000/api/v1/vendors/%1").arg(vendorMap.value("id").toString())));
|
|
request.setHeader(QNetworkRequest::ContentTypeHeader, "text/json");
|
|
clientSpy.clear();
|
|
QNetworkReply *reply = nam->get(request);
|
|
clientSpy.wait();
|
|
QVERIFY2(clientSpy.count() == 1, "expected exactly 1 response from webserver");
|
|
int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
|
QCOMPARE(statusCode, 200);
|
|
jsonDoc = QJsonDocument::fromJson(reply->readAll(), &error);
|
|
QCOMPARE(error.error, QJsonParseError::NoError);
|
|
reply->deleteLater();
|
|
|
|
}
|
|
nam->deleteLater();
|
|
}
|
|
|
|
|
|
#include "testrestvendors.moc"
|
|
QTEST_MAIN(TestRestVendors)
|