File: http_message_tests.cpp

package info (click to toggle)
pion 5.0.5%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 8,852 kB
  • ctags: 17,413
  • sloc: cpp: 13,965; sh: 10,263; perl: 269; makefile: 207; pascal: 152
file content (623 lines) | stat: -rw-r--r-- 20,474 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
// ---------------------------------------------------------------------
// pion:  a Boost C++ framework for building lightweight HTTP interfaces
// ---------------------------------------------------------------------
// Copyright (C) 2007-2012 Cloudmeter, Inc.  (http://www.cloudmeter.com)
//
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
//

#include <sstream>
#include <vector>
#include <algorithm>
#include <pion/config.hpp>
#include <pion/http/message.hpp>
#include <pion/http/request.hpp>
#include <pion/http/response.hpp>
#include <pion/test/unit_test.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/mpl/list.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/foreach.hpp>

using namespace pion;


BOOST_AUTO_TEST_CASE(checkHTTPRequestCopyConstructor) {
    http::request req1;
    req1.add_header("Test", "HTTPMessage");
    req1.set_method("GET");
    http::request req2(req1);
    BOOST_CHECK_EQUAL(req1.get_method(), "GET");
    BOOST_CHECK_EQUAL(req1.get_method(), req2.get_method());
    BOOST_CHECK_EQUAL(req1.get_header("Test"), "HTTPMessage");
    BOOST_CHECK_EQUAL(req1.get_header("Test"), req2.get_header("Test"));
}

BOOST_AUTO_TEST_CASE(checkHTTPRequestAssignmentOperator) {
    http::request req1, req2;
    req1.set_method("GET");
    req1.add_header("Test", "HTTPMessage");
    req2 = req1;
    BOOST_CHECK_EQUAL(req1.get_method(), "GET");
    BOOST_CHECK_EQUAL(req1.get_method(), req2.get_method());
    BOOST_CHECK_EQUAL(req1.get_header("Test"), "HTTPMessage");
    BOOST_CHECK_EQUAL(req1.get_header("Test"), req2.get_header("Test"));
}

BOOST_AUTO_TEST_CASE(checkHTTPResponseCopyConstructor) {
    http::response rsp1;
    rsp1.add_header("Test", "HTTPMessage");
    rsp1.set_status_code(199);
    http::response rsp2(rsp1);
    BOOST_CHECK_EQUAL(rsp1.get_status_code(), 199U);
    BOOST_CHECK_EQUAL(rsp1.get_status_code(), rsp2.get_status_code());
    BOOST_CHECK_EQUAL(rsp1.get_header("Test"), "HTTPMessage");
    BOOST_CHECK_EQUAL(rsp1.get_header("Test"), rsp2.get_header("Test"));
}

BOOST_AUTO_TEST_CASE(checkHTTPResponseAssignmentOperator) {
    http::response rsp1, rsp2;
    rsp1.add_header("Test", "HTTPMessage");
    rsp1.set_status_code(199);
    rsp2 = rsp1;
    BOOST_CHECK_EQUAL(rsp1.get_status_code(), 199U);
    BOOST_CHECK_EQUAL(rsp1.get_status_code(), rsp2.get_status_code());
    BOOST_CHECK_EQUAL(rsp1.get_header("Test"), "HTTPMessage");
    BOOST_CHECK_EQUAL(rsp1.get_header("Test"), rsp2.get_header("Test"));
}

BOOST_AUTO_TEST_CASE(checkGetFirstLineForRequest) {
    http::request http_request;
    
    http_request.set_method("GET");
    http_request.set_resource("/");

    BOOST_CHECK_EQUAL(http_request.get_first_line(), "GET / HTTP/1.1");
    
    http_request.set_method("POST");

    BOOST_CHECK_EQUAL(http_request.get_first_line(), "POST / HTTP/1.1");

    http_request.set_resource("/index.html");

    BOOST_CHECK_EQUAL(http_request.get_first_line(), "POST /index.html HTTP/1.1");

    http_request.set_version_major(1);
    http_request.set_version_minor(0);

    BOOST_CHECK_EQUAL(http_request.get_first_line(), "POST /index.html HTTP/1.0");
}

