| 1 | // |
| 2 | // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com) |
| 3 | // |
| 4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | // |
| 7 | // Official repository: https://github.com/boostorg/url |
| 8 | // |
| 9 | |
| 10 | #include <boost/url.hpp> |
| 11 | |
| 12 | #include "test_suite.hpp" |
| 13 | |
| 14 | #include <boost/core/ignore_unused.hpp> |
| 15 | #include <iostream> |
| 16 | #include <list> |
| 17 | #include <string> |
| 18 | |
| 19 | namespace boost { |
| 20 | namespace urls { |
| 21 | |
| 22 | struct doc_3_urls_test |
| 23 | { |
| 24 | static |
| 25 | void |
| 26 | doc_parsing() |
| 27 | { |
| 28 | { |
| 29 | //[code_containers_1_1 |
| 30 | system::result< url_view > r = parse_uri( s: "https://www.example.com/path/to/file.txt" ); |
| 31 | //] |
| 32 | ignore_unused(r); |
| 33 | } |
| 34 | { |
| 35 | //[code_containers_1_2 |
| 36 | url_view u1 = parse_uri_reference( s: "wss://example.com/quote.cgi?symbol=BOOST¤cy=USD" ).value(); |
| 37 | |
| 38 | url_view u2( "wss://example.com/quote.cgi?symbol=BOOST¤cy=USD" ); |
| 39 | //] |
| 40 | ignore_unused(u1, u2); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | //[code_container_4_1 |
| 45 | auto segs( core::string_view s ) -> std::list< std::string > |
| 46 | { |
| 47 | url_view u( s ); |
| 48 | std::list< std::string > seq; |
| 49 | for( auto seg : u.encoded_segments() ) |
| 50 | seq.push_back( x: seg.decode() ); |
| 51 | return seq; |
| 52 | } |
| 53 | //] |
| 54 | |
| 55 | //[code_container_5_1 |
| 56 | auto parms( core::string_view s ) -> std::list< param > |
| 57 | { |
| 58 | url_view u( s ); |
| 59 | std::list< param > seq; |
| 60 | for( auto qp : u.params() ) |
| 61 | seq.push_back( x: qp ); |
| 62 | return seq; |
| 63 | } |
| 64 | //] |
| 65 | |
| 66 | void |
| 67 | path_segments() |
| 68 | { |
| 69 | auto check = []( |
| 70 | core::string_view path, |
| 71 | std::initializer_list<core::string_view> segs, |
| 72 | bool path_abs) |
| 73 | { |
| 74 | auto r1 = parse_path(s: path); |
| 75 | BOOST_TEST(r1.has_value()); |
| 76 | auto ss = r1.value(); |
| 77 | BOOST_TEST_EQ(segs.size(), ss.size()); |
| 78 | BOOST_TEST_EQ(path_abs, r1->is_absolute()); |
| 79 | auto it0 = segs.begin(); |
| 80 | auto it1 = ss.begin(); |
| 81 | while (it0 != segs.end()) |
| 82 | { |
| 83 | BOOST_TEST_EQ(*it0, *it1); |
| 84 | ++it0; |
| 85 | ++it1; |
| 86 | } |
| 87 | }; |
| 88 | |
| 89 | check("" , { }, false); |
| 90 | check("/" , { }, true); |
| 91 | check("./" , { "" }, false); |
| 92 | check("./usr" , { "usr" }, false); |
| 93 | check("/index.htm" , { "index.htm" }, true); |
| 94 | check("/images/cat-pic.gif" , { "images" , "cat-pic.gif" }, true); |
| 95 | check("images/cat-pic.gif" , { "images" , "cat-pic.gif" }, false); |
| 96 | check("/fast//query" , { "fast" , "" , "query" }, true); |
| 97 | check("fast//" , { "fast" , "" , "" }, false); |
| 98 | check("/./" , { "" }, true); |
| 99 | check(".//" , { "" , "" }, false); |
| 100 | } |
| 101 | |
| 102 | void |
| 103 | run() |
| 104 | { |
| 105 | // segs() |
| 106 | { |
| 107 | url_view u; |
| 108 | segs(s: "http://example.com/path/to/file.txt" ); |
| 109 | } |
| 110 | |
| 111 | path_segments(); |
| 112 | } |
| 113 | }; |
| 114 | |
| 115 | TEST_SUITE( |
| 116 | doc_3_urls_test, |
| 117 | "boost.url.doc.3_urls" ); |
| 118 | |
| 119 | } // urls |
| 120 | } // boost |
| 121 | |