Skip to content

Commit 1f4ad37

Browse files
http: Integrated cookie, made it a part in http
1 parent 72d2e81 commit 1f4ad37

File tree

5 files changed

+35
-46
lines changed

5 files changed

+35
-46
lines changed

api/net/http/cookie.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
// See the License for the specific language governing permissions and
1616
// limitations under the License.
1717

18-
#ifndef COOKIE_HPP
19-
#define COOKIE_HPP
18+
#ifndef HTTP_COOKIE_HPP
19+
#define HTTP_COOKIE_HPP
2020

2121
#include <regex>
2222
#include <string>
@@ -29,7 +29,7 @@
2929
#include <functional>
3030
#include <exception>
3131

32-
namespace cookie {
32+
namespace http {
3333

3434
class CookieException : public std::exception {
3535
std::string msg;
@@ -225,6 +225,6 @@ bool operator == (const Cookie& a, const Cookie& b) noexcept;
225225

226226
std::ostream& operator << (std::ostream& output_device, const Cookie& cookie);
227227

228-
}; // < namespace cookie
228+
}; // < namespace http
229229

230-
#endif
230+
#endif // < HTTP_COOKIE_HPP

src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ set(OS_OBJECTS
4444
net/super_stack.cpp
4545
net/http/header.cpp net/http/header_fields.cpp net/http/message.cpp net/http/request.cpp
4646
net/http/response.cpp net/http/status_codes.cpp net/http/time.cpp net/http/version.cpp
47-
net/http/client.cpp net/http/connection.cpp
47+
net/http/client.cpp net/http/connection.cpp net/http/cookie.cpp
4848
fs/disk.cpp fs/filesystem.cpp fs/mbr.cpp fs/path.cpp
4949
fs/fat.cpp fs/fat_async.cpp fs/fat_sync.cpp fs/memdisk.cpp
5050
posix/fd.cpp posix/tcp_fd.cpp posix/udp_fd.cpp posix/unistd.cpp posix/fcntl.cpp posix/syslog.cpp

src/net/http/cookie.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
// See the License for the specific language governing permissions and
1616
// limitations under the License.
1717

18-
#include "cookie.hpp"
18+
#include <net/http/cookie.hpp>
1919

20-
namespace cookie {
20+
namespace http {
2121

2222
bool operator < (const Cookie& a, const Cookie& b) noexcept {
2323
return std::hash<std::string>{}(a.get_name() + a.get_value())
@@ -245,4 +245,4 @@ std::string Cookie::to_string() const {
245245
return cookie_string;
246246
}
247247

248-
}; // < namespace cookie
248+
}; // < namespace http

test/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ set(TEST_SOURCES
3939
${TEST}/util/unit/path_to_regex_parse.cpp
4040
${TEST}/util/unit/path_to_regex_options.cpp
4141
${TEST}/util/unit/path_to_regex_no_options.cpp
42+
${TEST}/net/unit/cookie_test.cpp
4243
)
4344

4445
set(OS_SOURCES
@@ -67,6 +68,7 @@ set(OS_SOURCES
6768
${SRC}/util/uri.cpp
6869
${SRC}/util/percent_encoding.cpp
6970
${SRC}/util/path_to_regex.cpp
71+
${SRC}/net/http/cookie.cpp
7072
)
7173

7274
add_executable(unittests ${OS_SOURCES} ${TEST_SOURCES} )

test/net/unit/cookie_test.cpp

Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,19 @@
1515
// See the License for the specific language governing permissions and
1616
// limitations under the License.
1717

18-
#include "../cookie.hpp"
19-
#include <lest/lest.hpp>
18+
#include <common.cxx>
19+
#include <net/http/cookie.hpp>
2020

2121
using namespace std;
22-
using namespace cookie;
22+
using namespace http;
2323

24-
const lest::test test_cookie_creation[] =
25-
{
2624
// --------------- Testing Cookie name ------------------
2725

2826
CASE("CookieException thrown when creating Cookie with no name")
2927
{
3028
EXPECT_THROWS_AS( (Cookie{"", "value"}), CookieException );
3129
EXPECT_THROWS( (Cookie{"", "value"}) );
32-
},
30+
}
3331

3432
CASE("CookieException thrown when creating Cookie with name containing invalid character")
3533
{
@@ -54,7 +52,7 @@ const lest::test test_cookie_creation[] =
5452
EXPECT_THROWS( (Cookie{"n{ame", "value"}) );
5553
EXPECT_THROWS( (Cookie{"name}", "value"}) );
5654
EXPECT_THROWS( (Cookie{":nam[]{e", "value"}) );
57-
},
55+
}
5856

5957
CASE("No CookieException thrown when creating Cookie with valid name")
6058
{
@@ -74,14 +72,14 @@ const lest::test test_cookie_creation[] =
7472
* SP = <US-ASCII SP, space (32)>
7573
* HT = <US-ASCII HT, horizontal-tab (9)>
7674
*/
77-
},
75+
}
7876

7977
// --------------- Testing Cookie value ------------------
8078

8179
CASE("No CookieException thrown when creating Cookie with no value")
8280
{
8381
EXPECT_NO_THROW( (Cookie{"name", ""}) );
84-
},
82+
}
8583

8684
CASE("CookieException thrown when creating Cookie with value containing invalid character")
8785
{
@@ -106,14 +104,14 @@ const lest::test test_cookie_creation[] =
106104
EXPECT_THROWS( (Cookie{"name", "va{lue"}) );
107105
EXPECT_THROWS( (Cookie{"name", "value}"}) );
108106
EXPECT_THROWS( (Cookie{"name", "v:a[]l{ue"}) );
109-
},
107+
}
110108