BOOST_AUTO_TEST_CASE(checkGetFirstLineForResponse) {
    http::response http_response;
    
    http_response.set_status_code(http::types::RESPONSE_CODE_OK);
    http_response.set_status_message(http::types::RESPONSE_MESSAGE_OK);

    BOOST_CHECK_EQUAL(http_response.get_first_line(), "HTTP/1.1 200 OK");
    
    http_response.set_status_code(http::types::RESPONSE_CODE_NOT_FOUND);

    BOOST_CHECK_EQUAL(http_response.get_first_line(), "HTTP/1.1 404 OK");

    http_response.set_status_message(http::types::RESPONSE_MESSAGE_NOT_FOUND);

    BOOST_CHECK_EQUAL(http_response.get_first_line(), "HTTP/1.1 404 Not Found");
}


#define FIXTURE_TYPE_LIST(F) boost::mpl::list<F<http::request>, F<http::response> >

template<typename ConcreteMessageType>
class NewHTTPMessage_F : public ConcreteMessageType {
public:
    NewHTTPMessage_F() {
    }
    ~NewHTTPMessage_F() {
    }
};

BOOST_AUTO_TEST_SUITE_FIXTURE_TEMPLATE(NewHTTPMessage_S,
                                       FIXTURE_TYPE_LIST(NewHTTPMessage_F))

BOOST_AUTO_TEST_CASE_FIXTURE_TEMPLATE(checkGetContentLengthReturnsZero) {
    BOOST_CHECK_EQUAL(F::get_content_length(), static_cast<size_t>(0));
}

BOOST_AUTO_TEST_CASE_FIXTURE_TEMPLATE(checkSetContentLengthDoesntThrow) {
    BOOST_CHECK_NO_THROW(F::set_content_length(10));
    BOOST_CHECK_NO_THROW(F::set_content_length(0));
}

// Is this what we want?  Note that the length is zero here.
BOOST_AUTO_TEST_CASE_FIXTURE_TEMPLATE(checkCreateContentBufferReturnsPointer) {
    BOOST_CHECK(F::create_content_buffer() != NULL);
}

BOOST_AUTO_TEST_CASE_FIXTURE_TEMPLATE(checkGetContentReturnsEmptyString) {
    BOOST_CHECK(F::get_content() != NULL);
    BOOST_CHECK_EQUAL(strcmp(F::get_content(), ""), 0);
    BOOST_CHECK(!F::is_content_buffer_allocated());
}

// Is this what we want?  Note that the length is zero here.
BOOST_AUTO_TEST_CASE_FIXTURE_TEMPLATE(checkGetContentReturnsPointerAfterCreatingContentBuffer) {
    F::create_content_buffer();
    BOOST_CHECK(F::get_content() != NULL);
}

BOOST_AUTO_TEST_CASE_FIXTURE_TEMPLATE(checkIsValidReturnsFalse) {
    BOOST_CHECK(!F::is_valid());
}

BOOST_AUTO_TEST_CASE_FIXTURE_TEMPLATE(testChunksSupportedAccessors) {
    BOOST_CHECK(!F::get_chunks_supported());
    F::set_chunks_supported(true);
    BOOST_CHECK(F::get_chunks_supported());
    F::set_chunks_supported(false);
    BOOST_CHECK(!F::get_chunks_supported());
}

BOOST_AUTO_TEST_CASE_FIXTURE_TEMPLATE(testHeaderCaseIsIgnored) {
    const std::string xml_content_type("text/xml");

    F::add_header(http::types::HEADER_CONTENT_TYPE, xml_content_type);
    BOOST_CHECK_EQUAL(F::get_header("CoNTenT-TYPe"), xml_content_type);

    F::add_header("content-length", "10");
    BOOST_CHECK_EQUAL(F::get_header(http::types::HEADER_CONTENT_LENGTH), "10");
}

BOOST_AUTO_TEST_SUITE_END()

template<typename ConcreteMessageType>
class HTTPMessageWithContentLengthSet_F : public ConcreteMessageType {
public:
    HTTPMessageWithContentLengthSet_F() {
        this->set_content_length(20);
    }
    ~HTTPMessageWithContentLengthSet_F() {
    }
};

BOOST_AUTO_TEST_SUITE_FIXTURE_TEMPLATE(HTTPMessageWithContentLengthSet_S,
                                       FIXTURE_TYPE_LIST(HTTPMessageWithContentLengthSet_F))

