NAWA 0.9
Web Application Framework for C++
utils.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2019-2022 Tobias Flaig.
3 *
4 * This file is part of nawa.
5 *
6 * nawa is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License,
8 * version 3, as published by the Free Software Foundation.
9 *
10 * nawa is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with nawa. If not, see <https://www.gnu.org/licenses/>.
17 */
18
24#include "nawa/Exception.h"
25#include <catch2/catch.hpp>
26#include <nawa/util/utils.h>
27
28using namespace nawa;
29using namespace std;
30
31TEST_CASE("nawa::utils functions", "[unit][utils]") {
32
33 SECTION("Time conversions") {
34 time_t currentTime = time(nullptr);
35 time_t smtpTime1;
36 CHECK_NOTHROW(smtpTime1 = utils::readSmtpTime("Thu, 7 Nov 2019 16:29:50 +0100"));
37 CHECK(smtpTime1 == 1573140590);
38 time_t httpTime1;
39 CHECK_NOTHROW(httpTime1 = utils::readHttpTime(utils::makeHttpTime(currentTime)));
40 CHECK(httpTime1 == currentTime);
41 time_t smtpTime2;
42 CHECK_NOTHROW(smtpTime2 = utils::readSmtpTime(utils::makeSmtpTime(currentTime)));
43 CHECK(smtpTime2 == currentTime);
44 CHECK_THROWS_AS(utils::readSmtpTime("test"), Exception);
45 CHECK_THROWS_AS(utils::readHttpTime("test"), Exception);
46 }
47
48 SECTION("Path splitting") {
49 string t1 = "p1/p2/p3";
50 string t2 = "/p1/p2/p3";
51 string t3 = "/p1/p2/p3/";
52 string t4 = "/p1/p2/p3?test=/xyz";
53 string t5 = "/p1/p2/p3/?test=/xyz/";
54 auto t1_split = utils::splitPath(t1);
55 CHECK(t1_split == utils::splitPath(t2));
56 CHECK(t1_split == utils::splitPath(t3));
57 CHECK(t1_split == utils::splitPath(t4));
58 CHECK(t1_split == utils::splitPath(t5));
59 }
60}
Exception class that can be used by apps to catch errors resulting from nawa function calls.
std::vector< std::string > splitPath(std::string const &pathString)
Definition: utils.cpp:476
std::string makeHttpTime(time_t time)
Definition: utils.cpp:359
time_t readSmtpTime(std::string const &smtpTime)
Definition: utils.cpp:405
time_t readHttpTime(std::string const &httpTime)
Definition: utils.cpp:373
std::string makeSmtpTime(time_t time)
Definition: utils.cpp:392
Definition: AppInit.h:31
TEST_CASE("nawa::utils functions", "[unit][utils]")
Definition: utils.cpp:31
Contains useful functions that improve the readability and facilitate maintenance of the NAWA code.