...

Source file src/github.com/go-openapi/runtime/headers_test.go

Documentation: github.com/go-openapi/runtime

     1  // Copyright 2015 go-swagger maintainers
     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 runtime
    16  
    17  import (
    18  	"mime"
    19  	"net/http"
    20  	"testing"
    21  
    22  	"github.com/go-openapi/errors"
    23  	"github.com/stretchr/testify/assert"
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  func TestParseContentType(t *testing.T) {
    28  	_, _, reason1 := mime.ParseMediaType("application(")
    29  	_, _, reason2 := mime.ParseMediaType("application/json;char*")
    30  	data := []struct {
    31  		hdr, mt, cs string
    32  		err         *errors.ParseError
    33  	}{
    34  		{"application/json", "application/json", "", nil},
    35  		{"text/html; charset=utf-8", "text/html", "utf-8", nil},
    36  		{"text/html;charset=utf-8", "text/html", "utf-8", nil},
    37  		{"", "application/octet-stream", "", nil},
    38  		{"text/html;           charset=utf-8", "text/html", "utf-8", nil},
    39  		{"application(", "", "", errors.NewParseError("Content-Type", "header", "application(", reason1)},
    40  		{"application/json;char*", "", "", errors.NewParseError("Content-Type", "header", "application/json;char*", reason2)},
    41  	}
    42  
    43  	headers := http.Header(map[string][]string{})
    44  	for _, v := range data {
    45  		if v.hdr != "" {
    46  			headers.Set("Content-Type", v.hdr)
    47  		} else {
    48  			headers.Del("Content-Type")
    49  		}
    50  		ct, cs, err := ContentType(headers)
    51  		if v.err == nil {
    52  			require.NoError(t, err, "input: %q, err: %v", v.hdr, err)
    53  		} else {
    54  			require.Error(t, err, "input: %q", v.hdr)
    55  			assert.IsType(t, &errors.ParseError{}, err, "input: %q", v.hdr)
    56  			assert.Equal(t, v.err.Error(), err.Error(), "input: %q", v.hdr)
    57  		}
    58  		assert.Equal(t, v.mt, ct, "input: %q", v.hdr)
    59  		assert.Equal(t, v.cs, cs, "input: %q", v.hdr)
    60  	}
    61  }
    62  

View as plain text