BOOST_AUTO_TEST_CASE_FIXTURE_TEMPLATE(checkGetContentLengthReturnsCorrectLength) {
    BOOST_CHECK_EQUAL(F::get_content_length(), static_cast<size_t>(20));
}

BOOST_AUTO_TEST_CASE_FIXTURE_TEMPLATE(checkGetContentLengthAfterSettingLengthAgain) {
    F::set_content_length(30);
    BOOST_CHECK_EQUAL(F::get_content_length(), static_cast<size_t>(30));
    F::set_content_length(0);
    BOOST_CHECK_EQUAL(F::get_content_length(), static_cast<size_t>(0));
}

BOOST_AUTO_TEST_CASE_FIXTURE_TEMPLATE(checkCreateContentBufferVarious) {
    char *ptr = F::create_content_buffer();
    BOOST_CHECK(ptr != NULL);
    BOOST_CHECK_EQUAL(ptr, F::get_content());
    BOOST_CHECK_EQUAL(F::get_content_buffer_size(), 20U);
    BOOST_CHECK(F::is_content_buffer_allocated());
}

BOOST_AUTO_TEST_CASE_FIXTURE_TEMPLATE(checkGetContentReturnsEmptyString) {
    BOOST_CHECK(F::get_content() != NULL);
    BOOST_CHECK_EQUAL(strcmp(F::get_content(), ""), 0);
}

BOOST_AUTO_TEST_CASE_FIXTURE_TEMPLATE(checkIsValidReturnsFalse) {
    BOOST_CHECK(!F::is_valid());
}

BOOST_AUTO_TEST_SUITE_END()

template<typename ConcreteMessageType>
class HTTPMessageWithContentBufferCreated_F : public ConcreteMessageType {
public:
    HTTPMessageWithContentBufferCreated_F() {
        m_len = 10;
        this->set_content_length(m_len);
        m_content_buffer = this->create_content_buffer();
    }
    ~HTTPMessageWithContentBufferCreated_F() {
    }

    int m_len;
    char* m_content_buffer;
};

BOOST_AUTO_TEST_SUITE_FIXTURE_TEMPLATE(HTTPMessageWithContentBufferCreated_S,
                                       FIXTURE_TYPE_LIST(HTTPMessageWithContentBufferCreated_F))

// ???
BOOST_AUTO_TEST_CASE_FIXTURE_TEMPLATE(checkCreateContentBufferAgainReturnsPointer) {
    BOOST_CHECK(F::create_content_buffer() != NULL);
}

BOOST_AUTO_TEST_CASE_FIXTURE_TEMPLATE(checkGetContentReturnsPointer) {
    BOOST_CHECK(F::get_content() != NULL);
}

BOOST_AUTO_TEST_CASE_FIXTURE_TEMPLATE(checkGetContentReturnsWhatWasWrittenToBuffer) {
    char buf[] = {0, 1, 2, 3, 127, 0, -1, -2, -3, -128};
    BOOST_CHECK_EQUAL(sizeof(buf), static_cast<unsigned int>(F::m_len));
    memcpy(F::m_content_buffer, buf, F::m_len);
    BOOST_CHECK(memcmp(buf, F::get_content(), F::m_len) == 0);
}

BOOST_AUTO_TEST_SUITE_END()

static const char TEXT_STRING_1[] = "0123456789";
static const char TEXT_STRING_2[] = "9876543210";
static const char TEXT_STRING_3[] = "0123456789abcde";

template<typename ConcreteMessageType>
class HTTPMessageWithTextOnlyContent_F : public ConcreteMessageType {
public:
    HTTPMessageWithTextOnlyContent_F() {
        m_len = strlen(TEXT_STRING_1);
        this->set_content_length(m_len);
        m_content_buffer = this->create_content_buffer();
        memcpy(m_content_buffer, TEXT_STRING_1, m_len);
    }
    ~HTTPMessageWithTextOnlyContent_F() {
    }

    size_t m_len;
    char* m_content_buffer;
};

BOOST_AUTO_TEST_SUITE_FIXTURE_TEMPLATE(HTTPMessageWithTextOnlyContent_S, 
                                       FIXTURE_TYPE_LIST(HTTPMessageWithTextOnlyContent_F))

BOOST_AUTO_TEST_CASE_FIXTURE_TEMPLATE(checkGetContentReturnsPointer) {
    BOOST_CHECK(F::get_content() != NULL);
}

BOOST_AUTO_TEST_CASE_FIXTURE_TEMPLATE(checkGetContentReturnsWhatWasWrittenToBuffer) {
    BOOST_CHECK(memcmp(TEXT_STRING_1, F::get_content(), F::m_len) == 0);
}

BOOST_AUTO_TEST_CASE_FIXTURE_TEMPLATE(checkGetContentAfterChangingContent) {
    BOOST_CHECK_EQUAL(strlen(TEXT_STRING_2), static_cast<unsigned int>(F::m_len));
    memcpy(F::m_content_buffer, TEXT_STRING_2, F::m_len);
    BOOST_CHECK(memcmp(TEXT_STRING_2, F::get_content(), F::m_len) == 0);
}

BOOST_AUTO_TEST_CASE_FIXTURE_TEMPLATE(checkGetContentAfterChangingSizeAndContent) {
    F::m_len = strlen(TEXT_STRING_3);
    F::set_content_length(F::m_len);
    F::m_content_buffer = F::create_content_buffer();
    memcpy(F::m_content_buffer, TEXT_STRING_3, F::m_len);
    BOOST_CHECK(memcmp(TEXT_STRING_3, F::get_content(), F::m_len) == 0);
}

// This is just for convenience for text-only post content.
// Strictly speaking, get_content() guarantees nothing beyond the buffer.
BOOST_AUTO_TEST_CASE_FIXTURE_TEMPLATE(checkGetContentReturnsZeroTerminatedBuffer) {
    // This crashes due to bug in Boost.Test if m_content_buffer[m_len] is negative, so use workaround.
    //BOOST_CHECK_EQUAL(m_content_buffer[m_len], static_cast<char>(0));

    // Illustration of bug:
    //char c1 = -2;
    //char c2 = -2;
    //BOOST_CHECK_EQUAL(c1, c2);

    BOOST_CHECK_EQUAL(static_cast<int>(F::m_content_buffer[F::m_len]), static_cast<int>(0));
}

// See comments for checkGetContentReturnsZeroTerminatedBuffer.
BOOST_AUTO_TEST_CASE_FIXTURE_TEMPLATE(checkContentPointerUsableAsString) {
    const std::string s1 = TEXT_STRING_1;
    std::string s2 = F::get_content();
    BOOST_CHECK_EQUAL(s1, s2);
}

BOOST_AUTO_TEST_SUITE_END()


/// simple fixture for testing read() and write() methods
class HTTPMessageReadWrite_F {
public:
    HTTPMessageReadWrite_F()
        : m_filename("output.tmp")
    {
        openNewFile();
    }
    
    ~HTTPMessageReadWrite_F() {
        m_file.close();
        boost::filesystem::remove(m_filename);
    }
    
    void openNewFile(void) {
        if (m_file.is_open()) {
            m_file.close();
            m_file.clear();
        }
        m_file.open(m_filename.c_str(), std::ios::in | std::ios::out | std::ios::trunc | std::ios::binary);
        BOOST_REQUIRE(m_file.is_open());
    }
    
    std::string getFileContents(void) {
        // if only it were this easy... unfortunately order of headers can vary
        //std::stringstream ss;
        //m_file.seekg(0);
        //ss << m_file.rdbuf();
        //return ss.str();

        char line[256];
        std::stringstream ss;
        std::vector<std::string> lines;
        bool is_first_line = true;

        m_file.clear();
        m_file.seekg(0);
        
        while (m_file.getline(line, 255)) {
            std::size_t len = strlen(line);
            if (len > 0 && line[len-1]=='\r')
                line[len-1] = '\0';
            if (is_first_line) {
                ss << line << "\r\n";
                lines.clear();
                is_first_line = false;
            } else if (line[0] == '\0') {
                std::sort(lines.begin(), lines.end());
                BOOST_FOREACH(const std::string& l, lines) {
                    ss << l << "\r\n";
                }
                ss << "\r\n";
                lines.clear();
                is_first_line = true;
            } else {
                lines.push_back(line);
            }
        }
        
        std::sort(lines.begin(), lines.end());
        BOOST_FOREACH(const std::string& l, lines) {
            ss << l << "\r\n";
        }

        return ss.str();
    }

    std::string     m_filename;
    std::fstream    m_file;
};

BOOST_FIXTURE_TEST_SUITE(HTTPMessageReadWrite_S, HTTPMessageReadWrite_F)

