-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
client: set grpc-accept-encoding header with all registered compressors
- Loading branch information
Showing
5 changed files
with
211 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* | ||
* Copyright 2022 gRPC authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package grpcutil | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestAdvertisedCompressors(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
compressors []string | ||
additionalCompressor string | ||
want string | ||
}{ | ||
{ | ||
name: "no_registered_compressors", | ||
}, | ||
{ | ||
name: "with_registered_compressors", | ||
compressors: []string{"gzip", "snappy"}, | ||
want: "identity,gzip,snappy", | ||
}, | ||
{ | ||
name: "with_additional_compressors", | ||
additionalCompressor: "gzip", | ||
want: "identity,gzip", | ||
}, | ||
{ | ||
name: "with_registered_and_additional_compressors", | ||
compressors: []string{"gzip", "snappy"}, | ||
additionalCompressor: "test", | ||
want: "identity,gzip,snappy,test", | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
RegisteredCompressorNames = test.compressors | ||
t.Cleanup(func() { RegisteredCompressorNames = []string{} }) | ||
|
||
if got := AdvertisedCompressors(test.additionalCompressor); got != test.want { | ||
t.Fatalf("got \"%s\", want \"%s\"", got, test.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* | ||
* Copyright 2022 gRPC authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package grpcutil | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
// RegisteredCompressorNames holds names of the registered compressors. | ||
var RegisteredCompressorNames []string | ||
|
||
// IsCompressorNameRegistered returns true when name is available in registry. | ||
func IsCompressorNameRegistered(name string) bool { | ||
for _, compressor := range RegisteredCompressorNames { | ||
if compressor == name { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// AdvertisedCompressors returns a string of registered compressor names | ||
// and provided additional compressor separated by comma. | ||
func AdvertisedCompressors(additionalCompressor string) string { | ||
if len(RegisteredCompressorNames) == 0 && additionalCompressor == "" { | ||
return "" | ||
} | ||
|
||
b := strings.Builder{} | ||
b.WriteString("identity") | ||
for _, compressor := range RegisteredCompressorNames { | ||
b.WriteRune(',') | ||
b.WriteString(compressor) | ||
} | ||
if additionalCompressor != "" { | ||
b.WriteRune(',') | ||
b.WriteString(additionalCompressor) | ||
} | ||
return b.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters