1 //
2 //
3 // Copyright 2015 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18
19 #include <memory>
20
21 #include "absl/types/optional.h"
22 #include "gmock/gmock.h"
23 #include "gtest/gtest.h"
24
25 #include <grpc/status.h>
26
27 #include "src/core/lib/gprpp/time.h"
28 #include "test/core/end2end/end2end_tests.h"
29
30 using testing::AnyOf;
31 using testing::StartsWith;
32
33 namespace grpc_core {
34 namespace {
35
CORE_END2END_TEST(CoreClientChannelTest,DefaultHost)36 CORE_END2END_TEST(CoreClientChannelTest, DefaultHost) {
37 auto c = NewClientCall("/foo").Timeout(Duration::Seconds(5)).Create();
38 EXPECT_NE(c.GetPeer(), absl::nullopt);
39 IncomingStatusOnClient server_status;
40 IncomingMetadata server_initial_metadata;
41 c.NewBatch(1)
42 .SendInitialMetadata({})
43 .SendCloseFromClient()
44 .RecvInitialMetadata(server_initial_metadata)
45 .RecvStatusOnClient(server_status);
46 auto s = RequestCall(101);
47 Expect(101, true);
48 Step();
49 EXPECT_NE(s.GetPeer(), absl::nullopt);
50 EXPECT_NE(c.GetPeer(), absl::nullopt);
51 IncomingCloseOnServer client_close;
52 s.NewBatch(102)
53 .SendInitialMetadata({})
54 .SendStatusFromServer(GRPC_STATUS_UNIMPLEMENTED, "xyz", {})
55 .RecvCloseOnServer(client_close);
56 Expect(102, true);
57 Expect(1, true);
58 Step();
59 EXPECT_EQ(server_status.status(), GRPC_STATUS_UNIMPLEMENTED);
60 EXPECT_EQ(server_status.message(), "xyz");
61 EXPECT_EQ(s.method(), "/foo");
62 if (GetParam()->overridden_call_host != nullptr) {
63 EXPECT_EQ(GetParam()->overridden_call_host, s.host());
64 } else {
65 EXPECT_THAT(s.host(),
66 AnyOf(StartsWith("localhost"), StartsWith("127.0.0.1"),
67 StartsWith("[::1]"), StartsWith("grpc_fullstack_test."),
68 StartsWith("tmp%2Fgrpc_fullstack_test."),
69 StartsWith("C:%2Ftmp%2Fgrpc_fullstack_test.")));
70 }
71 EXPECT_FALSE(client_close.was_cancelled());
72 }
73
74 } // namespace
75 } // namespace grpc_core
76