BOOST_AUTO_TEST_CASE(checkWriteReadHTTPRequestNoContent) {
    // build a request
    http::request req;
    req.set_resource("/test.html");
    req.add_header("Test", "Something");
    
    // write to file
    boost::system::error_code ec;
    req.write(m_file, ec);
    BOOST_REQUIRE(! ec);
    m_file.flush();
    
    // read from file
    http::request req2;
    m_file.clear();
    m_file.seekg(0);
    req2.read(m_file, ec);
    BOOST_REQUIRE(! ec);

    // make sure we're now at EOF
    http::request req3;
    req3.read(m_file, ec);
    BOOST_CHECK_EQUAL(ec.value(), boost::system::errc::io_error);
    
    // check request read from file
    BOOST_CHECK_EQUAL(req2.get_resource(), "/test.html");
    BOOST_CHECK_EQUAL(req2.get_header("Test"), "Something");
    BOOST_CHECK_EQUAL(req2.get_content_length(), 0U);

    // validate file contents
    std::string req_contents = getFileContents();
    BOOST_CHECK_EQUAL(req_contents, "GET /test.html HTTP/1.1\r\nConnection: Keep-Alive\r\nContent-Length: 0\r\nTest: Something\r\n\r\n");

    // create a new file for req2
    openNewFile();
    req2.write(m_file, ec);
    BOOST_REQUIRE(! ec);
    m_file.flush();

    // make sure file matches original (no loss/change from read/write cycle)
    std::string req2_contents = getFileContents();
    BOOST_CHECK_EQUAL(req_contents, req2_contents);
}

BOOST_AUTO_TEST_CASE(checkWriteReadHTTPResponseNoContent) {
    // build a response
    http::response rsp;
    rsp.set_status_code(202);
    rsp.set_status_message("Hi There");
    rsp.add_header("HeaderA", "a value");
    
    // write to file
    boost::system::error_code ec;
    rsp.write(m_file, ec);
    BOOST_REQUIRE(! ec);
    m_file.flush();
    
    // read from file
    http::response rsp2;
    m_file.clear();
    m_file.seekg(0);
    rsp2.read(m_file, ec);
    BOOST_REQUIRE(! ec);
    
    // make sure we're now at EOF
    http::response rsp3;
    rsp3.read(m_file, ec);
    BOOST_CHECK_EQUAL(ec.value(), boost::system::errc::io_error);
    
    // check response read from file
    BOOST_CHECK_EQUAL(rsp2.get_status_code(), 202U);
    BOOST_CHECK_EQUAL(rsp2.get_status_message(), "Hi There");
    BOOST_CHECK_EQUAL(rsp2.get_header("HeaderA"), "a value");
    BOOST_CHECK_EQUAL(rsp2.get_content_length(), 0U);

    // validate file contents
    std::string rsp_contents = getFileContents();
    BOOST_CHECK_EQUAL(rsp_contents, "HTTP/1.1 202 Hi There\r\nConnection: Keep-Alive\r\nContent-Length: 0\r\nHeaderA: a value\r\n\r\n");

    // create a new file for rsp2
    openNewFile();
    rsp2.write(m_file, ec);
    BOOST_REQUIRE(! ec);
    m_file.flush();

    // make sure file matches original (no loss/change from read/write cycle)
    std::string rsp2_contents = getFileContents();
    BOOST_CHECK_EQUAL(rsp_contents, rsp2_contents);
}