111109
CASE("No CookieException thrown when creating Cookie with valid value")
112110
{
113111
EXPECT_NO_THROW( (Cookie{"name", "value"}) );
114112
EXPECT_NO_THROW( (Cookie{"name", "1928sdfg'"}) );
115113
EXPECT_NO_THROW( (Cookie{"name", "&INAann'dp21"}) );
116-
},
114+
}
117115

118116
// --------------- Testing Cookie options ------------------
119117

@@ -124,20 +122,20 @@ const lest::test test_cookie_creation[] =
124122
CASE("CookieException thrown when creating Cookie with empty option name")
125123
{
126124
EXPECT_THROWS( (Cookie{"name", "value", {"", "Sun, 06 Nov 1994 08:49:37 GMT"}}) );
127-
},
125+
}
128126

129127
CASE("CookieException thrown when creating Cookie with invalid option name")
130128
{
131129
EXPECT_THROWS( (Cookie{"name", "value", {"something", "something"}}) );
132130
EXPECT_THROWS( (Cookie{"name", "value", {"expires_", "Sun, 06 Nov 1994 08:49:37 GMT"}}) );
133-
},
131+
}
134132

135133
CASE("CookieException thrown when creating Cookie with odd number of vector-elements")
136134
{
137135
EXPECT_THROWS( (Cookie{"name", "value", {"Expires", "Sun, 06 Nov 1994 08:49:37 GMT", "Path"}}) );
138136
EXPECT_THROWS( (Cookie{"name", "value", {"Expires"}}) );
139137
EXPECT_THROWS( (Cookie{"name", "value", {"Expires", "Sun, 06 Nov 1994 08:49:37 GMT", "Path", "/something123", "Domain"}}) );
140-
},
138+
}
141139

142140
CASE("No CookieException thrown when creating Cookie with multiple valid options")
143141
{
@@ -149,11 +147,11 @@ const lest::test test_cookie_creation[] =
149147
"domain", "example.com", "secure", "true", "httponly", "true"}}) );
150148
EXPECT_NO_THROW( (Cookie{"name", "value", {"Expires", "Sun, 06 Nov 1994 08:49:37 GMT", "Path", "/anotherPath134",
151149
"secure", "true", "domain", "example.com"}}) );
152-
},
150+
}
153151

154152
// Scenario when creating cookie. Check the set values.
155153

156-
SCENARIO("Creating cookies with valid values and changing these values")
154+
CASE("Creating cookies with valid values and changing these values")
157155
{
158156
GIVEN("An option vector with all possible options sent to Cookie constructor")
159157
{
@@ -277,7 +275,7 @@ const lest::test test_cookie_creation[] =
277275
}
278276
}
279277
}
280-
},
278+
}
281279

282280
// Option: Expires (string GMT datetime)
283281

@@ -312,7 +310,7 @@ const lest::test test_cookie_creation[] =
312310
EXPECT_NO_THROW( (Cookie{"name", "value", {"expires", "Thu Feb 22 19:17:11 2013"}}) );
313311
EXPECT_NO_THROW( (Cookie{"name", "value", {"Expires", "fri Mar 1 13:00:06 2023"}}) );
314312
EXPECT_NO_THROW( (Cookie{"name", "value", {"expiRes", "saT Apr 9 00:09:44 2016"}}) );
315-
},
313+
}
316314

