Skip to content

Commit 81caa4e

Browse files
committed
Began adding tests to the project with Catch2
Added a test build option to the CMake project with basic message tests to start.
1 parent d4f711d commit 81caa4e

File tree

7 files changed

+14460
-26
lines changed

7 files changed

+14460
-26
lines changed

CMakeLists.txt

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
1-
cmake_minimum_required(VERSION 3.0)
1+
cmake_minimum_required (VERSION 3.0)
2+
project (atomizes)
23

3-
project(atomizes)
4+
set (CMAKE_CXX_STANDARD 11)
5+
set (ATOMIZES_TEST_SOURCE ${PROJECT_SOURCE_DIR}/tests/main.cpp ${PROJECT_SOURCE_DIR}/tests/message_tests.cpp ${PROJECT_SOURCE_DIR}/tests/parser_tests.cpp)
46

5-
add_library(atomizes INTERFACE)
7+
add_library (atomizes INTERFACE)
68

7-
target_include_directories(atomizes INTERFACE include/)
9+
target_include_directories (atomizes INTERFACE include/)
10+
11+
option (ATOMIZES_ENABLE_TESTS "Build tests for atomizes?" ON)
12+
13+
if (ATOMIZES_ENABLE_TESTS)
14+
enable_testing ()
15+
add_executable (atomizes_test ${ATOMIZES_TEST_SOURCE})
16+
target_link_libraries (atomizes_test atomizes)
17+
add_test (NAME AtomizesTests COMMAND atomizes_test)
18+
endif ()

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ This simple message would result in the following output:
5151

5252
```
5353
HTTP/1.1 200 OK
54-
Content-Type: text/plain
5554
Connection: close
55+
Content-Type: text/plain
5656
Content-Length: 12
5757
5858
Hello world!
@@ -71,13 +71,13 @@ To define a simple request with an `HTTPMessage` and print it to standard out, y
7171

7272
int main()
7373
{
74-
atomizes::HTTPMessage request;
74+
atomizes::HTTPMessage response;
7575

7676
response.SetMethod(atomizes::MessageMethod::GET)
7777
.SetPath("/")
78-
.SetHeader("Host", "example.com")
7978
.SetHeader("User-Agent", "Test Agent")
80-
.SetHeader("Connection", "keep-alive");
79+
.SetHeader("Connection", "keep-alive")
80+
.SetHeader("Host", "example.com");
8181

8282
std::cout << response.ToString() << std::endl;
8383

include/atomizes.hpp

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,15 @@ namespace atomizes
5656
*/
5757
enum class MessageMethod : uint8_t
5858
{
59-
NONE = 0x00,
60-
GET = 0x01,
61-
HEAD = 0x02,
62-
POST = 0x03,
63-
PUT = 0x04,
64-
DELETE = 0x05,
65-
CONNECT = 0x06,
66-
TRACE = 0x07,
67-
PATCH = 0x08
59+
NONE,
60+
GET,
61+
HEAD,
62+
POST,
63+
PUT,
64+
DELETE,
65+
CONNECT,
66+
TRACE,
67+
PATCH
6868
};
6969

7070
/**
@@ -286,10 +286,17 @@ namespace atomizes
286286

287287
/**
288288
* Get the string value of a single header from the message.
289+
*
290+
* Will return an empty string if the header does not exist.
289291
*/
290292
inline std::string GetHeader(const std::string& name) const
291293
{
292-
return m_headers.at(name);
294+
auto find = m_headers.find(name);
295+
296+
if (find != m_headers.end())
297+
return find->second;
298+
299+
return "";
293300
}
294301

295302
/**
@@ -427,7 +434,7 @@ namespace atomizes
427434
// automatically output the content length based on
428435
// the size of the body member if body isn't empty
429436
if (!m_body.empty())
430-
output << "Content-Length: " << m_body.size() << std::endl;
437+
output << "Content-Length: " << m_body.size() << CarriageReturn;
431438

432439
// seperate headers and body with an extra carriage return
433440
output << CarriageReturn;
@@ -528,13 +535,13 @@ namespace atomizes
528535
*/
529536
enum class MessageParserState : uint8_t
530537
{
531-
NONE = 0x00,
532-
PARSING_START_LINE = 0x01,
533-
START_LINE_REQUEST = 0x02,
534-
START_LINE_RESPONSE = 0x03,
535-
HEADER_KEY = 0x04,
536-
HEADER_VALUE = 0x05,
537-
PARSING_BODY = 0x06,
538+
NONE,
539+
PARSING_START_LINE,
540+
START_LINE_REQUEST,
541+
START_LINE_RESPONSE,
542+
HEADER_KEY,
543+
HEADER_VALUE,
544+
PARSING_BODY,
538545
};
539546

540547
/**

0 commit comments

Comments
 (0)