Repository: envoyproxy/envoy
File: test/common/router/config_impl_test.cc (C++)

Code before:
  }
}

// Test the parsing of direct response configurations within routes.
TEST_F(RouteConfigurationV2, DirectResponse) {
  const std::string yaml = R"EOF(
virtual_hosts:
  - name: direct
    domains: [example.com]
    routes:
      - match: { prefix: "/"}
        direct_response: { status: 200, body: { inline_string: "content" } }
  )EOF";

  TestConfigImpl config(parseRouteConfigurationFromYaml(yaml), factory_context_, true,
                        creation_status_);

  const auto* direct_response =
      config.route(genHeaders("example.com", "/", "GET"), 0)->directResponseEntry();
  EXPECT_NE(nullptr, direct_response);
  EXPECT_EQ(Http::Code::OK, direct_response->responseCode());
  EXPECT_STREQ("content", direct_response->responseBody().c_str());
}

// Test the formatting of direct response bodies with formatting.
TEST_F(RouteConfigurationV2, DirectResponseBodyFormatting) {
  const std::string yaml = R"EOF(
virtual_hosts:
  - name: direct
    domains: [example.com]
    routes:
      - match: { prefix: "/"}
        direct_response:
          status: 200
          body: { inline_string: "inner" }
          body_format:
            text_format_source:
              inline_string: "prefix %LOCAL_REPLY_BODY% suffix"
)EOF";

  TestConfigImpl config(parseRouteConfigurationFromYaml(yaml), factory_context_, true,
                        creation_status_);

  const auto* direct_response =
      config.route(genHeaders("example.com", "/", "GET"), 0)->directResponseEntry();
  std::string body;
  Http::TestRequestHeaderMapImpl req_headers{{":method", "GET"}, {":path", "/"}};
  Http::TestResponseHeaderMapImpl resp_headers;
  NiceMock<Envoy::StreamInfo::MockStreamInfo> stream_info;
  EXPECT_NE(nullptr, direct_response);
  std::string formatted;

Review comment (suggestion):
I would say don't bother with this test - the integration test covers the exact same thing, and does it better by calling the external functions not the internal implementation function.

Code after:
virtual_hosts:
  - name: redirect
    domains: [redirect.lyft.com]
    routes:
      - match: { prefix: "/"}
        redirect: { host_redirect: new.lyft.com, response_code: TEMPORARY_REDIRECT }

  )EOF";

  TestConfigImpl config(parseRouteConfigurationFromYaml(yaml), factory_context_, true,
                        creation_status_);

  EXPECT_EQ(nullptr, config.route(genRedirectHeaders("www.foo.com", "/foo", true, true), 0).route);

  {
    Http::TestRequestHeaderMapImpl headers =
        genRedirectHeaders("redirect.lyft.com", "/foo", false, false);
    EXPECT_EQ("http://new.lyft.com/foo",
              config.route(headers, 0)->directResponseEntry()->newUri(headers));
    EXPECT_EQ(Http::Code::TemporaryRedirect,
              config.route(headers, 0)->directResponseEntry()->responseCode());
  }
}

// Test the parsing of direct response configurations within routes.
TEST_F(RouteConfigurationV2, DirectResponse) {
  const std::string yaml = R"EOF(
virtual_hosts:
  - name: direct
    domains: [example.com]
    routes:
      - match: { prefix: "/"}
        direct_response: { status: 200, body: { inline_string: "content" } }
  )EOF";

  TestConfigImpl config(parseRouteConfigurationFromYaml(yaml), factory_context_, true,
                        creation_status_);

  const auto* direct_response =
      config.route(genHeaders("example.com", "/", "GET"), 0)->directResponseEntry();
  Http::TestRequestHeaderMapImpl request_headers;
  Http::TestResponseHeaderMapImpl response_headers;
  NiceMock<Envoy::StreamInfo::MockStreamInfo> stream_info;
  std::string body;
  EXPECT_NE(nullptr, direct_response);
  EXPECT_EQ(Http::Code::OK, direct_response->responseCode());
  EXPECT_EQ("content",
            direct_response->formatBody(request_headers, response_headers, stream_info, body));
}

// Test the parsing of a direct response configuration where the response body is too large.