Repository: envoyproxy/envoy
File: test/extensions/filters/http/common/jwks_fetcher_test.cc (C++)

Code before:
  ]
}
)";

const std::string config = R"(
http_uri:
  uri: https://pubkey_server/pubkey_path
  cluster: pubkey_cluster
  timeout:
    seconds: 5
)";

class JwksFetcherTest : public testing::Test {
public:
  void setupFetcher(const std::string& config_str) {
    TestUtility::loadFromYaml(config_str, remote_jwks_);
    mock_factory_ctx_.server_factory_context_.cluster_manager_.initializeThreadLocalClusters(
        {"pubkey_cluster"});

    Router::RetryPolicyConstSharedPtr retry_policy = nullptr;
    if (remote_jwks_.has_retry_policy()) {
      envoy::config::route::v3::RetryPolicy route_retry_policy =
          Http::Utility::convertCoreToRouteRetryPolicy(remote_jwks_.retry_policy(),
                                                       "5xx,gateway-error,connect-failure,reset");
      // Use the null validation visitor because it used by the async client in the previous
      // implementation.
      auto policy_or_error = Router::RetryPolicyImpl::create(
          route_retry_policy, ProtobufMessage::getNullValidationVisitor(),
          mock_factory_ctx_.server_factory_context_);
      THROW_IF_NOT_OK_REF(policy_or_error.status());
      retry_policy = std::move(policy_or_error.value());
    }

    fetcher_ = JwksFetcher::create(mock_factory_ctx_.server_factory_context_.cluster_manager_,
                                   retry_policy, remote_jwks_);
    EXPECT_TRUE(fetcher_ != nullptr);
  }

  RemoteJwks remote_jwks_;
  testing::NiceMock<Server::Configuration::MockFactoryContext> mock_factory_ctx_;
  std::unique_ptr<JwksFetcher> fetcher_;
  NiceMock<Tracing::MockSpan> parent_span_;
};

// Test findByIssuer
TEST_F(JwksFetcherTest, TestGetSuccess) {
  // Setup
  setupFetcher(config);
  MockUpstream mock_pubkey(mock_factory_ctx_.server_factory_context_.cluster_manager_, "200",
                           publicKey);
  MockJwksReceiver receiver;

Review comment (suggestion):
```suggestion
      // Use the null validation visitor because it was used by the async client in the previous
      // implementation.
```

Code after:
  ]
}
)";

const std::string config = R"(
http_uri:
  uri: https://pubkey_server/pubkey_path
  cluster: pubkey_cluster
  timeout:
    seconds: 5
)";

Router::RetryPolicyConstSharedPtr
getRetryPolicy(const RemoteJwks& remote_jwks,
               Server::Configuration::CommonFactoryContext& context) {
  if (remote_jwks.has_retry_policy()) {
    envoy::config::route::v3::RetryPolicy route_retry_policy =
        Http::Utility::convertCoreToRouteRetryPolicy(remote_jwks.retry_policy(),
                                                     "5xx,gateway-error,connect-failure,reset");
    // Use the null validation visitor because it was used by the async client in the previous
    // implementation.
    auto policy_or_error = Router::RetryPolicyImpl::create(
        route_retry_policy, ProtobufMessage::getNullValidationVisitor(), context);
    THROW_IF_NOT_OK_REF(policy_or_error.status());
    return std::move(policy_or_error.value());
  }
  return nullptr;
}

class JwksFetcherTest : public testing::Test {
public:
  void setupFetcher(const std::string& config_str) {
    TestUtility::loadFromYaml(config_str, remote_jwks_);
    mock_factory_ctx_.server_factory_context_.cluster_manager_.initializeThreadLocalClusters(
        {"pubkey_cluster"});

    fetcher_ = JwksFetcher::create(
        mock_factory_ctx_.server_factory_context_.cluster_manager_,
        getRetryPolicy(remote_jwks_, mock_factory_ctx_.server_factory_context_), remote_jwks_);
    EXPECT_TRUE(fetcher_ != nullptr);
  }

  RemoteJwks remote_jwks_;
  testing::NiceMock<Server::Configuration::MockFactoryContext> mock_factory_ctx_;
  std::unique_ptr<JwksFetcher> fetcher_;
  NiceMock<Tracing::MockSpan> parent_span_;
};

// Test findByIssuer
TEST_F(JwksFetcherTest, TestGetSuccess) {
  // Setup