33#include <jau/test/catch2_ext.hpp>
60 std::string one_line =
"Hello World, this is a test and I like it. Exactly 100 characters long. 0123456780 abcdefghjklmnop..";
63 REQUIRE( ofs.
good() ==
true );
64 REQUIRE( ofs.
is_open() ==
true );
66 for(
size_t i=0; i < 1024_uz * 10_uz; i+=one_line.size()) {
67 REQUIRE( one_line.size() == ofs.
write(one_line.data(), one_line.size()) );
71 int res = std::system(
"killall mini_httpd");
74 const std::string cmd = std::string(
mini_httpd_exe)+
" -p 8080 -l "+cwd+
"/mini_httpd.log";
76 res = std::system(cmd.c_str());
83 int res = std::system(
"killall mini_httpd");
93 REQUIRE( 0 < protos.size() );
95 REQUIRE( 0 == protos.size() );
106 const std::string url =
"https://localhost:8080/" +
basename_10kiB;
116 const std::string url =
"lala://localhost:8080/" +
basename_10kiB;
122 const std::string url =
"lala://localhost:8080/" +
basename_10kiB;
124 size_t consumed_calls = 0;
125 uint64_t consumed_total_bytes = 0;
129 consumed_total_bytes += data.size();
133 REQUIRE( 0 == http_total_bytes );
134 REQUIRE( consumed_total_bytes == http_total_bytes );
135 REQUIRE( 0 == consumed_calls );
139 const std::string url =
"lala://localhost:8080/" +
basename_10kiB;
143 REQUIRE( res->header_resp.completed() ==
true );
144 REQUIRE( res->has_content_length ==
false );
145 REQUIRE( res->content_length == 0 );
146 REQUIRE( res->failed() ==
true );
150 const std::string url =
"lala://localhost:8080/" +
basename_10kiB;
154 REQUIRE( !res->thread.joinable() );
155 REQUIRE( res->header_resp.completed() ==
true );
156 REQUIRE( res->has_content_length ==
false );
157 REQUIRE( res->content_length == 0 );
158 REQUIRE( res->failed() ==
true );
168 const size_t file_size = in_stats.
size();
172 REQUIRE( outfile.
good() );
176 size_t consumed_calls = 0;
177 uint64_t consumed_total_bytes = 0;
180 if( data.size() != outfile.
write(data.data(), data.size()) ) {
183 consumed_total_bytes += data.size();
184 jau::PLAIN_PRINT(
true,
"test01_sync_ok #%zu: consumed size %zu, total %" PRIu64
", capacity %zu, final %d",
185 consumed_calls, data.size(), consumed_total_bytes, data.capacity(), is_final );
189 const uint64_t out_bytes_total = outfile.
tellp();
190 jau::PLAIN_PRINT(
true,
"test01_sync_ok Done: total %" PRIu64
", capacity %zu", consumed_total_bytes, buffer.capacity());
192 REQUIRE( file_size == http_total_bytes );
193 REQUIRE( consumed_total_bytes == http_total_bytes );
194 REQUIRE( consumed_total_bytes == out_bytes_total );
202 const std::string url_input =
url_input_root +
"doesnt_exists.txt";
205 REQUIRE( outfile.
good() );
209 size_t consumed_calls = 0;
210 uint64_t consumed_total_bytes = 0;
213 if( data.size() != outfile.
write(data.data(), data.size()) ) {
216 consumed_total_bytes += data.size();
217 jau::PLAIN_PRINT(
true,
"test02_sync_404 #%zu: consumed size %zu, total %" PRIu64
", capacity %zu, final %d",
218 consumed_calls, data.size(), consumed_total_bytes, data.capacity(), is_final );
222 const uint64_t out_bytes_total = outfile.
tellp();
223 jau::PLAIN_PRINT(
true,
"test02_sync_404 Done: total %" PRIu64
", capacity %zu", consumed_total_bytes, buffer.capacity());
225 REQUIRE( 0 == http_total_bytes );
226 REQUIRE( consumed_total_bytes == http_total_bytes );
227 REQUIRE( consumed_total_bytes == out_bytes_total );
236 const size_t file_size = in_stats.
size();
240 REQUIRE( outfile.
good() );
243 constexpr const size_t buffer_size = 4096;
247 REQUIRE( res->failed() ==
false );
250 size_t consumed_loops = 0;
251 uint64_t consumed_total_bytes = 0;
253 while( res->processing() || !rb.
isEmpty() ) {
256 const size_t consumed_bytes = rb.
getBlocking(buffer.data(), buffer_size, 1, 500_ms);
257 consumed_total_bytes += consumed_bytes;
258 jau::PLAIN_PRINT(
true,
"test11_async_ok.0 #%zu: consumed[this %zu, total %" PRIu64
", result %d, rb %s",
259 consumed_loops, consumed_bytes, consumed_total_bytes, res->result.load(), rb.
toString().c_str() );
260 if( consumed_bytes != outfile.
write(buffer.data(), consumed_bytes) ) {
264 const uint64_t out_bytes_total = outfile.
tellp();
265 jau::PLAIN_PRINT(
true,
"test11_async_ok.X Done: total %" PRIu64
", result %d, rb %s",
266 consumed_total_bytes, (
int)res->result.load(), rb.
toString().c_str() );
270 REQUIRE( res->header_resp.completed() ==
true );
271 REQUIRE( res->has_content_length ==
true );
272 REQUIRE( res->content_length == file_size );
273 REQUIRE( res->content_length == consumed_total_bytes );
274 REQUIRE( res->content_length == out_bytes_total );
275 REQUIRE( res->success() ==
true );
283 const std::string url_input =
url_input_root +
"doesnt_exists.txt";
286 REQUIRE( outfile.
good() );
289 constexpr const size_t buffer_size = 4096;
293 REQUIRE( res->failed() ==
false );
296 size_t consumed_loops = 0;
297 uint64_t consumed_total_bytes = 0;
299 while( res->processing() || !rb.
isEmpty() ) {
302 const size_t consumed_bytes = rb.
getBlocking(buffer.data(), buffer_size, 1, 500_ms);
303 consumed_total_bytes += consumed_bytes;
304 jau::PLAIN_PRINT(
true,
"test12_async_404.0 #%zu: consumed[this %zu, total %" PRIu64
", result %d, rb %s",
305 consumed_loops, consumed_bytes, consumed_total_bytes, res->result.load(), rb.
toString().c_str() );
306 if( consumed_bytes != outfile.
write(
reinterpret_cast<char*
>(buffer.data()), consumed_bytes) ) {
310 const uint64_t out_bytes_total = outfile.
tellp();
311 jau::PLAIN_PRINT(
true,
"test12_async_404.X Done: total %" PRIu64
", result %d, rb %s",
312 consumed_total_bytes, (
int)res->result.load(), rb.
toString().c_str() );
316 REQUIRE( res->header_resp.completed() ==
true );
317 REQUIRE( res->has_content_length ==
false );
318 REQUIRE( res->content_length == 0 );
319 REQUIRE( res->content_length == consumed_total_bytes );
320 REQUIRE( res->content_length == out_bytes_total );
321 REQUIRE( res->failed() ==
true );
322 REQUIRE( res->header_resp.response_code() == 404 );
const std::string basename_10kiB
const std::string url_input_root
Platform agnostic representation of POSIX ::lstat() and ::stat() for a given pathname.
uint64_t size() const noexcept
Returns the size in bytes of this element if is_file(), otherwise zero.
File based byte output stream, including named file descriptor.
size_t write(const void *, size_t) noexcept override
Write to the data sink.
uint64_t tellp() const noexcept override
Returns the output position indicator.
bool is_open() const noexcept override
Checks if the stream has an associated file.
bool good() const noexcept
Checks if no error nor eof() has occurred i.e.
bool getBlocking(Value_type &result, const fraction_i64 &timeout, bool &timeout_occurred) noexcept
Dequeues the oldest enqueued element.
bool isEmpty() const noexcept
Returns true if this ring buffer is empty, otherwise false.
std::string toString() const noexcept
Returns a short string representation incl.
std::string to_string(const endian_t v) noexcept
Return std::string representation of the given endian.
std::string get_cwd() noexcept
Return the current working directory or empty on failure.
bool remove(const std::string &path, const traverse_options topts=traverse_options::none) noexcept
Remove the given path.
AsyncStreamResponseRef read_url_stream_async(net_tk_handle handle, const std::string &url, http::PostRequestPtr httpPostReq, ByteRingbuffer *buffer, const AsyncStreamConsumerFunc &consumer_fn) noexcept
Asynchronous URL stream reader using the given AsyncStreamConsumerFunc consumer_fn.
SyncStreamResponseRef read_url_stream_sync(net_tk_handle handle, const std::string &url, http::PostRequestPtr httpPostReq, ByteRingbuffer *buffer, const SyncStreamConsumerFunc &consumer_fn) noexcept
Synchronous URL stream reader using the given SyncStreamConsumerFunc consumer_fn.
bool is_local_file_protocol(const std::string_view &uri) noexcept
Returns true if the uri-scheme of given uri matches the local file protocol, i.e.
uint64_t read_url_stream(const std::string &url, secure_vector< uint8_t > &buffer, const StreamConsumerFunc &consumer_fn) noexcept
Synchronous URL stream reader using the given StreamConsumerFunc consumer_fn.
jau::function< bool(secure_vector< uint8_t > &, bool)> StreamConsumerFunc
Stream consumer function.
std::vector< std::string_view > supported_protocols() noexcept
Returns a list of supported protocol supported by libcurl network protocols, queried at runtime.
std::shared_ptr< SyncStreamResponse > SyncStreamResponseRef
std::shared_ptr< AsyncStreamResponse > AsyncStreamResponseRef
jau::ringbuffer< uint8_t, size_t > ByteRingbuffer
const size_t BEST_URLSTREAM_RINGBUFFER_SIZE
std::vector< T, jau::callocator_sec< T > > secure_vector
bool protocol_supported(const std::string_view &uri) noexcept
Returns true if the uri-scheme of given uri matches a supported by libcurl network protocols otherwis...
void PLAIN_PRINT(const bool printPrefix, const char *format,...) noexcept
Use for unconditional plain messages, prefix '[elapsed_time] ' if printPrefix == true.
constexpr std::string_view mini_httpd_exe
METHOD_AS_TEST_CASE(TestIOStream01::test00_protocols, "TestIOStream01 - test00_protocols")