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

Code before:
    EXPECT_DEATH(callback->onFailure(request, Http::AsyncClient::FailureReason::Reset),
                 "Malformed oauth client state");
  }));
}

TEST_F(OAuth2ClientTest, NoCluster) {
  ON_CALL(cm_, getThreadLocalCluster("auth")).WillByDefault(Return(nullptr));
  client_->setCallbacks(*mock_callbacks_);
  EXPECT_CALL(*mock_callbacks_, sendUnauthorizedResponse(_));
  client_->asyncGetAccessToken("a", "b", "c", "d", "e");
  EXPECT_EQ(0, callbacks_.size());
}

TEST_F(OAuth2ClientTest, RequestAccessTokenRetryPolicy) {
  envoy::config::core::v3::HttpUri uri;
  uri.set_cluster("auth");
  uri.set_uri("auth.com/oauth/token");
  uri.mutable_timeout()->set_seconds(1);

  envoy::config::route::v3::RetryPolicy retry_policy;
  retry_policy.set_retry_on("5xx,reset");
  retry_policy.mutable_retry_back_off()->mutable_base_interval()->set_seconds(1);
  retry_policy.mutable_retry_back_off()->mutable_max_interval()->set_seconds(10);
  retry_policy.mutable_num_retries()->set_value(5);

  testing::NiceMock<Server::Configuration::MockServerFactoryContext> server_factory_context_;
  auto parsed_retry_policy = Router::RetryPolicyImpl::create(
      retry_policy, ProtobufMessage::getNullValidationVisitor(), server_factory_context_);

  client_ =
      std::make_shared<OAuth2ClientImpl>(cm_, uri, std::move(parsed_retry_policy.value()), 2000s);

  EXPECT_CALL(cm_.thread_local_cluster_.async_client_, send_(_, _, _))
      .WillOnce(Invoke(
          [&](Http::RequestMessagePtr&, Http::AsyncClient::Callbacks&,
              const Http::AsyncClient::RequestOptions& options) -> Http::AsyncClient::Request* {
            EXPECT_TRUE(options.parsed_retry_policy != nullptr);
            EXPECT_TRUE(options.buffer_body_for_retry);
            EXPECT_EQ(options.parsed_retry_policy->numRetries(), 5);
            EXPECT_TRUE(options.parsed_retry_policy->baseInterval().has_value());
            EXPECT_TRUE(options.parsed_retry_policy->maxInterval().has_value());
            EXPECT_EQ(options.parsed_retry_policy->baseInterval().value().count(), 1 * 1000);
            EXPECT_EQ(options.parsed_retry_policy->maxInterval().value().count(), 10 * 1000);
            const auto retry_on = options.parsed_retry_policy->retryOn();
            EXPECT_TRUE(retry_on | Router::RetryPolicy::RETRY_ON_5XX);
            EXPECT_TRUE(retry_on | Router::RetryPolicy::RETRY_ON_RESET);
            return nullptr;
          }));

  client_->setCallbacks(*mock_callbacks_);
  client_->asyncGetAccessToken("a", "b", "c", "d", "e");

Review comment (suggestion):
```suggestion
  testing::NiceMock<Server::Configuration::MockServerFactoryContext> server_factory_context;
```

Code after:
    EXPECT_DEATH(callback->onFailure(request, Http::AsyncClient::FailureReason::Reset),
                 "Malformed oauth client state");
  }));
}

TEST_F(OAuth2ClientTest, NoCluster) {
  ON_CALL(cm_, getThreadLocalCluster("auth")).WillByDefault(Return(nullptr));
  client_->setCallbacks(*mock_callbacks_);
  EXPECT_CALL(*mock_callbacks_, sendUnauthorizedResponse(_));
  client_->asyncGetAccessToken("a", "b", "c", "d", "e");
  EXPECT_EQ(0, callbacks_.size());
}

TEST_F(OAuth2ClientTest, RequestAccessTokenRetryPolicy) {
  envoy::config::core::v3::HttpUri uri;
  uri.set_cluster("auth");
  uri.set_uri("auth.com/oauth/token");
  uri.mutable_timeout()->set_seconds(1);

  envoy::config::route::v3::RetryPolicy retry_policy;
  retry_policy.set_retry_on("5xx,reset");
  retry_policy.mutable_retry_back_off()->mutable_base_interval()->set_seconds(1);
  retry_policy.mutable_retry_back_off()->mutable_max_interval()->set_seconds(10);
  retry_policy.mutable_num_retries()->set_value(5);

  testing::NiceMock<Server::Configuration::MockServerFactoryContext> server_factory_context;
  auto parsed_retry_policy = Router::RetryPolicyImpl::create(
      retry_policy, ProtobufMessage::getNullValidationVisitor(), server_factory_context);

  client_ =
      std::make_shared<OAuth2ClientImpl>(cm_, uri, std::move(parsed_retry_policy.value()), 2000s);

  EXPECT_CALL(cm_.thread_local_cluster_.async_client_, send_(_, _, _))
      .WillOnce(Invoke(
          [&](Http::RequestMessagePtr&, Http::AsyncClient::Callbacks&,
              const Http::AsyncClient::RequestOptions& options) -> Http::AsyncClient::Request* {
            EXPECT_TRUE(options.parsed_retry_policy != nullptr);
            EXPECT_TRUE(options.buffer_body_for_retry);
            EXPECT_EQ(options.parsed_retry_policy->numRetries(), 5);
            EXPECT_TRUE(options.parsed_retry_policy->baseInterval().has_value());
            EXPECT_TRUE(options.parsed_retry_policy->maxInterval().has_value());
            EXPECT_EQ(options.parsed_retry_policy->baseInterval().value().count(), 1 * 1000);
            EXPECT_EQ(options.parsed_retry_policy->maxInterval().value().count(), 10 * 1000);
            const auto retry_on = options.parsed_retry_policy->retryOn();
            EXPECT_TRUE(retry_on & Router::RetryPolicy::RETRY_ON_5XX);
            EXPECT_TRUE(retry_on & Router::RetryPolicy::RETRY_ON_RESET);
            return nullptr;
          }));

  client_->setCallbacks(*mock_callbacks_);
  client_->asyncGetAccessToken("a", "b", "c", "d", "e");