From 8955462f7321adde8f6da0f7aca3454b7fa40256 Mon Sep 17 00:00:00 2001 From: Dimon4eg Date: Mon, 6 May 2019 22:47:15 +0300 Subject: [PATCH] added tests for IXUrlParser (#52) * added tests for IXUrlParser * add me as author --- ixwebsocket/IXNetSystem.cpp | 2 +- test/CMakeLists.txt | 1 + test/IXUrlParserTest.cpp | 113 ++++++++++++++++++++++++++++++++++++ 3 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 test/IXUrlParserTest.cpp diff --git a/ixwebsocket/IXNetSystem.cpp b/ixwebsocket/IXNetSystem.cpp index ff290ee1..91b17bb7 100644 --- a/ixwebsocket/IXNetSystem.cpp +++ b/ixwebsocket/IXNetSystem.cpp @@ -1,6 +1,6 @@ /* * IXNetSystem.cpp - * Author: Benjamin Sergeant + * Author: Korchynskyi Dmytro * Copyright (c) 2019 Machine Zone. All rights reserved. */ diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 4f168ba5..47e1281e 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -35,6 +35,7 @@ set (SOURCES IXWebSocketServerTest.cpp IXWebSocketPingTest.cpp IXWebSocketTestConnectionDisconnection.cpp + IXUrlParserTest.cpp ) # Some unittest don't work on windows yet diff --git a/test/IXUrlParserTest.cpp b/test/IXUrlParserTest.cpp new file mode 100644 index 00000000..444957b2 --- /dev/null +++ b/test/IXUrlParserTest.cpp @@ -0,0 +1,113 @@ +/* + * IXSocketTest.cpp + * Author: Korchynskyi Dmytro + * Copyright (c) 2019 Machine Zone. All rights reserved. + */ + +#include +#include + +#include "IXTest.h" +#include "catch.hpp" +#include + +using namespace ix; + +namespace ix +{ + +TEST_CASE("urlParser", "[urlParser]") +{ + SECTION("http://google.com") + { + std::string url = "http://google.com"; + std::string protocol, host, path, query; + int port; + bool websocket = false; + bool res; + + res = UrlParser::parse(url, protocol, host, path, query, port, websocket); + + REQUIRE(res); + REQUIRE(protocol == "http"); + REQUIRE(host == "google.com"); + REQUIRE(path == "/"); + REQUIRE(query == ""); + REQUIRE(port == 80); // default port for http + } + + SECTION("https://google.com") + { + std::string url = "https://google.com"; + std::string protocol, host, path, query; + int port; + bool websocket = false; + bool res; + + res = UrlParser::parse(url, protocol, host, path, query, port, websocket); + + REQUIRE(res); + REQUIRE(protocol == "https"); + REQUIRE(host == "google.com"); + REQUIRE(path == "/"); + REQUIRE(query == ""); + REQUIRE(port == 443); // default port for https + } + + SECTION("ws://google.com") + { + std::string url = "ws://google.com"; + std::string protocol, host, path, query; + int port; + bool websocket = true; + bool res; + + res = UrlParser::parse(url, protocol, host, path, query, port, websocket); + + REQUIRE(res); + REQUIRE(protocol == "ws"); + REQUIRE(host == "google.com"); + REQUIRE(path == "/"); + REQUIRE(query == ""); + REQUIRE(port == 80); // default port for ws + } + + SECTION("wss://google.com/ws?arg=value") + { + std::string url = "wss://google.com/ws?arg=value&arg2=value2"; + std::string protocol, host, path, query; + int port; + bool websocket = true; + bool res; + + res = UrlParser::parse(url, protocol, host, path, query, port, websocket); + + REQUIRE(res); + REQUIRE(protocol == "wss"); + REQUIRE(host == "google.com"); + REQUIRE(path == "/ws?arg=value&arg2=value2"); + REQUIRE(query == "arg=value&arg2=value2"); + REQUIRE(port == 443); // default port for wss + } + + SECTION("real test") + { + std::string url = "ws://127.0.0.1:7350/ws?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1NTcxNzAwNzIsInVpZCI6ImMwZmZjOGE1LTk4OTktNDAwYi1hNGU5LTJjNWM3NjFmNWQxZiIsInVzbiI6InN2YmhOdlNJSmEifQ.5L8BUbpTA4XAHlSrdwhIVlrlIpRtjExepim7Yh5eEO4&status=true&format=protobuf"; + std::string protocol, host, path, query; + int port; + bool websocket = true; + bool res; + + res = UrlParser::parse(url, protocol, host, path, query, port, websocket); + + REQUIRE(res); + REQUIRE(protocol == "ws"); + REQUIRE(host == "127.0.0.1"); + REQUIRE(path == "/ws?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1NTcxNzAwNzIsInVpZCI6ImMwZmZjOGE1LTk4OTktNDAwYi1hNGU5LTJjNWM3NjFmNWQxZiIsInVzbiI6InN2YmhOdlNJSmEifQ.5L8BUbpTA4XAHlSrdwhIVlrlIpRtjExepim7Yh5eEO4&status=true&format=protobuf"); + REQUIRE(query == "token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1NTcxNzAwNzIsInVpZCI6ImMwZmZjOGE1LTk4OTktNDAwYi1hNGU5LTJjNWM3NjFmNWQxZiIsInVzbiI6InN2YmhOdlNJSmEifQ.5L8BUbpTA4XAHlSrdwhIVlrlIpRtjExepim7Yh5eEO4&status=true&format=protobuf"); + REQUIRE(port == 7350); + } + +} + +}