...

Source file src/github.com/go-openapi/runtime/middleware/negotiate_test.go

Documentation: github.com/go-openapi/runtime/middleware

     1  // Copyright 2013 The Go Authors. All rights reserved.
     2  //
     3  // Use of this source code is governed by a BSD-style
     4  // license that can be found in the LICENSE file or at
     5  // https://developers.google.com/open-source/licenses/bsd.
     6  
     7  package middleware
     8  
     9  import (
    10  	"net/http"
    11  	"testing"
    12  )
    13  
    14  var negotiateContentEncodingTests = []struct {
    15  	s      string
    16  	offers []string
    17  	expect string
    18  }{
    19  	{"", []string{"identity", "gzip"}, "identity"},
    20  	{"*;q=0", []string{"identity", "gzip"}, ""},
    21  	{"gzip", []string{"identity", "gzip"}, "gzip"},
    22  }
    23  
    24  func TestNegotiateContentEnoding(t *testing.T) {
    25  	for _, tt := range negotiateContentEncodingTests {
    26  		r := &http.Request{Header: http.Header{"Accept-Encoding": {tt.s}}}
    27  		actual := NegotiateContentEncoding(r, tt.offers)
    28  		if actual != tt.expect {
    29  			t.Errorf("NegotiateContentEncoding(%q, %#v)=%q, want %q", tt.s, tt.offers, actual, tt.expect)
    30  		}
    31  	}
    32  }
    33  
    34  var negotiateContentTypeTests = []struct {
    35  	s            string
    36  	offers       []string
    37  	defaultOffer string
    38  	expect       string
    39  }{
    40  	{"text/html, */*;q=0", []string{"x/y"}, "", ""},
    41  	{"text/html, */*", []string{"x/y"}, "", "x/y"},
    42  	{"text/html, image/png", []string{"text/html", "image/png"}, "", "text/html"},
    43  	{"text/html, image/png", []string{"image/png", "text/html"}, "", "image/png"},
    44  	{"text/html, image/png; q=0.5", []string{"image/png"}, "", "image/png"},
    45  	{"text/html, image/png; q=0.5", []string{"text/html"}, "", "text/html"},
    46  	{"text/html, image/png; q=0.5", []string{"foo/bar"}, "", ""},
    47  	{"text/html, image/png; q=0.5", []string{"image/png", "text/html"}, "", "text/html"},
    48  	{"text/html, image/png; q=0.5", []string{"text/html", "image/png"}, "", "text/html"},
    49  	{"text/html;q=0.5, image/png", []string{"image/png"}, "", "image/png"},
    50  	{"text/html;q=0.5, image/png", []string{"text/html"}, "", "text/html"},
    51  	{"text/html;q=0.5, image/png", []string{"image/png", "text/html"}, "", "image/png"},
    52  	{"text/html;q=0.5, image/png", []string{"text/html", "image/png"}, "", "image/png"},
    53  	{"text/html;q=0.5, image/png", []string{"text/html", "image/png"}, "", "image/png"},
    54  	{"image/png, image/*;q=0.5", []string{"image/jpg", "image/png"}, "", "image/png"},
    55  	{"image/png, image/*;q=0.5", []string{"image/jpg"}, "", "image/jpg"},
    56  	{"image/png, image/*;q=0.5", []string{"image/jpg", "image/gif"}, "", "image/jpg"},
    57  	{"image/png, image/*", []string{"image/jpg", "image/gif"}, "", "image/jpg"},
    58  	{"image/png, image/*", []string{"image/gif", "image/jpg"}, "", "image/gif"},
    59  	{"image/png, image/*", []string{"image/gif", "image/png"}, "", "image/png"},
    60  	{"image/png, image/*", []string{"image/png", "image/gif"}, "", "image/png"},
    61  	{"application/vnd.google.protobuf;proto=io.prometheus.client.MetricFamily;encoding=delimited;q=0.7,text/plain;version=0.0.4;q=0.3", []string{"text/plain"}, "", "text/plain"},
    62  	{"application/json", []string{"application/json; charset=utf-8", "image/png"}, "", "application/json; charset=utf-8"},
    63  	{"application/json; charset=utf-8", []string{"application/json; charset=utf-8", "image/png"}, "", "application/json; charset=utf-8"},
    64  	{"application/json", []string{"application/vnd.cia.v1+json"}, "", ""},
    65  	// Default header of java clients
    66  	{"text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2", []string{"application/json"}, "", "application/json"},
    67  }
    68  
    69  func TestNegotiateContentType(t *testing.T) {
    70  	for _, tt := range negotiateContentTypeTests {
    71  		r := &http.Request{Header: http.Header{"Accept": {tt.s}}}
    72  		actual := NegotiateContentType(r, tt.offers, tt.defaultOffer)
    73  		if actual != tt.expect {
    74  			t.Errorf("NegotiateContentType(%q, %#v, %q)=%q, want %q", tt.s, tt.offers, tt.defaultOffer, actual, tt.expect)
    75  		}
    76  	}
    77  }
    78  
    79  func TestNegotiateContentTypeNoAcceptHeader(t *testing.T) {
    80  	r := &http.Request{Header: http.Header{}}
    81  	offers := []string{"application/json", "text/xml"}
    82  	actual := NegotiateContentType(r, offers, "")
    83  	if actual != "application/json" {
    84  		t.Errorf("NegotiateContentType(empty, %#v, empty)=%q, want %q", offers, actual, "application/json")
    85  	}
    86  }
    87  

View as plain text