go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/grpc/prpc/format_test.go (about)

     1  // Copyright 2016 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package prpc
    16  
    17  import (
    18  	"testing"
    19  
    20  	. "github.com/smartystreets/goconvey/convey"
    21  	. "go.chromium.org/luci/common/testing/assertions"
    22  )
    23  
    24  func TestFormat(t *testing.T) {
    25  	Convey("requestFormat", t, func() {
    26  		test := func(contentType string, expectedFormat Format, expectedErr any) {
    27  			Convey("Content-Type: "+contentType, func() {
    28  				actualFormat, err := FormatFromContentType(contentType)
    29  				So(err, ShouldErrLike, expectedErr)
    30  				if err == nil {
    31  					So(actualFormat, ShouldEqual, expectedFormat)
    32  				}
    33  			})
    34  		}
    35  
    36  		test("", FormatBinary, nil)
    37  		test(ContentTypePRPC, FormatBinary, nil)
    38  		test(mtPRPCBinary, FormatBinary, nil)
    39  		test(mtPRPCJSONPBLegacy, FormatJSONPB, nil)
    40  		test(mtPRPCText, FormatText, nil)
    41  		test(
    42  			ContentTypePRPC+"; encoding=blah",
    43  			0,
    44  			`invalid encoding parameter: "blah". Valid values: "binary", "json", "text"`)
    45  		test(ContentTypePRPC+"; boo=true", 0, `unexpected parameter "boo"`)
    46  
    47  		test(ContentTypeJSON, FormatJSONPB, nil)
    48  		test(ContentTypeJSON+"; whatever=true", FormatJSONPB, nil)
    49  
    50  		test("x", 0, `unknown content type: "x"`)
    51  		test("x,y", 0, "mime: expected slash after first token")
    52  	})
    53  }