Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 59 additions & 3 deletions source/MRTest/MRCPRTests.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,42 @@
#include "MRMesh/MRMeshFwd.h"
#if !defined( __EMSCRIPTEN__)
#include <cpr/cpr.h>
#include "MRPch/MRSpdlog.h"
#include "MRMesh/MRGTest.h"
#include <curl/curl.h>
#include <cpr/cpr.h>

constexpr int MAX_RETRIES = 10;
constexpr std::chrono::seconds COOLDOWN_PERIOD { 10 };

namespace
{
void logCurlInfo()
{
// Initialize libcurl
curl_global_init( CURL_GLOBAL_DEFAULT );

// Get version information
const curl_version_info_data* data = curl_version_info( CURLVERSION_NOW );

// Check if data is valid and print the version string
if ( data )
{
spdlog::info( "libcurl Version: {}", data->version );
}
else
{
spdlog::error( "Failed to get libcurl version information." );
}

// Clean up libcurl
curl_global_cleanup();
}
}

TEST( MRViewer, CPRTestGet )
{
logCurlInfo();

std::string baseUrl = "https://postman-echo.com/get";
std::vector<std::pair<std::string, std::string>> params = { {"1","1"} };

Expand All @@ -18,10 +46,23 @@ TEST( MRViewer, CPRTestGet )

for ( int i = 0; i < MAX_RETRIES; ++i )
{
auto code = cpr::Get( cpr::Url{ baseUrl }, cpr::Timeout{ 3000 }, parameters ).status_code;
cpr::Session session;
session.SetVerbose( cpr::Verbose( true ) );
session.SetDebugCallback( std::function{ []( cpr::DebugCallback::InfoType, std::string data, intptr_t )
{
spdlog::info( data );
} } );
//session.SetSslOptions( cpr::SslOptions{ .ssl_no_revoke = true } );
session.SetUrl( cpr::Url{ baseUrl } );
session.SetTimeout( cpr::Timeout{ 3000 } );
session.SetParameters( parameters );
const auto resp = session.Get();// cpr::Get( cpr::Url{ baseUrl }, cpr::Timeout{ 3000 }, parameters );
auto code = resp.status_code;
if ( code == 200 )
break;
spdlog::warn( "status code {} after try #{}", code, i + 1 );
if ( resp.error )
spdlog::warn( "curl error {}: {}", (int32_t)resp.error.code, resp.error.message );
if ( i + 1 == MAX_RETRIES )
{
EXPECT_EQ( code, 200 );
Expand All @@ -32,6 +73,8 @@ TEST( MRViewer, CPRTestGet )

TEST( MRViewer, CPRTestPost )
{
logCurlInfo();

std::string baseUrl = "https://postman-echo.com/post";
std::vector<std::pair<std::string, std::string>> params = { {"1","1"} };

Expand All @@ -44,10 +87,23 @@ TEST( MRViewer, CPRTestPost )

for ( int i = 0; i < MAX_RETRIES; ++i )
{
auto code = cpr::Post( cpr::Url{ baseUrl }, cpr::Timeout{ 3000 }, payload ).status_code;
cpr::Session session;
session.SetVerbose( cpr::Verbose( true ) );
session.SetDebugCallback( std::function{ []( cpr::DebugCallback::InfoType, std::string data, intptr_t )
{
spdlog::info( data );
} } );
//session.SetSslOptions( cpr::SslOptions{ .ssl_no_revoke = true } );
session.SetUrl( cpr::Url{ baseUrl } );
session.SetTimeout( cpr::Timeout{ 3000 } );
session.SetPayload( payload );
const auto resp = session.Post();// cpr::Post( cpr::Url{ baseUrl }, cpr::Timeout{ 3000 }, payload );
auto code = resp.status_code;
if ( code == 200 )
break;
spdlog::warn( "status code {} after try #{}", code, i + 1 );
if ( resp.error )
spdlog::warn( "curl error {}: {}", (int32_t)resp.error.code, resp.error.message );
if ( i + 1 == MAX_RETRIES )
{
EXPECT_EQ( code, 200 );
Expand Down
Loading