Skip to content

Commit

Permalink
tests(grpc): use new EchoHeaders rpc in tests
Browse files Browse the repository at this point in the history
Replace template-based mock grpc service with actual gRPC service to
test for `:authority` headers.
  • Loading branch information
gszr committed Dec 23, 2024
1 parent bbde4b0 commit 2b3dcff
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 154 deletions.
52 changes: 20 additions & 32 deletions spec/02-integration/05-proxy/19-grpc_proxy_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ for _, strategy in helpers.each_strategy() do
reload_router(flavor)

lazy_setup(function()
assert(helpers.start_grpc_target())

local bp = helpers.get_db_utils(strategy, {
"routes",
"services",
Expand All @@ -47,12 +49,16 @@ for _, strategy in helpers.each_strategy() do

local mock_grpc_service = assert(bp.services:insert {
name = "mock_grpc_service",
url = "grpc://localhost:8765",
protocol = "grpc",
host = "127.0.0.1",
port = helpers.get_grpc_target_port(),
})

local mock_grpc_service_retry = assert(bp.services:insert {
name = "mock_grpc_service_retry",
url = "grpc://grpc_retry",
protocol = "grpc",
host = "127.0.0.1",
port = helpers.get_grpc_target_port(),
})

local upstream_retry = assert(bp.upstreams:insert {
Expand Down Expand Up @@ -111,30 +117,10 @@ for _, strategy in helpers.each_strategy() do
},
})

local fixtures = {
http_mock = {}
}

fixtures.http_mock.my_server_block = [[
server {
server_name myserver;
listen 8765;
http2 on;
location ~ / {
content_by_lua_block {
ngx.header.content_type = "application/grpc"
ngx.header.received_host = ngx.req.get_headers()["Host"]
}
}
}
]]

assert(helpers.start_kong({
router_flavor = flavor,
database = strategy,
nginx_conf = "spec/fixtures/custom_nginx.template",
}, nil, nil, fixtures))
}))

proxy_client_grpc = helpers.proxy_client_grpc()
proxy_client_grpcs = helpers.proxy_client_grpcs()
Expand All @@ -150,6 +136,7 @@ for _, strategy in helpers.each_strategy() do

lazy_teardown(function()
helpers.stop_kong()
helpers.stop_grpc_target()
end)

it("proxies grpc", function()
Expand Down Expand Up @@ -228,32 +215,33 @@ for _, strategy in helpers.each_strategy() do

it("proxies :authority header if `preserve_host` is set", function()
local _, resp = proxy_client_grpc({
service = "hello.HelloService.SayHello",
service = "targetservice.Bouncer.EchoHeaders",
body = {
greeting = "world!"
},
opts = {
["-proto"] = "./spec/fixtures/grpc/proto/targetservice.proto",
["-import-path"] = "./spec/fixtures/grpc/proto",
["-authority"] = "grpc_authority_1.example",
["-v"] = true,
}
})

assert.matches("received%-host: grpc_authority_1.example", resp)
local headers = cjson.decode(resp).headers
assert.matches("grpc_authority_1.example", headers[":authority"])
end)

it("sets default :authority header if `preserve_host` isn't set", function()
local _, resp = proxy_client_grpc({
service = "hello.HelloService.SayHello",
service = "targetservice.Bouncer.EchoHeaders",
body = {
greeting = "world!"
},
opts = {
["-proto"] = "./spec/fixtures/grpc/proto/targetservice.proto",
["-import-path"] = "./spec/fixtures/grpc/proto",
["-authority"] = "grpc_authority_2.example",
["-v"] = true,
}
})

assert.matches("received%-host: localhost:8765", resp)
local headers = cjson.decode(resp).headers
assert.matches("127.0.0.1:15010", headers[":authority"])
end)

it("proxies :authority header on balancer retry", function()
Expand Down
7 changes: 1 addition & 6 deletions spec/fixtures/grpc/proto/targetservice.proto
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,8 @@ message EchoMsg {
string nullable = 2;
}

message Header {
string key = 1;
string value = 2;
}

message Headers {
repeated Header headers = 1;
map<string, string> headers = 1;
}

message Void {}
8 changes: 3 additions & 5 deletions spec/fixtures/grpc/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,11 @@ func (s *server) EchoHeaders(ctx context.Context, in *pb.Void) (*pb.Headers, err
return nil, status.Errorf(codes.DataLoss, "UnaryEcho: failed to get metadata")
}
headers := &pb.Headers{}
var headersMap = make(map[string]string, len(md))
for k, v := range md {
header := &pb.Header{
Key: k,
Value: strings.Join(v, ", "),
}
headers.Headers = append(headers.Headers, header)
headersMap[k] = strings.Join(v, ", ")
}
headers.Headers = headersMap
return headers, nil
}

Expand Down
172 changes: 61 additions & 111 deletions spec/fixtures/grpc/targetservice/targetservice.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2b3dcff

Please sign in to comment.