Skip to content

Commit

Permalink
Fix server URLs
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Carroll <[email protected]>
  • Loading branch information
mjcarroll committed Sep 20, 2023
1 parent fde7c6c commit aa8695c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 31 deletions.
12 changes: 10 additions & 2 deletions src/FuelClient.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,11 @@ bool FuelClient::ParseModelUrl(const common::URI &_modelUrl,
}

// Get remaining server information from config
_id.Server().SetUrl(common::URI(scheme + "://" + server));
common::URI serverUri;
serverUri.SetScheme(scheme);
serverUri.SetAuthority(gz::common::URIAuthority("//" + server));

_id.Server().SetUrl(serverUri);
_id.Server().SetVersion(apiVersion);
for (const auto &s : this->dataPtr->config.Servers())
{
Expand Down Expand Up @@ -1160,7 +1164,11 @@ bool FuelClient::ParseWorldUrl(const common::URI &_worldUrl,
}

// Get remaining server information from config
_id.Server().SetUrl(common::URI(scheme + "://" + server));
common::URI serverUri;
serverUri.SetScheme(scheme);
serverUri.SetAuthority(gz::common::URIAuthority("//" + server));

_id.Server().SetUrl(serverUri);
_id.Server().SetVersion(apiVersion);
for (const auto &s : this->dataPtr->config.Servers())
{
Expand Down
62 changes: 35 additions & 27 deletions src/FuelClient_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "gz/fuel_tools/FuelClient.hh"
#include "gz/fuel_tools/ClientConfig.hh"
#include "gz/fuel_tools/Helpers.hh"
#include "gz/fuel_tools/Result.hh"
#include "gz/fuel_tools/WorldIdentifier.hh"

Expand Down Expand Up @@ -82,7 +83,7 @@ void createLocalModel(ClientConfig &_conf)
}

gz::fuel_tools::ServerConfig srv;
srv.SetUrl(common::URI("http://localhost:8007/"));
srv.SetUrl(common::URI("http://localhost:8007/", true));
_conf.AddServer(srv);
}

Expand Down Expand Up @@ -113,7 +114,7 @@ void createLocalWorld(ClientConfig &_conf)
}

gz::fuel_tools::ServerConfig srv;
srv.SetUrl(gz::common::URI("http://localhost:8007/"));
srv.SetUrl(gz::common::URI("http://localhost:8007/", true));
_conf.AddServer(srv);
}

Expand Down Expand Up @@ -697,66 +698,69 @@ TEST_F(FuelClientTest, CachedModel)
FuelClient client(config);
EXPECT_EQ(config.CacheLocation(), client.Config().CacheLocation());

auto basePath = common::joinPaths(common::cwd(), "test_cache",
sanitizeAuthority("localhost:8007"));

// Cached model (no version)
{
common::URI url{"http://localhost:8007/1.0/alice/models/My Model"};
common::URI url{"http://localhost:8007/1.0/alice/models/My Model", true};
std::string path;
auto result = client.CachedModel(url, path);
EXPECT_TRUE(result);
EXPECT_EQ(ResultType::FETCH_ALREADY_EXISTS, result.Type());
EXPECT_EQ(common::joinPaths(common::cwd(), "test_cache", "localhost:8007",
EXPECT_EQ(common::joinPaths(basePath,
"alice", "models", "My Model", "3"), path);
}

// Cached model (tip)
{
common::URI url{"http://localhost:8007/1.0/alice/models/My Model/tip"};
common::URI url{"http://localhost:8007/1.0/alice/models/My Model/tip", true};
std::string path;
auto result = client.CachedModel(url, path);
EXPECT_TRUE(result);
EXPECT_EQ(ResultType::FETCH_ALREADY_EXISTS, result.Type());
EXPECT_EQ(common::joinPaths(common::cwd(), "test_cache", "localhost:8007",
EXPECT_EQ(common::joinPaths(basePath,
"alice", "models", "My Model", "3"), path);
}

// Cached model (version number)
{
common::URI url{"http://localhost:8007/1.0/alice/models/My Model/2"};
common::URI url{"http://localhost:8007/1.0/alice/models/My Model/2", true};
std::string path;
auto result = client.CachedModel(url, path);
EXPECT_TRUE(result);
EXPECT_EQ(ResultType::FETCH_ALREADY_EXISTS, result.Type());
EXPECT_EQ(common::joinPaths(common::cwd(), "test_cache", "localhost:8007",
EXPECT_EQ(common::joinPaths(basePath,
"alice", "models", "My Model", "2"), path);
}

// Cached model file (tip)
{
common::URI url{
"http://localhost:8007/1.0/alice/models/My Model/tip/files/model.sdf"};
"http://localhost:8007/1.0/alice/models/My Model/tip/files/model.sdf", true};
std::string path;
auto result = client.CachedModelFile(url, path);
EXPECT_TRUE(result);
EXPECT_EQ(ResultType::FETCH_ALREADY_EXISTS, result.Type());
EXPECT_EQ(common::joinPaths(common::cwd(), "test_cache", "localhost:8007",
EXPECT_EQ(common::joinPaths(basePath,
"alice", "models", "My Model", "3", "model.sdf"), path);
}

// Deeper cached model file
{
common::URI url{"http://localhost:8007/1.0/alice/models/My Model/2/files/"
"meshes/model.dae"};
"meshes/model.dae", true};
std::string path;
auto result = client.CachedModelFile(url, path);
EXPECT_TRUE(result);
EXPECT_EQ(ResultType::FETCH_ALREADY_EXISTS, result.Type());
EXPECT_EQ(common::joinPaths(common::cwd(), "test_cache", "localhost:8007",
EXPECT_EQ(common::joinPaths(basePath,
"alice", "models", "My Model", "2", "meshes", "model.dae"), path);
}

// Non-cached model
{
common::URI url{"http://localhost:8007/1.0/alice/models/Banana"};
common::URI url{"http://localhost:8007/1.0/alice/models/Banana", true};
std::string path;
auto result = client.CachedModel(url, path);
EXPECT_FALSE(result);
Expand All @@ -765,7 +769,7 @@ TEST_F(FuelClientTest, CachedModel)

// Non-cached model (when looking for file)
{
common::URI url{"http://localhost:8007/1.0/alice/models/Banana/model.sdf"};
common::URI url{"http://localhost:8007/1.0/alice/models/Banana/model.sdf", true};
std::string path;
auto result = client.CachedModelFile(url, path);
EXPECT_FALSE(result);
Expand All @@ -775,7 +779,7 @@ TEST_F(FuelClientTest, CachedModel)
// Non-cached model file
{
common::URI url{"http://localhost:8007/1.0/alice/models/My Model/tip/files/"
"meshes/banana.dae"
"meshes/banana.dae", true
};
std::string path;
auto result = client.CachedModelFile(url, path);
Expand All @@ -785,7 +789,7 @@ TEST_F(FuelClientTest, CachedModel)

// Model root URL to model file
{
common::URI url{"http://localhost:8007/1.0/alice/models/My Model"};
common::URI url{"http://localhost:8007/1.0/alice/models/My Model", true};
std::string path;
auto result = client.CachedModelFile(url, path);
EXPECT_FALSE(result);
Expand Down Expand Up @@ -1158,15 +1162,18 @@ TEST_F(FuelClientTest, CachedWorld)
FuelClient client(config);
EXPECT_EQ(config.CacheLocation(), client.Config().CacheLocation());

auto basePath = common::joinPaths(common::cwd(), "test_cache",
sanitizeAuthority("localhost:8007"));

// Cached world (no version)
{
common::URI url{"http://localhost:8007/1.0/banana/worlds/My World"};
std::string path;
auto result = client.CachedWorld(url, path);
EXPECT_TRUE(result);
EXPECT_EQ(ResultType::FETCH_ALREADY_EXISTS, result.Type());
EXPECT_EQ(common::joinPaths(common::cwd(), "test_cache",
"localhost:8007", "banana", "worlds", "My World", "3"), path);
EXPECT_EQ(common::joinPaths(basePath,
"banana", "worlds", "My World", "3"), path);
}

// Cached world (tip)
Expand All @@ -1176,8 +1183,8 @@ TEST_F(FuelClientTest, CachedWorld)
auto result = client.CachedWorld(url, path);
EXPECT_TRUE(result);
EXPECT_EQ(ResultType::FETCH_ALREADY_EXISTS, result.Type());
EXPECT_EQ(common::joinPaths(common::cwd(), "test_cache",
"localhost:8007", "banana", "worlds", "My World", "3"), path);
EXPECT_EQ(common::joinPaths(basePath,
"banana", "worlds", "My World", "3"), path);
}

// Cached world (version number)
Expand All @@ -1187,8 +1194,8 @@ TEST_F(FuelClientTest, CachedWorld)
auto result = client.CachedWorld(url, path);
EXPECT_TRUE(result);
EXPECT_EQ(ResultType::FETCH_ALREADY_EXISTS, result.Type());
EXPECT_EQ(common::joinPaths(common::cwd(), "test_cache",
"localhost:8007", "banana", "worlds", "My World", "2"), path);
EXPECT_EQ(common::joinPaths(basePath,
"banana", "worlds", "My World", "2"), path);
}

// Cached world file (tip)
Expand All @@ -1199,8 +1206,8 @@ TEST_F(FuelClientTest, CachedWorld)
auto result = client.CachedWorldFile(url, path);
EXPECT_TRUE(result);
EXPECT_EQ(ResultType::FETCH_ALREADY_EXISTS, result.Type());
EXPECT_EQ(common::joinPaths(common::cwd(), "test_cache",
"localhost:8007", "banana", "worlds", "My World", "3",
EXPECT_EQ(common::joinPaths(basePath,
"banana", "worlds", "My World", "3",
"strawberry.world"), path);
}

Expand All @@ -1212,8 +1219,8 @@ TEST_F(FuelClientTest, CachedWorld)
auto result = client.CachedWorldFile(url, path);
EXPECT_TRUE(result);
EXPECT_EQ(ResultType::FETCH_ALREADY_EXISTS, result.Type());
EXPECT_EQ(common::joinPaths(common::cwd(), "test_cache",
"localhost:8007", "banana", "worlds", "My World", "2",
EXPECT_EQ(common::joinPaths(basePath,
"banana", "worlds", "My World", "2",
"strawberry.world"), path);
}

Expand Down Expand Up @@ -1498,7 +1505,8 @@ TEST_F(FuelClientTest, PatchModelFail)

// Bad model.config
result = client.PatchModel(modelId, headers,
common::joinPaths(common::cwd(), "test_cache", "localhost:8007",
common::joinPaths(common::cwd(), "test_cache",
sanitizeAuthority("localhost:8007"),
"alice", "models", "My Model", "3"));
EXPECT_EQ(ResultType::UPLOAD_ERROR, result.Type());

Expand Down
4 changes: 4 additions & 0 deletions src/LocalCache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,13 @@ ModelIter LocalCache::AllModels()
{
for (auto &server : this->dataPtr->config->Servers())
{
auto uri = server.Url();

std::string path = common::joinPaths(
this->dataPtr->config->CacheLocation(), uriToPath(server.Url()));

auto srvModels = this->dataPtr->ModelsInServer(path);

for (auto &mod : srvModels)
{
mod.dataPtr->id.SetServer(server);
Expand Down Expand Up @@ -284,6 +287,7 @@ Model LocalCache::MatchingModel(const ModelIdentifier &_id)
for (ModelIter iter = this->AllModels(); iter; ++iter)
{
ModelIdentifier id = iter->Identification();

if (_id == id)
{
if (_id.Version() == id.Version())
Expand Down
3 changes: 1 addition & 2 deletions src/WorldIdentifier.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ WorldIdentifier::~WorldIdentifier()
//////////////////////////////////////////////////
std::string WorldIdentifier::UniqueName() const
{
return common::joinPaths(uriToPath(this->dataPtr->server.Url()),
return common::joinPaths(this->dataPtr->server.Url().Str(),
this->dataPtr->owner,
"worlds",
this->dataPtr->name);
Expand Down Expand Up @@ -228,4 +228,3 @@ std::string WorldIdentifier::AsPrettyString(const std::string &_prefix) const
<< this->Server().AsPrettyString(_prefix + " ");
return out.str();
}

0 comments on commit aa8695c

Please sign in to comment.