Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Tensorflow Serving Http Rest Api Handler Test

From Leeroopedia
Knowledge Sources
Domains Testing, Model Serving API
Last Updated 2026-02-13 00:00 GMT

Overview

Test suite validating the HttpRestApiHandler component, which processes HTTP/REST requests for TensorFlow Serving model inference, status, and metadata endpoints.

Description

This test file exercises the HttpRestApiHandler class against a real ServerCore loaded with the "half_plus_two" test model. The fixture HttpRestApiHandlerTest sets up a ServerCore at the suite level with a specific model version and version label, then constructs the handler with an infinite timeout. Tests cover URL path regex validation, unsupported API call rejection, predict/regress/classify endpoints with valid and invalid inputs, model name and version error handling, model status retrieval, and model metadata queries.

Usage

Run these tests to validate that the HTTP REST API layer correctly routes requests, parses JSON payloads, produces correct inference results, and returns appropriate error messages for malformed or invalid requests.

Code Reference

Source Location

  • Repository: Tensorflow_Serving
  • File: tensorflow_serving/model_servers/http_rest_api_handler_test.cc
  • Lines: 1-536

Test Fixture

class HttpRestApiHandlerTest : public ::testing::Test {
 public:
  static void SetUpTestSuite() {
    TF_ASSERT_OK(CreateServerCore(&server_core_));
    const int total = 1;
    int count = 0;
    while ((count = server_core_->ListAvailableServableIds().size()) < total) {
      absl::SleepFor(absl::Milliseconds(500));
    }
  }
  static void TearDownTestSuite() { server_core_.reset(); }

 protected:
  HttpRestApiHandlerTest()
      : handler_(/*timeout_in_ms=*/-1, GetServerCore()) {}

  static absl::Status CreateServerCore(
      std::unique_ptr<ServerCore>* server_core);
  string GetJsonErrorMsg(const string& json);
  ServerCore* GetServerCore() { return server_core_.get(); }

  HttpRestApiHandler handler_;
 private:
  static std::unique_ptr<ServerCore> server_core_;
};

Build Target

bazel test //tensorflow_serving/model_servers:http_rest_api_handler_test

Test Coverage

Key Test Cases

Test Name Category Description
kPathRegex Routing Validates URL path regex matches expected model/version/verb patterns
UnsupportedApiCalls Error Handling Verifies unsupported API paths return appropriate errors
PredictModelNameVersionErrors Validation Tests error responses for invalid model names and versions
PredictRequestErrors Validation Tests error responses for malformed predict request bodies
Predict Inference Validates correct predict responses with the half_plus_two model
Regress Inference Validates correct regress responses with the half_plus_two model
Classify Inference Validates correct classify responses with the half_plus_two model
GetStatus Status Tests model status retrieval for specific versions and all versions
GetModelMetadata Metadata Tests model metadata retrieval including signature definitions

Usage Examples

Test Pattern

TEST_F(HttpRestApiHandlerTest, Predict) {
  HeaderList headers;
  string output;
  // Request body for predict endpoint
  TF_ASSERT_OK(handler_.ProcessRequest(
      "POST",
      absl::StrCat("/v1/models/", kTestModelName, "/versions/",
                    kTestModelVersion1, ":predict"),
      R"({"instances": [1.0, 2.0, 5.0]})",
      &headers, &output));
  // Verify output contains expected predictions
  EXPECT_THAT(output, HasSubstr("predictions"));
}

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment