#include <QString>
#include <QtTest>
#include <QUrl>
#include <QFile>
#include <QTextStream>
#include <QWebSocket>

#include "qwebsockethandshakerequestpart.h"

class TestTest : public QObject
{
    Q_OBJECT

public:
    TestTest();

private Q_SLOTS:
    void set_host_without_port();
    void set_host_with_port_fails();

    void parse_request_correctly();
    void parse_request_incorrectly_because_of_port();

    void test_for_possible_false_valid_url();
};

TestTest::TestTest()
{
}

void TestTest::set_host_without_port()
{
    QString hostToSet = "localhost";
    QString expectedHost = "localhost";
    QString resultHost;
    QString resultErrorString;
    QString expectedErrorString;
    QUrl testUrl;

    testUrl.setHost(hostToSet);
    resultHost = testUrl.host();
    resultErrorString = testUrl.errorString();

    QCOMPARE(resultHost, expectedHost);
    QCOMPARE(resultErrorString, expectedErrorString);
}

void TestTest::set_host_with_port_fails()
{
    QString hostToSet = "localhost:8080";
    QString expectedHost = "";
    QString resultHost;
    QString resultErrorString;
    QString expectedErrorString;
    QUrl testUrl;

    testUrl.setHost(hostToSet);
    resultHost = testUrl.host();
    resultErrorString = testUrl.errorString();

    QCOMPARE(resultHost, expectedHost);

    QCOMPARE(true, resultErrorString != expectedErrorString);
}

void TestTest::parse_request_correctly()
{
    QFile testFile(":/Responses/correct.txt");
    if (!testFile.open(QFile::ReadOnly))
    {
        QCOMPARE(true, false);
    }

    QTextStream textStream(&testFile);

    QWebSocketHandshakeRequest myRequest = QWebSocketHandshakeRequest(8080, false);
    myRequest.readHandshake(textStream);

    QString expectedHost = "localhost";
    QString resultHost;

    bool expectedIsValid = true;
    bool resultIsValid;
    int expectedPort = 8080;
    int resultPort;

    resultIsValid = myRequest.isValid();
    resultHost = myRequest.host();
    resultPort = myRequest.port();

    QCOMPARE(resultIsValid, expectedIsValid);
    QCOMPARE(resultHost, expectedHost);
    QCOMPARE(resultPort, expectedPort);
}

void TestTest::parse_request_incorrectly_because_of_port()
{
    QFile testFile(":/Responses/incorrect.txt");
    if (!testFile.open(QFile::ReadOnly))
    {
        QCOMPARE(true, false);
    }

    QTextStream textStream(&testFile);

    QWebSocketHandshakeRequest myRequest = QWebSocketHandshakeRequest(8080, true);
    myRequest.readHandshake(textStream);

    QString expectedHost = "";
    QString resultHost;

    bool expectedIsValid = false;
    bool resultIsValid;
    int expectedPort = 1234;
    int resultPort;

    resultIsValid = myRequest.isValid();
    resultHost = myRequest.host();
    resultPort = myRequest.port();

    QCOMPARE(resultIsValid, expectedIsValid);
    QCOMPARE(resultHost, expectedHost);

    // not sure if port 8080 or 1234?
    QCOMPARE(resultPort, expectedPort);
}

void TestTest::test_for_possible_false_valid_url()
{
    // host must be set for absolute urls?
    QUrl url = QUrl("ws:///ABC/DEF");

    bool expectedValidity = false;
    bool resultValidity;

    resultValidity = url.isValid();

    QCOMPARE(resultValidity, expectedValidity);
}

QTEST_APPLESS_MAIN(TestTest)

#include "tst_testtest.moc"
