1 // 2 // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot 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/beast 8 // 9 10 // Test that header file is self-contained. 11 #include <boost/beast/_experimental/http/icy_stream.hpp> 12 13 #include <boost/beast/core/buffers_adaptor.hpp> 14 #include <boost/beast/core/buffers_to_string.hpp> 15 #include <boost/beast/core/read_size.hpp> 16 #include <boost/beast/_experimental/test/stream.hpp> 17 #include <boost/beast/_experimental/unit_test/suite.hpp> 18 #include <array> 19 #include <memory> 20 #include <string> 21 22 namespace boost { 23 namespace beast { 24 namespace http { 25 26 class icy_stream_test 27 : public unit_test::suite 28 { 29 public: 30 void doMatrix(string_view in,string_view out)31 doMatrix(string_view in, string_view out) 32 { 33 using net::mutable_buffer; 34 net::io_context ioc; 35 auto len = out.size() + 8; 36 std::unique_ptr<char[]> p(new char[len]); 37 for(std::size_t i = 1; i < len; ++i) 38 { 39 std::array<mutable_buffer, 2> mbs{{ 40 mutable_buffer(p.get(), i), 41 mutable_buffer(p.get() + i, len - i)}}; 42 for(std::size_t j = 1; j < in.size(); ++j) 43 { 44 for(std::size_t k = 1; k < len; ++k) 45 { 46 // sync 47 { 48 buffers_adaptor<decltype(mbs)> ba(mbs); 49 std::memset(p.get(), 0, len); 50 51 icy_stream<test::stream> is{ioc}; 52 is.next_layer().read_size(j); 53 is.next_layer().append(in); 54 connect(is.next_layer()).close(); 55 56 error_code ec; 57 for(;;) 58 { 59 ba.commit(is.read_some( 60 ba.prepare(read_size(ba, k)), ec)); 61 if(ec) 62 break; 63 } 64 if(! BEAST_EXPECTS( 65 ec == net::error::eof, ec.message())) 66 continue; 67 auto const s = buffers_to_string(ba.data()); 68 BEAST_EXPECTS(s == out, s); 69 } 70 // async 71 { 72 buffers_adaptor<decltype(mbs)> ba(mbs); 73 std::memset(p.get(), 0, len); 74 75 icy_stream<test::stream> is{ioc}; 76 is.next_layer().read_size(j); 77 is.next_layer().append(in); 78 connect(is.next_layer()).close(); 79 80 error_code ec; 81 for(;;) 82 { 83 is.async_read_some( 84 ba.prepare(read_size(ba, k)), 85 [&ec, &ba](error_code ec_, std::size_t n) 86 { 87 ec = ec_; 88 ba.commit(n); 89 }); 90 ioc.run(); 91 ioc.restart(); 92 if(ec) 93 break; 94 } 95 if(! BEAST_EXPECTS( 96 ec == net::error::eof, ec.message())) 97 continue; 98 auto const s = buffers_to_string(ba.data()); 99 if(! BEAST_EXPECTS(s == out, s)) 100 { 101 s.size(); 102 } 103 } 104 } 105 } 106 } 107 } 108 109 void testStream()110 testStream() 111 { 112 doMatrix("HTTP/1.1 200 OK\r\n", "HTTP/1.1 200 OK\r\n"); 113 doMatrix("ICY 200 OK\r\n", "HTTP/1.1 200 OK\r\n"); 114 } 115 116 void run()117 run() override 118 { 119 testStream(); 120 } 121 }; 122 123 BEAST_DEFINE_TESTSUITE(beast,http,icy_stream); 124 125 } // http 126 } // beast 127 } // boost 128