BOOST_AUTO_TEST_CASE(checkWriteReadMixedMessages) {
    boost::system::error_code ec;
    http::request req;
    http::response rsp;

    // build a request & write to file
    req.set_resource("/test.html");
    req.add_header("Test", "Something");
    req.write(m_file, ec);
    BOOST_REQUIRE(! ec);

    // build a response & write to file
    rsp.set_status_code(202);
    rsp.set_status_message("Hi There");
    rsp.add_header("HeaderA", "a value");
    rsp.set_content("My message content");
    rsp.write(m_file, ec);
    BOOST_REQUIRE(! ec);

    // another request
    req.set_resource("/blah.html");
    req.add_header("HeaderA", "a value");
    req.set_content("My request content");
    req.write(m_file, ec);
    BOOST_REQUIRE(! ec);
    
    // another response
    rsp.set_status_code(302);
    rsp.set_status_message("Hello There");
    rsp.add_header("HeaderB", "another value");
    rsp.clear_content();
    rsp.write(m_file, ec);
    BOOST_REQUIRE(! ec);

    // one last request
    req.set_resource("/last.html");
    req.add_header("HeaderB", "Bvalue");
    req.clear_content();
    req.write(m_file, ec);
    BOOST_REQUIRE(! ec);

    // flush file output
    m_file.flush();
    
    // validate file contents
    std::string contents = getFileContents();
    BOOST_CHECK_EQUAL(contents, "GET /test.html HTTP/1.1\r\nConnection: Keep-Alive\r\nContent-Length: 0\r\nTest: Something\r\n\r\n"
        "HTTP/1.1 202 Hi There\r\nConnection: Keep-Alive\r\nContent-Length: 18\r\nHeaderA: a value\r\n\r\nMy message content"
        "GET /blah.html HTTP/1.1\r\nConnection: Keep-Alive\r\nContent-Length: 18\r\nHeaderA: a value\r\nTest: Something\r\n\r\nMy request content"
        "HTTP/1.1 302 Hello There\r\nConnection: Keep-Alive\r\nContent-Length: 0\r\nHeaderA: a value\r\nHeaderB: another value\r\n\r\n"
        "GET /last.html HTTP/1.1\r\nConnection: Keep-Alive\r\nContent-Length: 0\r\nHeaderA: a value\r\nHeaderB: Bvalue\r\nTest: Something\r\n\r\n");

    m_file.clear();
    m_file.seekg(0);

    // read first request
    http::request req1;
    req1.read(m_file, ec);
    BOOST_REQUIRE(! ec);
    
    // read first response
    http::response rsp1;
    rsp1.read(m_file, ec);
    BOOST_REQUIRE(! ec);

    // read second request
    http::request req2;
    req2.read(m_file, ec);
    BOOST_REQUIRE(! ec);
    
    // read second response
    http::response rsp2;
    rsp2.read(m_file, ec);
    BOOST_REQUIRE(! ec);
    BOOST_CHECK_EQUAL(rsp2.get_status_code(), 302U);
    BOOST_CHECK_EQUAL(rsp2.get_status_message(), "Hello There");

    // read third request
    http::request req3;
    req3.read(m_file, ec);
    BOOST_REQUIRE(! ec);
    
    // write everything back to new file
    openNewFile();
    req1.write(m_file, ec);
    BOOST_REQUIRE(! ec);
    rsp1.write(m_file, ec);
    BOOST_REQUIRE(! ec);
    req2.write(m_file, ec);
    BOOST_REQUIRE(! ec);
    rsp2.write(m_file, ec);
    BOOST_REQUIRE(! ec);
    req3.write(m_file, ec);
    BOOST_REQUIRE(! ec);

    // flush file output
    m_file.flush();

    // make sure file matches original (no loss/change from read/write cycle)
    std::string new_contents = getFileContents();
    BOOST_CHECK_EQUAL(contents, new_contents);
}

BOOST_AUTO_TEST_CASE(checkWriteHTTPRequestWithCookies) {
    // build a request
    http::request req;
    req.set_resource("/test.html");
    req.add_cookie("a", "value");
    
    // write to file
    boost::system::error_code ec;
    req.write(m_file, ec);
    BOOST_REQUIRE(! ec);
    m_file.flush();
    
    // validate file contents
    std::string req_contents = getFileContents();
    BOOST_CHECK_EQUAL(req_contents, "GET /test.html HTTP/1.1\r\nConnection: Keep-Alive\r\nContent-Length: 0\r\nCookie: a=value\r\n\r\n");
}

BOOST_AUTO_TEST_CASE(checkWriteHTTPResponseWithCookies) {
    // build a request
    http::response rsp;
    rsp.set_status_code(200);
    rsp.set_status_message("OK");
    rsp.add_cookie("a", "value");

    // write to file
    boost::system::error_code ec;
    rsp.write(m_file, ec);
    BOOST_REQUIRE(! ec);
    m_file.flush();
    
    // validate file contents
    std::string rsp_contents = getFileContents();
    BOOST_CHECK_EQUAL(rsp_contents, "HTTP/1.1 200 OK\r\nConnection: Keep-Alive\r\nContent-Length: 0\r\nSet-Cookie: a=\"value\"; Version=1; Path=/\r\n\r\n");
}

BOOST_AUTO_TEST_SUITE_END()