File: authentication_tests.cpp

package info (click to toggle)
cpprest 2.10.19-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,916 kB
  • sloc: cpp: 71,086; sh: 275; makefile: 170; javascript: 147
file content (727 lines) | stat: -rw-r--r-- 27,666 bytes parent folder | download | duplicates (2)
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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
/***
 * Copyright (C) Microsoft. All rights reserved.
 * Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
 *
 * =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
 *
 * Tests cases for authentication with http_clients.
 *
 * =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 ****/

#include "stdafx.h"

#include <stdexcept>

#ifdef _WIN32
#ifdef __cplusplus_winrt
#if !defined(__WRL_NO_DEFAULT_LIB__)
#define __WRL_NO_DEFAULT_LIB__
#endif
#include <msxml6.h>
#include <wrl.h>
#else
#include <windows.h>

#include <winhttp.h>
#pragma comment(lib, "winhttp")
#endif
#endif

#if !defined(_WIN32)
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Winfinite-recursion"
#endif
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
#if defined(__clang__)
#pragma clang diagnostic pop
#endif
#endif

using namespace web;
using namespace utility;
using namespace concurrency;
using namespace web::http;
using namespace web::http::client;

using namespace tests::functional::http::utilities;

namespace tests
{
namespace functional
{
namespace http
{
namespace client
{
SUITE(authentication_tests)
{
    TEST_FIXTURE(uri_address, auth_no_data, "Ignore:Linux", "89", "Ignore:Apple", "89")
    {
        pplx::task<void> t, t2;
        {
            test_http_server::scoped_server scoped(m_uri);
            http_client_config client_config;
            web::credentials cred(U("some_user"), U("some_password")); // WinHTTP requires non-empty password
            client_config.set_credentials(cred);
            http_client client(m_uri, client_config);
            const method mtd = methods::POST;

            http_request msg(mtd);

            t = scoped.server()->next_request().then([&](test_request* p_request) {
                http_asserts::assert_test_request_equals(p_request, mtd, U("/"));

                // Auth header
                std::map<utility::string_t, utility::string_t> headers;
                headers[U("WWW-Authenticate")] = U("Basic realm = \"WallyWorld\"");

                // unauthorized
                p_request->reply(status_codes::Unauthorized, U("Authentication Failed"), headers);
            });
            t2 = scoped.server()->next_request().then([&](test_request* p_request) {
                http_asserts::assert_test_request_equals(p_request, methods::POST, U("/"));
                p_request->reply(200);
            });

            try
            {
                http_asserts::assert_response_equals(client.request(msg).get(), status_codes::OK);
            }
            catch (...)
            {
                VERIFY_ARE_EQUAL(0, 1);
            }
        }
        try
        {
            t.get();
        }
        catch (...)
        {
            VERIFY_ARE_EQUAL(0, 1);
        }
        try
        {
            t2.get();
        }
        catch (...)
        {
            VERIFY_ARE_EQUAL(0, 1);
        }
    }

// TFS 648783
#ifndef __cplusplus_winrt
    TEST_FIXTURE(uri_address, proxy_auth_known_contentlength, "Ignore:Linux", "88", "Ignore:Apple", "88")
    {
        pplx::task<void> t, t2;
        {
            test_http_server::scoped_server scoped(m_uri);
            http_client_config client_config;
            web::credentials cred(U("some_user"), U("some_password")); // WinHTTP requires non-empty password
            client_config.set_credentials(cred);
            http_client client(m_uri, client_config);
            const method mtd = methods::POST;
            utility::string_t contents(U("Hello World"));

            http_request msg(mtd);
            msg.set_body(contents);

            t = scoped.server()->next_request().then([&](test_request* p_request) {
                http_asserts::assert_test_request_equals(p_request, mtd, U("/"));

                // Auth header
                std::map<utility::string_t, utility::string_t> headers;
                headers[U("WWW-Authenticate")] = U("Basic realm = \"WallyWorld\"");

                // unauthorized
                p_request->reply(status_codes::Unauthorized, U("Authentication Failed"), headers);
            });

            t2 = scoped.server()->next_request().then([&](test_request* p_request) {
                http_asserts::assert_test_request_equals(
                    p_request, methods::POST, U("/"), U("text/plain; charset=utf-8"), contents);

                p_request->reply(200);
            });

            http_asserts::assert_response_equals(client.request(msg).get(), status_codes::OK);
        }
        try
        {
            t.get();
        }
        catch (...)
        {
            VERIFY_ARE_EQUAL(0, 1);
        }
        try
        {
            t2.get();
        }
        catch (...)
        {
            VERIFY_ARE_EQUAL(0, 1);
        }
    }
#endif

    TEST_FIXTURE(uri_address, proxy_auth_noseek, "Ignore:Linux", "88", "Ignore:Apple", "88")
    {
        web::http::uri uri(U("http://localhost:34567/"));
        test_http_server::scoped_server scoped(uri);
        http_client client(
            uri); // In this test, the request cannot be resent, so the username and password are not required
        const method mtd = methods::POST;

        auto buf = streams::producer_consumer_buffer<unsigned char>();
        buf.putc('a').get();
        buf.close(std::ios_base::out).get();

        http_request msg(mtd);
        msg.set_body(buf.create_istream(), 1);

        scoped.server()->next_request().then([&](test_request* p_request) {
            http_asserts::assert_test_request_equals(p_request, mtd, U("/"));

            // Auth header
            std::map<utility::string_t, utility::string_t> headers;
            headers[U("WWW-Authenticate")] = U("Basic realm = \"WallyWorld\"");

            // unauthorized
            p_request->reply(status_codes::Unauthorized, U("Authentication Failed"), headers);
        });

        http_asserts::assert_response_equals(client.request(msg).get(), status_codes::Unauthorized);
    }

// Must specify content length with winrt client, so this test case isn't possible.
#ifndef __cplusplus_winrt
    TEST_FIXTURE(uri_address, proxy_auth_unknown_contentlength, "Ignore:Linux", "88", "Ignore:Apple", "88")
    {
        pplx::task<void> t;
        {
            test_http_server::scoped_server scoped(m_uri);
            http_client_config client_config;
            web::credentials cred(U("some_user"), U("some_password")); // WinHTTP requires non-empty password
            client_config.set_credentials(cred);
            http_client client(m_uri, client_config);
            const method mtd = methods::POST;

            std::vector<uint8_t> msg_body;
            msg_body.push_back('a');

            http_request msg(mtd);
            msg.set_body(streams::container_stream<std::vector<uint8_t>>::open_istream(std::move(msg_body)));

            auto replyFunc = [&](test_request* p_request) {
                utility::string_t contents(U("a"));
                http_asserts::assert_test_request_equals(
                    p_request, methods::POST, U("/"), U("application/octet-stream"), contents);

                p_request->reply(200);
            };

            t = scoped.server()
                    ->next_request()
                    .then([&](test_request* p_request) {
                        http_asserts::assert_test_request_equals(p_request, mtd, U("/"));

                        // Auth header
                        std::map<utility::string_t, utility::string_t> headers;
                        headers[U("WWW-Authenticate")] = U("Basic realm = \"WallyWorld\"");

                        // unauthorized
                        p_request->reply(status_codes::Unauthorized, U("Authentication Failed"), headers);
                    })
                    .then([&scoped, replyFunc]() {
                        // Client resent the request
                        return scoped.server()->next_request().then(replyFunc);
                    });

            http_asserts::assert_response_equals(client.request(msg).get(), status_codes::OK);
        }
        t.get();
    }

    // Accessing a server that returns 401 with an empty user name should not resend the request with an empty password
    TEST_FIXTURE(uri_address, empty_username_password)
    {
        test_http_server::scoped_server scoped(m_uri);
        http_client client(m_uri);

        auto t = scoped.server()->next_request().then([&](test_request* p_request) {
            std::map<utility::string_t, utility::string_t> headers;
            headers[U("h1")] = U("data1");
            // Auth header
            headers[U("WWW-Authenticate")] = U("Basic realm = \"myRealm\"");
            // unauthorized
            p_request->reply(status_codes::Unauthorized, U("Authentication Failed"), headers, "a");
        });

        http_response response = client.request(methods::GET).get();
        auto str_body = response.extract_vector().get();
        auto h1 = response.headers()[U("h1")];
        VERIFY_ARE_EQUAL(status_codes::Unauthorized, response.status_code());
        VERIFY_ARE_EQUAL(str_body[0], 'a');
        VERIFY_ARE_EQUAL(h1, U("data1"));
        t.get();
    }
#endif

    // Fails on WinRT due to TFS 648278
    // Accessing a server that supports auth, but returns 401, even after the user has provided valid creds
    // We're making sure the error is reported properly, and the response data from the second response is received
    TEST_FIXTURE(uri_address, error_after_valid_credentials, "Ignore:Linux", "89", "Ignore:Apple", "89")
    {
        pplx::task<void> t;
        {
            web::http::uri uri(U("http://localhost:34569/"));
            test_http_server::scoped_server scoped(uri);
            http_client_config client_config;
            web::credentials cred(U("some_user"), U("some_password"));
            client_config.set_credentials(cred);
            http_client client(uri, client_config);

            auto replyFunc = [&](test_request* p_request) {
                std::map<utility::string_t, utility::string_t> headers;
                // Auth header
                headers[U("WWW-Authenticate")] = U("Basic realm = \"WallyWorld\"");
                headers[U("h1")] = U("data2");
                // still unauthorized after the user has resent the request with the credentials
                p_request->reply(status_codes::Unauthorized, U("Authentication Failed"), headers, "def");
            };

            t = scoped.server()
                    ->next_request()
                    .then([&](test_request* p_request) {
                        std::map<utility::string_t, utility::string_t> headers;
                        headers[U("h1")] = U("data1");
                        // Auth header
                        headers[U("WWW-Authenticate")] = U("Basic realm = \"myRealm\"");
                        // unauthorized
                        p_request->reply(status_codes::Unauthorized, U("Authentication Failed"), headers, "abc");
                    })
                    .then([&scoped, &replyFunc]() {
                        // Client resent the request
                        return scoped.server()->next_request().then(replyFunc);
                    })
#ifdef __cplusplus_winrt
                    .then([&scoped, &replyFunc]() {
                        // in winrt, client resent the request again
                        return scoped.server()->next_request().then(replyFunc);
                    })
#endif
                ;

            http_response response = client.request(methods::GET).get();
            auto str_body = response.extract_vector().get();
            auto h1 = response.headers()[U("h1")];
            VERIFY_ARE_EQUAL(status_codes::Unauthorized, response.status_code());
            VERIFY_ARE_EQUAL(str_body[0], 'd');
            VERIFY_ARE_EQUAL(str_body[1], 'e');
            VERIFY_ARE_EQUAL(str_body[2], 'f');
            VERIFY_ARE_EQUAL(h1, U("data2"));
        }
        t.get();
    }

    // These tests are disabled since they require a server with authentication running.
    // The server portion to use is the C# AuthenticationListener.

    class server_properties
    {
    public:
        server_properties() {}

        // Helper function to retrieve all parameters necessary for setup tests.
        void load_parameters()
        {
            m_uri = uri(utility::conversions::to_string_t(UnitTest::GlobalSettings::Get("Server")));
            if (UnitTest::GlobalSettings::Has("UserName"))
            {
                m_username = utility::conversions::to_string_t(UnitTest::GlobalSettings::Get("UserName"));
            }
            if (UnitTest::GlobalSettings::Has("Password"))
            {
                m_password = utility::conversions::to_string_t(UnitTest::GlobalSettings::Get("Password"));
            }
        }

        web::http::uri m_uri;
        string_t m_username;
        string_t m_password;
    };

    // This test should be executed for NTLM, Negotiate, IntegratedWindowsAuth, and Anonymous.
    TEST_FIXTURE(server_properties, successful_auth_no_cred, "Requires", "Server")
    {
        load_parameters();

        http_client client(m_uri);
        http_response response = client.request(methods::GET).get();
        VERIFY_ARE_EQUAL(status_codes::OK, response.status_code());
    }

    TEST_FIXTURE(server_properties, digest_basic_auth_no_cred, "Requires", "Server")
    {
        load_parameters();

        http_client client(m_uri);
        http_response response = client.request(methods::GET).get();
        VERIFY_ARE_EQUAL(status_codes::Unauthorized, response.status_code());
    }

    TEST_FIXTURE(server_properties, none_auth_no_cred, "Requires", "Server")
    {
        load_parameters();

        http_client client(m_uri);
        http_response response = client.request(methods::GET).get();
        VERIFY_ARE_EQUAL(status_codes::Forbidden, response.status_code());
    }

    // This test should be executed for NTLM, Negotiate, IntegratedWindowsAuth, and Digest.
    TEST_FIXTURE(server_properties, unsuccessful_auth_with_basic_cred, "Requires", "Server;UserName;Password")
    {
        load_parameters();

        http_client_config config;
        config.set_credentials(web::credentials(m_username, m_password));

        http_client client(m_uri, config);
        http_response response = client.request(methods::GET).get();
        VERIFY_ARE_EQUAL(status_codes::Unauthorized, response.status_code());
    }

    TEST_FIXTURE(server_properties, basic_anonymous_auth_with_basic_cred, "Requires", "Server;UserName;Password")
    {
        load_parameters();

        http_client_config config;
        config.set_credentials(web::credentials(m_username, m_password));
        http_client client(m_uri, config);
        http_request req(methods::GET);
        req.headers().add(U("UserName"), m_username);
        req.headers().add(U("Password"), m_password);
        http_response response = client.request(req).get();
        VERIFY_ARE_EQUAL(status_codes::OK, response.status_code());
    }

    TEST_FIXTURE(server_properties, none_auth_with_cred, "Requires", "Server;UserName;Password")
    {
        load_parameters();

        http_client_config config;
        config.set_credentials(web::credentials(m_username, m_password));
        http_client client(m_uri, config);
        http_response response = client.request(methods::GET).get();
        VERIFY_ARE_EQUAL(status_codes::Forbidden, response.status_code());
    }

    // This test should be executed for all authentication schemes except None.
    TEST_FIXTURE(server_properties, successful_auth_with_domain_cred, "Requires", "Server;UserName;Password")
    {
        load_parameters();

        http_client_config config;
        config.set_credentials(web::credentials(m_username, m_password));
        http_client client(m_uri, config);
        http_request req(methods::GET);
        req.headers().add(U("UserName"), m_username);
        req.headers().add(U("Password"), m_password);
        http_response response = client.request(req).get();
        VERIFY_ARE_EQUAL(status_codes::OK, response.status_code());
    }

#ifndef __cplusplus_winrt // WinRT implementation doesn't support request buffer caching.
    TEST_FIXTURE(server_properties, failed_authentication_resend_request_error, "Requires", "Server;UserName;Password")
    {
        load_parameters();

        http_client_config config;
        config.set_credentials(web::credentials(m_username, m_password));
        http_client client(m_uri, config);

        const size_t rawDataSize = 8;

        std::vector<unsigned char> data(rawDataSize);
        memcpy(&data[0], "raw data", rawDataSize);

        http_request request;
        request.set_method(methods::POST);
        request.set_body(data);
        http_response response = client.request(request).get();

        VERIFY_ARE_EQUAL(200, response.status_code());
    }
#endif

#ifdef __cplusplus_winrt
    TEST_FIXTURE(uri_address, set_user_options_winrt)
    {
        test_http_server::scoped_server scoped(m_uri);
        scoped.server()->next_request().then([](test_request* p_request) { p_request->reply(status_codes::OK); });

        http_client_config config;
        config.set_nativehandle_options([](native_handle handle) -> void {
            auto hr = handle->SetProperty(XHR_PROP_TIMEOUT, 1000);
            if (!SUCCEEDED(hr)) throw std::runtime_error("The Test Exception");
        });
        http_client client(m_uri, config);
        auto response = client.request(methods::GET).get();
        VERIFY_ARE_EQUAL(200, response.status_code());
    }
#endif // __cplusplus_winrt

#ifdef _WIN32
#if !defined(__cplusplus_winrt)
    TEST_FIXTURE(server_properties, set_user_options, "Requires", "Server;UserName;Password")
    {
        load_parameters();

        http_client_config config;
        config.set_credentials(web::credentials(m_username, m_password));

        config.set_nativehandle_options([&](native_handle handle) -> void {
            DWORD policy = WINHTTP_AUTOLOGON_SECURITY_LEVEL_LOW;
            if (!WinHttpSetOption(handle, WINHTTP_OPTION_AUTOLOGON_POLICY, &policy, sizeof(policy)))
            {
                throw std::runtime_error("The Test Error");
            }
        });

        http_client client(m_uri, config);

        const size_t rawDataSize = 8;

        std::vector<unsigned char> data(rawDataSize);
        memcpy(&data[0], "raw data", rawDataSize);

        http_request request;
        request.set_method(methods::POST);
        request.set_body(data);

        VERIFY_ARE_EQUAL(200, client.request(request).get().status_code());
    }

    TEST_FIXTURE(uri_address, auth_producer_consumer_buffer)
    {
        auto buf = streams::producer_consumer_buffer<unsigned char>();
        buf.putc('a').get();
        buf.putc('a').get();
        buf.putc('a').get();
        buf.putc('a').get();
        buf.close(std::ios_base::out).get();
        http_request msg(methods::POST);
        msg.set_body(buf.create_istream());

        http_client_config config;
        VERIFY_IS_FALSE(config.buffer_request());
        config.set_buffer_request(true);
        VERIFY_IS_TRUE(config.buffer_request());
        config.set_credentials(web::credentials(U("USERNAME"), U("PASSWORD")));

        http_client client(m_uri, config);

        pplx::task<void> t, t2;
        test_http_server::scoped_server scoped(m_uri);

        t = scoped.server()->next_request().then([&](test_request* p_request) {
            http_asserts::assert_test_request_equals(
                p_request, methods::POST, U("/"), U("application/octet-stream"), U("aaaa"));
            std::map<utility::string_t, utility::string_t> headers;
            headers[U("WWW-Authenticate")] = U("Basic realm = \"WallyWorld\"");

            p_request->reply(status_codes::Unauthorized, U("Authentication Failed"), headers);
        });
        t2 = scoped.server()->next_request().then([&](test_request* p_request) {
            http_asserts::assert_test_request_equals(
                p_request, methods::POST, U("/"), U("application/octet-stream"), U("aaaa"));
            p_request->reply(200);
        });

        http_asserts::assert_response_equals(client.request(msg).get(), status_codes::OK);
        scoped.server()->close();
        VERIFY_NO_THROWS(t.get());
        VERIFY_NO_THROWS(t2.get());
    }

    TEST_FIXTURE(uri_address, auth_producer_comsumer_buffer_fail_no_cred)
    {
        auto buf = streams::producer_consumer_buffer<unsigned char>();
        buf.putc('a').get();
        buf.putc('a').get();
        buf.putc('a').get();
        buf.putc('a').get();
        buf.close(std::ios_base::out).get();
        http_request msg(methods::POST);
        msg.set_body(buf.create_istream());

        http_client client(m_uri);

        pplx::task<void> t;
        {
            test_http_server::scoped_server scoped(m_uri);
            t = scoped.server()->next_request().then([&](test_request* p_request) {
                http_asserts::assert_test_request_equals(
                    p_request, methods::POST, U("/"), U("application/octet-stream"), U("aaaa"));
                std::map<utility::string_t, utility::string_t> headers;
                headers[U("WWW-Authenticate")] = U("Basic realm = \"WallyWorld\"");

                p_request->reply(status_codes::Unauthorized, U("Authentication Failed"), headers);
            });

            http_asserts::assert_response_equals(client.request(msg).get(), status_codes::Unauthorized);
        }
        t.get();
    }

    TEST_FIXTURE(uri_address, auth_producer_comsumer_buffer_fail)
    {
        auto buf = streams::producer_consumer_buffer<unsigned char>();
        buf.putc('a').get();
        buf.close(std::ios_base::out).get();
        http_request msg(methods::POST);
        msg.set_body(buf.create_istream());

        http_client_config config;
        config.set_buffer_request(true);
        config.set_credentials(web::credentials(U("USERNAME"), U("PASSWORD")));

        http_client client(m_uri, config);
        pplx::task<void> t;
        {
            test_http_server::scoped_server scoped(m_uri);

            auto replyFunc = [&](test_request* p_request) {
                http_asserts::assert_test_request_equals(
                    p_request, methods::POST, U("/"), U("application/octet-stream"), U("a"));
                std::map<utility::string_t, utility::string_t> headers;
                headers[U("WWW-Authenticate")] = U("Basic realm = \"WallyWorld2\"");

                p_request->reply(status_codes::Unauthorized, U("Authentication Failed"), headers);
            };

            t = scoped.server()
                    ->next_request()
                    .then([&](test_request* p_request) {
                        http_asserts::assert_test_request_equals(
                            p_request, methods::POST, U("/"), U("application/octet-stream"), U("a"));
                        std::map<utility::string_t, utility::string_t> headers;
                        headers[U("WWW-Authenticate")] = U("Basic realm = \"WallyWorld\"");

                        p_request->reply(status_codes::Unauthorized, U("Authentication Failed"), headers);
                    })
                    .then([&scoped, replyFunc]() { return scoped.server()->next_request().then(replyFunc); });

            http_asserts::assert_response_equals(client.request(msg).get(), status_codes::Unauthorized);
        }
        VERIFY_NO_THROWS(t.get());
    }
#endif

    TEST_FIXTURE(uri_address, set_user_options_exceptions)
    {
        test_http_server::scoped_server scoped(m_uri);
        http_client_config config;
        class TestException;
        config.set_nativehandle_options([](native_handle) { throw std::runtime_error("The Test exception"); });
        http_client client(m_uri, config);
        VERIFY_THROWS(client.request(methods::GET).get(), std::runtime_error);
    }
#endif // _WIN32

    // Fix for 522831 AV after failed authentication attempt
    TEST_FIXTURE(uri_address, failed_authentication_attempt, "Ignore:Linux", "89", "Ignore:Apple", "89")
    {
        handle_timeout([] {
            http_client_config config;
            web::credentials cred(U("user"), U("schmuser"));
            config.set_credentials(cred);
            http_client client(U("https://apis.live.net"), config);
            http_response response = client.request(methods::GET, U("V5.0/me/skydrive/files")).get();
            VERIFY_ARE_EQUAL(status_codes::Unauthorized, response.status_code());
            auto v = response.extract_vector().get();
            std::string s(v.begin(), v.end());
            // The resulting data must be non-empty (an error about missing access token)
            VERIFY_IS_FALSE(s.empty());
        });
    }

#if !defined(_WIN32)

    // http_server does not support auth
    void auth_test_impl(bool fail)
    {
        std::string user("user1"), password("user1");
        auto return_code = status_codes::OK;

        if (fail)
        {
            password = "invalid";
            return_code = status_codes::Unauthorized;
        }

        http_client_config client_config;
        web::credentials cred(U(user), U(password));
        client_config.set_credentials(cred);
        http_client client(U("http://httpbin.org/basic-auth/user1/user1"), client_config);

        http_response response = client.request(methods::GET).get();
        VERIFY_ARE_EQUAL(return_code, response.status_code());
    }

    //TEST(auth_no_data) { auth_test_impl(false); }

    //TEST(unsuccessful_auth_with_basic_cred) { auth_test_impl(true); }

    TEST_FIXTURE(uri_address, set_user_options_asio_http)
    {
        test_http_server::scoped_server scoped(m_uri);
        scoped.server()->next_request().then([](test_request* p_request) { p_request->reply(status_codes::OK); });

        http_client_config config;
        config.set_nativehandle_options([](native_handle handle) {
            boost::asio::ip::tcp::socket* socket = static_cast<boost::asio::ip::tcp::socket*>(handle);
            // Socket shouldn't be open yet since no requests have gone out.
            VERIFY_ARE_EQUAL(false, socket->is_open());
        });
        http_client client(m_uri, config);
        auto response = client.request(methods::GET).get();
        VERIFY_ARE_EQUAL(200, response.status_code());
    }

    /*
    TEST_FIXTURE(uri_address, set_user_options_asio_https)
    {
        handle_timeout([] {
            http_client_config config;
            config.set_nativehandle_options([](native_handle handle) {
                boost::asio::ssl::stream<boost::asio::ip::tcp::socket&>* streamobj =
                    static_cast<boost::asio::ssl::stream<boost::asio::ip::tcp::socket&>*>(handle);
                const auto& tcpLayer = streamobj->lowest_layer();
                VERIFY_ARE_EQUAL(false, tcpLayer.is_open());
            });

            http_client client(U("https://apis.live.net"), config);
            http_response response = client.request(methods::GET, U("V5.0/me/skydrive/files")).get();
            VERIFY_ARE_EQUAL(status_codes::Unauthorized, response.status_code());
            auto v = response.extract_vector().get();
            // The resulting data must be non-empty (an error about missing access token)
            VERIFY_IS_FALSE(v.empty());
        });
    }
    */

#endif

} // SUITE(authentication_tests)

} // namespace client
} // namespace http
} // namespace functional
} // namespace tests