317315
CASE("CookieException thrown when creating Cookie with invalid Expires option")
318316
{
@@ -329,7 +327,7 @@ const lest::test test_cookie_creation[] =
329327
EXPECT_THROWS( (Cookie{"name", "value", {"Expires", "abc"}}) );
330328
EXPECT_THROWS( (Cookie{"name", "value", {"Expires", "saT, Apr 16 00:09:44 GMT"}}) );
331329
EXPECT_THROWS( (Cookie{"name", "value", {"Expires", "Sun Nov 6 08:49:37"}}) );
332-
},
330+
}
333331

334332
// Option: Max-Age (int)
335333

@@ -339,15 +337,15 @@ const lest::test test_cookie_creation[] =
339337
EXPECT_NO_THROW( (Cookie{"name", "value", {"Max-AgE", "0"}}) );
340338
EXPECT_NO_THROW( (Cookie{"name", "value", {"MAX-Age", "100000"}}) );
341339
EXPECT_NO_THROW( (Cookie{"name", "value", {"max-age", "5011010"}}) );
342-
},
340+
}
343341

344342
CASE("CookieException thrown when creating Cookie with invalid Max-Age option")
345343
{
346344
EXPECT_THROWS( (Cookie{"name", "value", {"Max-Age", "-1"}}) );
347345
EXPECT_THROWS( (Cookie{"name", "value", {"Max-Age", "ab"}}) );
348346
EXPECT_THROWS( (Cookie{"name", "value", {"Max-Age", ";12"}}) );
349347
EXPECT_THROWS( (Cookie{"name", "value", {"Max-Age", "9999999999"}}) );
350-
},
348+
}
351349

352350
// Option: Domain (string)
353351

@@ -358,7 +356,7 @@ const lest::test test_cookie_creation[] =
358356
EXPECT_NO_THROW( (Cookie{"name", "value", {"DOMAIN", "service.example.com"}}) );
359357
EXPECT_NO_THROW( (Cookie{"name", "value", {"Domain", "whateverdomainyouwant123.thebrowserremovesitifnotyourdomain"}}) );
360358
EXPECT_NO_THROW( (Cookie{"name", "value", {"Domain", ""}}) );
361-
},
359+
}
362360

363361
/* All characters are allowed in the domain option, but the browser will ignore it if the domain you enter isn't yours
364362
CASE("CookieException thrown when creating Cookie with invalid Domain option") {},
@@ -373,7 +371,7 @@ const lest::test test_cookie_creation[] =
373371
EXPECT_NO_THROW( (Cookie{"name", "value", {"PATH", "/ex?mypath even with whitespaces"}}) );
374372
EXPECT_NO_THROW( (Cookie{"name", "value", {"Path", "/whatever/path/you/want/:&something"}}) );
375373
EXPECT_NO_THROW( (Cookie{"name", "value", {"Path", ""}}) );
376-
},
374+
}
377375

378376
CASE("Path is set")
379377
{
@@ -388,7 +386,7 @@ const lest::test test_cookie_creation[] =
388386
EXPECT( c3.get_path() == "/ex?mypath even with whitespaces" );
389387
EXPECT( c4.get_path() == "/whatever/path/you/want/:&something" );
390388
EXPECT( c5.get_path() == "/" );
391-
},
389+
}
392390

393391
CASE("CookieException thrown when creating Cookie with invalid Path option")
394392
{
@@ -397,7 +395,7 @@ const lest::test test_cookie_creation[] =
397395
EXPECT_THROWS( (Cookie{"name", "value", {"Path", "/12\rmyPath"}}) );
398396
EXPECT_THROWS( (Cookie{"name", "value", {"Path", "/12\nmyPath"}}) );
399397
EXPECT_THROWS( (Cookie{"name", "value", {"Path", "/12\tmyPath"}}) );
400-
},
398+
}
401399

402400
// Option: Secure (bool)
403401

@@ -414,7 +412,7 @@ const lest::test test_cookie_creation[] =
414412
EXPECT( c3.is_secure() );
415413
EXPECT_NOT( c4.is_secure() );
416414
EXPECT_NOT( c5.is_secure() );
417-
},
415+
}
418416

419417
// Option: HttpOnly (bool)
420418

@@ -432,15 +430,4 @@ const lest::test test_cookie_creation[] =
432430
EXPECT_NOT( c4.is_http_only() );
433431
EXPECT_NOT( c5.is_http_only() );
434432
}
435-
};
436-
437-
int main(int argc, char * argv[])
438-
{
439-
printf("Running tests of cookie-creation...\n");
440-
441-
int res = lest::run(test_cookie_creation, argc, argv);
442-
443-
printf("Tests completed.\n");
444433

445-
return res;
446-
}

0 commit comments

Comments
 (0)