diff --git a/example_test.go b/example_test.go index fa8bc34..7cfd377 100644 --- a/example_test.go +++ b/example_test.go @@ -10,7 +10,7 @@ import ( func ExampleNegotiator_Accept() { req := httptest.NewRequest(http.MethodGet, "/", nil) - req.Header.Set(negotiator.HeaderAccept, "text/html, application/*;q=0.9, image/jpeg;q=0.8") + req.Header.Set("Accept", "text/html, application/*;q=0.9, image/jpeg;q=0.8") negotiator := negotiator.New(req) fmt.Println(negotiator.Accept([]string{"text/html", "application/json", "image/jpeg"})) @@ -25,7 +25,7 @@ func ExampleNegotiator_Accept() { func ExampleNegotiator_Encoding() { req := httptest.NewRequest(http.MethodGet, "/", nil) - req.Header.Set(negotiator.HeaderAcceptEncoding, "gzip, compress;q=0.2, identity;q=0.5") + req.Header.Set("Accept-Encoding", "gzip, compress;q=0.2, identity;q=0.5") negotiator := negotiator.New(req) fmt.Println(negotiator.Encoding([]string{"identity", "gzip"})) @@ -37,7 +37,7 @@ func ExampleNegotiator_Encoding() { func ExampleNegotiator_Language() { req := httptest.NewRequest(http.MethodGet, "/", nil) - req.Header.Set(negotiator.HeaderAcceptLanguage, "en;q=0.8, es, pt") + req.Header.Set("Accept-Language", "en;q=0.8, es, pt") negotiator := negotiator.New(req) fmt.Println(negotiator.Language([]string{"en", "es", "fr"})) @@ -49,7 +49,7 @@ func ExampleNegotiator_Language() { func ExampleNegotiator_Charset() { req := httptest.NewRequest(http.MethodGet, "/", nil) - req.Header.Set(negotiator.HeaderAcceptCharset, "utf-8, iso-8859-1;q=0.8, utf-7;q=0.2") + req.Header.Set("Accept-Language", "utf-8, iso-8859-1;q=0.8, utf-7;q=0.2") negotiator := negotiator.New(req) fmt.Println(negotiator.Charset([]string{"UTF-8", "ISO-8859-1", "ISO-8859-5"})) diff --git a/negotiator.go b/negotiator.go index 6ec2669..c711264 100644 --- a/negotiator.go +++ b/negotiator.go @@ -3,19 +3,13 @@ package negotiator import ( "net/http" "strings" -) -const ( - // HeaderAccept is the HTTP "Accept" Header. - HeaderAccept = "Accept" - // HeaderAcceptLanguage is the HTTP "Accept-Language" Header. - HeaderAcceptLanguage = "Accept-Language" - // HeaderAcceptEncoding is the HTTP "Accept-Encoding" Header. - HeaderAcceptEncoding = "Accept-Encoding" - // HeaderAcceptCharset is the HTTP "Accept-Charset" Header. - HeaderAcceptCharset = "Accept-Charset" + "github.com/go-http-utils/headers" ) +// Version is this package's version +const Version = "0.1.0" + type spec struct { val string q float64 @@ -82,7 +76,7 @@ func New(req *http.Request) Negotiator { func (n Negotiator) Accept(offers []string) (bestOffer string, matched bool) { parser := newHeaderParser(n.req.Header, true) - return parser.selectOffer(offers, parser.parse(HeaderAccept)) + return parser.selectOffer(offers, parser.parse(headers.Accept)) } // Language returns the most preferred language from the HTTP Accept-Language @@ -90,7 +84,7 @@ func (n Negotiator) Accept(offers []string) (bestOffer string, matched bool) { func (n Negotiator) Language(offers []string) (bestOffer string, matched bool) { parser := newHeaderParser(n.req.Header, false) - return parser.selectOffer(offers, parser.parse(HeaderAcceptLanguage)) + return parser.selectOffer(offers, parser.parse(headers.AcceptLanguage)) } // Encoding returns the most preferred language from the HTTP Accept-Encoding @@ -98,7 +92,7 @@ func (n Negotiator) Language(offers []string) (bestOffer string, matched bool) { func (n Negotiator) Encoding(offers []string) (bestOffer string, matched bool) { parser := newHeaderParser(n.req.Header, false) - return parser.selectOffer(offers, parser.parse(HeaderAcceptEncoding)) + return parser.selectOffer(offers, parser.parse(headers.AcceptEncoding)) } // Charset returns the most preferred language from the HTTP Accept-Charset @@ -106,5 +100,5 @@ func (n Negotiator) Encoding(offers []string) (bestOffer string, matched bool) { func (n Negotiator) Charset(offers []string) (bestOffer string, matched bool) { parser := newHeaderParser(n.req.Header, false) - return parser.selectOffer(offers, parser.parse(HeaderAcceptCharset)) + return parser.selectOffer(offers, parser.parse(headers.AcceptCharset)) } diff --git a/negotiator_test.go b/negotiator_test.go index f388c3b..1a88df8 100644 --- a/negotiator_test.go +++ b/negotiator_test.go @@ -5,6 +5,7 @@ import ( "net/http/httptest" "testing" + "github.com/go-http-utils/headers" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" ) @@ -27,7 +28,7 @@ type AcceptSuite struct { } func (s AcceptSuite) TestEmpty() { - n := setUpNegotiator(HeaderAccept, "application/json;q=0.2, text/html") + n := setUpNegotiator(headers.Accept, "application/json;q=0.2, text/html") _, matched := n.Accept([]string{}) @@ -35,7 +36,7 @@ func (s AcceptSuite) TestEmpty() { } func (s AcceptSuite) TestCaseInsensitive() { - n := setUpNegotiator(HeaderAccept, "text/html") + n := setUpNegotiator(headers.Accept, "text/html") bestOffer, matched := n.Accept([]string{"TEXT/HTML"}) @@ -44,7 +45,7 @@ func (s AcceptSuite) TestCaseInsensitive() { } func (s AcceptSuite) TestUnMatched() { - n := setUpNegotiator(HeaderAccept, "application/json;q=0.2, text/html") + n := setUpNegotiator(headers.Accept, "application/json;q=0.2, text/html") _, matched := n.Accept([]string{"text/plain"}) @@ -52,7 +53,7 @@ func (s AcceptSuite) TestUnMatched() { } func (s AcceptSuite) TestEmptyAccepts() { - n := setUpNegotiator(HeaderAccept, "application/json;q=0") + n := setUpNegotiator(headers.Accept, "application/json;q=0") _, matched := n.Accept([]string{"application/json"}) @@ -60,7 +61,7 @@ func (s AcceptSuite) TestEmptyAccepts() { } func (s AcceptSuite) TestOneMatch() { - n := setUpNegotiator(HeaderAccept, "application/json;q=0.2") + n := setUpNegotiator(headers.Accept, "application/json;q=0.2") bestOffer, matched := n.Accept([]string{"application/json"}) @@ -69,7 +70,7 @@ func (s AcceptSuite) TestOneMatch() { } func (s AcceptSuite) TestWithAsterisk() { - n := setUpNegotiator(HeaderAccept, "text/*") + n := setUpNegotiator(headers.Accept, "text/*") bestOffer, matched := n.Accept([]string{"text/*"}) @@ -78,7 +79,7 @@ func (s AcceptSuite) TestWithAsterisk() { } func (s AcceptSuite) TestMatchAsterisk() { - n := setUpNegotiator(HeaderAccept, "text/*") + n := setUpNegotiator(headers.Accept, "text/*") bestOffer, matched := n.Accept([]string{"text/html"}) @@ -87,7 +88,7 @@ func (s AcceptSuite) TestMatchAsterisk() { } func (s AcceptSuite) TestFirstMatchAsterisk() { - n := setUpNegotiator(HeaderAccept, "text/*") + n := setUpNegotiator(headers.Accept, "text/*") bestOffer, matched := n.Accept([]string{"text/html", "text/plain", "application/json"}) @@ -96,7 +97,7 @@ func (s AcceptSuite) TestFirstMatchAsterisk() { } func (s AcceptSuite) TestFirstMatchAllAsterisk() { - n := setUpNegotiator(HeaderAccept, "*/*, application/json;q=0.2") + n := setUpNegotiator(headers.Accept, "*/*, application/json;q=0.2") bestOffer, matched := n.Accept([]string{"text/html", "application/json", "text/plain"}) @@ -105,7 +106,7 @@ func (s AcceptSuite) TestFirstMatchAllAsterisk() { } func (s AcceptSuite) TestWithAllAsterisk() { - n := setUpNegotiator(HeaderAccept, "*/*") + n := setUpNegotiator(headers.Accept, "*/*") bestOffer, matched := n.Accept([]string{"application/json", "text/html", "text/plain"}) @@ -123,7 +124,7 @@ type LanguageSuite struct { } func (s LanguageSuite) TestEmpty() { - n := setUpNegotiator(HeaderAcceptLanguage, "") + n := setUpNegotiator(headers.AcceptLanguage, "") _, matched := n.Language([]string{}) @@ -131,7 +132,7 @@ func (s LanguageSuite) TestEmpty() { } func (s LanguageSuite) TestCaseInsensitive() { - n := setUpNegotiator(HeaderAcceptLanguage, "En") + n := setUpNegotiator(headers.AcceptLanguage, "En") bestOffer, matched := n.Language([]string{"eN"}) @@ -140,7 +141,7 @@ func (s LanguageSuite) TestCaseInsensitive() { } func (s LanguageSuite) TestUnMatched() { - n := setUpNegotiator(HeaderAcceptLanguage, "en,zh") + n := setUpNegotiator(headers.AcceptLanguage, "en,zh") _, matched := n.Language([]string{"ko"}) @@ -148,7 +149,7 @@ func (s LanguageSuite) TestUnMatched() { } func (s LanguageSuite) TestEmptyLanguages() { - n := setUpNegotiator(HeaderAcceptLanguage, "en;q=0") + n := setUpNegotiator(headers.AcceptLanguage, "en;q=0") _, matched := n.Language([]string{"en"}) @@ -156,7 +157,7 @@ func (s LanguageSuite) TestEmptyLanguages() { } func (s LanguageSuite) TestOneMatch() { - n := setUpNegotiator(HeaderAcceptLanguage, "en;q=0.2") + n := setUpNegotiator(headers.AcceptLanguage, "en;q=0.2") bestOffer, matched := n.Language([]string{"en"}) @@ -165,7 +166,7 @@ func (s LanguageSuite) TestOneMatch() { } func (s LanguageSuite) TestMatchAsterisk() { - n := setUpNegotiator(HeaderAcceptLanguage, "*") + n := setUpNegotiator(headers.AcceptLanguage, "*") bestOffer, matched := n.Language([]string{"ko", "en"}) @@ -174,7 +175,7 @@ func (s LanguageSuite) TestMatchAsterisk() { } func (s LanguageSuite) TestFirstMatchAllAsterisk() { - n := setUpNegotiator(HeaderAcceptLanguage, "*, ko;q=0.5") + n := setUpNegotiator(headers.AcceptLanguage, "*, ko;q=0.5") bestOffer, matched := n.Language([]string{"en", "ko", "zh"}) @@ -192,7 +193,7 @@ type EncodingSuite struct { } func (s EncodingSuite) TestEmpty() { - n := setUpNegotiator(HeaderAcceptEncoding, "") + n := setUpNegotiator(headers.AcceptEncoding, "") _, matched := n.Encoding([]string{}) @@ -200,7 +201,7 @@ func (s EncodingSuite) TestEmpty() { } func (s EncodingSuite) TestCaseInsensitive() { - n := setUpNegotiator(HeaderAcceptEncoding, "GZip") + n := setUpNegotiator(headers.AcceptEncoding, "GZip") bestOffer, matched := n.Encoding([]string{"Gzip"}) @@ -209,7 +210,7 @@ func (s EncodingSuite) TestCaseInsensitive() { } func (s EncodingSuite) TestUnMatched() { - n := setUpNegotiator(HeaderAcceptEncoding, "gzip,default") + n := setUpNegotiator(headers.AcceptEncoding, "gzip,default") _, matched := n.Encoding([]string{"zlib"}) @@ -217,7 +218,7 @@ func (s EncodingSuite) TestUnMatched() { } func (s EncodingSuite) TestEmptyLanguages() { - n := setUpNegotiator(HeaderAcceptEncoding, "gzip;q=0") + n := setUpNegotiator(headers.AcceptEncoding, "gzip;q=0") _, matched := n.Encoding([]string{"gzip"}) @@ -225,7 +226,7 @@ func (s EncodingSuite) TestEmptyLanguages() { } func (s EncodingSuite) TestOneMatch() { - n := setUpNegotiator(HeaderAcceptEncoding, "gzip;q=0.2") + n := setUpNegotiator(headers.AcceptEncoding, "gzip;q=0.2") bestOffer, matched := n.Encoding([]string{"gzip"}) @@ -234,7 +235,7 @@ func (s EncodingSuite) TestOneMatch() { } func (s EncodingSuite) TestMatchAsterisk() { - n := setUpNegotiator(HeaderAcceptEncoding, "*") + n := setUpNegotiator(headers.AcceptEncoding, "*") bestOffer, matched := n.Encoding([]string{"gzip", "deflate"}) @@ -243,7 +244,7 @@ func (s EncodingSuite) TestMatchAsterisk() { } func (s EncodingSuite) TestFirstMatchAllAsterisk() { - n := setUpNegotiator(HeaderAcceptEncoding, "*, gzip;q=0.5") + n := setUpNegotiator(headers.AcceptEncoding, "*, gzip;q=0.5") bestOffer, matched := n.Encoding([]string{"gzip", "deflate", "zlib"}) @@ -261,7 +262,7 @@ type CharsetSuite struct { } func (s CharsetSuite) TestEmpty() { - n := setUpNegotiator(HeaderAcceptCharset, "") + n := setUpNegotiator(headers.AcceptCharset, "") _, matched := n.Charset([]string{}) @@ -269,7 +270,7 @@ func (s CharsetSuite) TestEmpty() { } func (s CharsetSuite) TestCaseInsensitive() { - n := setUpNegotiator(HeaderAcceptCharset, "ISO-8859-1") + n := setUpNegotiator(headers.AcceptCharset, "ISO-8859-1") bestOffer, matched := n.Charset([]string{"ISO-8859-1"}) @@ -278,7 +279,7 @@ func (s CharsetSuite) TestCaseInsensitive() { } func (s CharsetSuite) TestUnMatched() { - n := setUpNegotiator(HeaderAcceptCharset, "ISO-8859-1,UTF-8") + n := setUpNegotiator(headers.AcceptCharset, "ISO-8859-1,UTF-8") _, matched := n.Charset([]string{"ASCII"}) @@ -286,7 +287,7 @@ func (s CharsetSuite) TestUnMatched() { } func (s CharsetSuite) TestEmptyCharset() { - n := setUpNegotiator(HeaderAcceptCharset, "UTF-8;q=0") + n := setUpNegotiator(headers.AcceptCharset, "UTF-8;q=0") _, matched := n.Charset([]string{"UTF-8"}) @@ -294,7 +295,7 @@ func (s CharsetSuite) TestEmptyCharset() { } func (s CharsetSuite) TestOneMatch() { - n := setUpNegotiator(HeaderAcceptCharset, "UTF-8;q=0.2") + n := setUpNegotiator(headers.AcceptCharset, "UTF-8;q=0.2") bestOffer, matched := n.Charset([]string{"UTF-8"}) @@ -303,7 +304,7 @@ func (s CharsetSuite) TestOneMatch() { } func (s CharsetSuite) TestMatchAsterisk() { - n := setUpNegotiator(HeaderAcceptCharset, "*") + n := setUpNegotiator(headers.AcceptCharset, "*") bestOffer, matched := n.Charset([]string{"UTF-8", "ISO-8859-1"}) @@ -312,7 +313,7 @@ func (s CharsetSuite) TestMatchAsterisk() { } func (s CharsetSuite) TestFirstMatchAllAsterisk() { - n := setUpNegotiator(HeaderAcceptCharset, "*, UTF-8;q=0.5") + n := setUpNegotiator(headers.AcceptCharset, "*, UTF-8;q=0.5") bestOffer, matched := n.Charset([]string{"UTF-8", "ISO-8859-1", "ASCII"}) @@ -321,7 +322,7 @@ func (s CharsetSuite) TestFirstMatchAllAsterisk() { } func (s CharsetSuite) TestHighOrderPreferred() { - n := setUpNegotiator(HeaderAcceptCharset, "UTF-8;q=0.6, ISO-8859-1;q=0.8, UTF-8;q=0.9") + n := setUpNegotiator(headers.AcceptCharset, "UTF-8;q=0.6, ISO-8859-1;q=0.8, UTF-8;q=0.9") bestOffer, matched := n.Charset([]string{"UTF-8", "ISO-8859-1", "ASCII"}) diff --git a/parser_accept_test.go b/parser_accept_test.go index a5dd253..4cecd6b 100644 --- a/parser_accept_test.go +++ b/parser_accept_test.go @@ -5,6 +5,7 @@ import ( "net/http" + "github.com/go-http-utils/headers" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" ) @@ -24,8 +25,8 @@ func (s *ParseAcceptTestSuite) SetupTest() { func (s *ParseAcceptTestSuite) TestEmpty() { assert := assert.New(s.T()) - s.header.Set(HeaderAccept, "") - specs := s.parser.parse(HeaderAccept) + s.header.Set(headers.Accept, "") + specs := s.parser.parse(headers.Accept) assert.Equal(1, len(specs)) @@ -35,8 +36,8 @@ func (s *ParseAcceptTestSuite) TestEmpty() { func (s *ParseAcceptTestSuite) TestAsterisk() { assert := assert.New(s.T()) - s.header.Set(HeaderAccept, "*/*") - specs := s.parser.parse(HeaderAccept) + s.header.Set(headers.Accept, "*/*") + specs := s.parser.parse(headers.Accept) assert.Equal(1, len(specs)) @@ -46,8 +47,8 @@ func (s *ParseAcceptTestSuite) TestAsterisk() { func (s *ParseAcceptTestSuite) TestOneType() { assert := assert.New(s.T()) - s.header.Set(HeaderAccept, "application/json") - specs := s.parser.parse(HeaderAccept) + s.header.Set(headers.Accept, "application/json") + specs := s.parser.parse(headers.Accept) assert.Equal(1, len(specs)) @@ -57,8 +58,8 @@ func (s *ParseAcceptTestSuite) TestOneType() { func (s *ParseAcceptTestSuite) TestOneTypeWithQZero() { assert := assert.New(s.T()) - s.header.Set(HeaderAccept, "application/json;q=0") - specs := s.parser.parse(HeaderAccept) + s.header.Set(headers.Accept, "application/json;q=0") + specs := s.parser.parse(headers.Accept) assert.Equal(0, len(specs)) } @@ -66,8 +67,8 @@ func (s *ParseAcceptTestSuite) TestOneTypeWithQZero() { func (s *ParseAcceptTestSuite) TestSortByQ() { assert := assert.New(s.T()) - s.header.Set(HeaderAccept, "application/json;q=0.2, text/html") - specs := s.parser.parse(HeaderAccept) + s.header.Set(headers.Accept, "application/json;q=0.2, text/html") + specs := s.parser.parse(headers.Accept) assert.Equal(2, len(specs)) @@ -78,8 +79,8 @@ func (s *ParseAcceptTestSuite) TestSortByQ() { func (s *ParseAcceptTestSuite) TestSuffixAsterisk() { assert := assert.New(s.T()) - s.header.Set(HeaderAccept, "text/*") - specs := s.parser.parse(HeaderAccept) + s.header.Set(headers.Accept, "text/*") + specs := s.parser.parse(headers.Accept) assert.Equal(1, len(specs)) @@ -89,8 +90,8 @@ func (s *ParseAcceptTestSuite) TestSuffixAsterisk() { func (s *ParseAcceptTestSuite) TestSortWithAsterisk() { assert := assert.New(s.T()) - s.header.Set(HeaderAccept, "text/plain, application/json;q=0.5, text/html, */*;q=0.1") - specs := s.parser.parse(HeaderAccept) + s.header.Set(headers.Accept, "text/plain, application/json;q=0.5, text/html, */*;q=0.1") + specs := s.parser.parse(headers.Accept) assert.Equal(4, len(specs)) diff --git a/parser_charset_test.go b/parser_charset_test.go index 0ea09f3..2a905bb 100644 --- a/parser_charset_test.go +++ b/parser_charset_test.go @@ -5,6 +5,7 @@ import ( "net/http" + "github.com/go-http-utils/headers" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" ) @@ -24,8 +25,8 @@ func (s *ParseCharsetTestSuite) SetupTest() { func (s *ParseCharsetTestSuite) TestEmpty() { assert := assert.New(s.T()) - s.header.Set(HeaderAcceptCharset, "") - specs := s.parser.parse(HeaderAcceptCharset) + s.header.Set(headers.AcceptCharset, "") + specs := s.parser.parse(headers.AcceptCharset) assert.Equal(1, len(specs)) @@ -35,8 +36,8 @@ func (s *ParseCharsetTestSuite) TestEmpty() { func (s *ParseCharsetTestSuite) TestAsterisk() { assert := assert.New(s.T()) - s.header.Set(HeaderAcceptCharset, "*") - specs := s.parser.parse(HeaderAcceptCharset) + s.header.Set(headers.AcceptCharset, "*") + specs := s.parser.parse(headers.AcceptCharset) assert.Equal(1, len(specs)) @@ -46,8 +47,8 @@ func (s *ParseCharsetTestSuite) TestAsterisk() { func (s *ParseCharsetTestSuite) TestOneLanguage() { assert := assert.New(s.T()) - s.header.Set(HeaderAcceptCharset, "UTF-8;level=1.0") - specs := s.parser.parse(HeaderAcceptCharset) + s.header.Set(headers.AcceptCharset, "UTF-8;level=1.0") + specs := s.parser.parse(headers.AcceptCharset) assert.Equal(1, len(specs)) @@ -57,8 +58,8 @@ func (s *ParseCharsetTestSuite) TestOneLanguage() { func (s *ParseCharsetTestSuite) TestOneLanguageWithQZero() { assert := assert.New(s.T()) - s.header.Set(HeaderAcceptCharset, "*, ISO-8859-1;level=0") - specs := s.parser.parse(HeaderAcceptCharset) + s.header.Set(headers.AcceptCharset, "*, ISO-8859-1;level=0") + specs := s.parser.parse(headers.AcceptCharset) assert.Equal(1, len(specs)) @@ -68,8 +69,8 @@ func (s *ParseCharsetTestSuite) TestOneLanguageWithQZero() { func (s *ParseCharsetTestSuite) TestSortByQ() { assert := assert.New(s.T()) - s.header.Set(HeaderAcceptCharset, "*;level=0.8, ISO-8859-1, UTF-8") - specs := s.parser.parse(HeaderAcceptCharset) + s.header.Set(headers.AcceptCharset, "*;level=0.8, ISO-8859-1, UTF-8") + specs := s.parser.parse(headers.AcceptCharset) assert.Equal(3, len(specs)) diff --git a/parser_encoding_test.go b/parser_encoding_test.go index db3c571..4902131 100644 --- a/parser_encoding_test.go +++ b/parser_encoding_test.go @@ -5,6 +5,7 @@ import ( "net/http" + "github.com/go-http-utils/headers" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" ) @@ -24,8 +25,8 @@ func (s *ParseEncodingTestSuite) SetupTest() { func (s *ParseEncodingTestSuite) TestEmpty() { assert := assert.New(s.T()) - s.header.Set(HeaderAcceptEncoding, "") - specs := s.parser.parse(HeaderAcceptEncoding) + s.header.Set(headers.AcceptEncoding, "") + specs := s.parser.parse(headers.AcceptEncoding) assert.Equal(1, len(specs)) @@ -35,8 +36,8 @@ func (s *ParseEncodingTestSuite) TestEmpty() { func (s *ParseEncodingTestSuite) TestAsterisk() { assert := assert.New(s.T()) - s.header.Set(HeaderAcceptEncoding, "*") - specs := s.parser.parse(HeaderAcceptEncoding) + s.header.Set(headers.AcceptEncoding, "*") + specs := s.parser.parse(headers.AcceptEncoding) assert.Equal(1, len(specs)) @@ -46,8 +47,8 @@ func (s *ParseEncodingTestSuite) TestAsterisk() { func (s *ParseEncodingTestSuite) TestOneEncoing() { assert := assert.New(s.T()) - s.header.Set(HeaderAcceptEncoding, "gzip") - specs := s.parser.parse(HeaderAcceptEncoding) + s.header.Set(headers.AcceptEncoding, "gzip") + specs := s.parser.parse(headers.AcceptEncoding) assert.Equal(1, len(specs)) @@ -57,8 +58,8 @@ func (s *ParseEncodingTestSuite) TestOneEncoing() { func (s *ParseEncodingTestSuite) TestOneEncodingWithQZero() { assert := assert.New(s.T()) - s.header.Set(HeaderAcceptEncoding, "*, gzip;q=0") - specs := s.parser.parse(HeaderAcceptEncoding) + s.header.Set(headers.AcceptEncoding, "*, gzip;q=0") + specs := s.parser.parse(headers.AcceptEncoding) assert.Equal(1, len(specs)) @@ -68,8 +69,8 @@ func (s *ParseEncodingTestSuite) TestOneEncodingWithQZero() { func (s *ParseEncodingTestSuite) TestSortByQ() { assert := assert.New(s.T()) - s.header.Set(HeaderAcceptEncoding, "*;q=0.8, defalte, gzip") - specs := s.parser.parse(HeaderAcceptEncoding) + s.header.Set(headers.AcceptEncoding, "*;q=0.8, defalte, gzip") + specs := s.parser.parse(headers.AcceptEncoding) assert.Equal(3, len(specs)) diff --git a/parser_language_test.go b/parser_language_test.go index 7a0db45..210599a 100644 --- a/parser_language_test.go +++ b/parser_language_test.go @@ -5,6 +5,7 @@ import ( "net/http" + "github.com/go-http-utils/headers" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" ) @@ -24,8 +25,8 @@ func (s *ParseLanguageTestSuite) SetupTest() { func (s *ParseLanguageTestSuite) TestEmpty() { assert := assert.New(s.T()) - s.header.Set(HeaderAcceptLanguage, "") - specs := s.parser.parse(HeaderAcceptLanguage) + s.header.Set(headers.AcceptLanguage, "") + specs := s.parser.parse(headers.AcceptLanguage) assert.Equal(1, len(specs)) @@ -35,8 +36,8 @@ func (s *ParseLanguageTestSuite) TestEmpty() { func (s *ParseLanguageTestSuite) TestAsterisk() { assert := assert.New(s.T()) - s.header.Set(HeaderAcceptLanguage, "*") - specs := s.parser.parse(HeaderAcceptLanguage) + s.header.Set(headers.AcceptLanguage, "*") + specs := s.parser.parse(headers.AcceptLanguage) assert.Equal(1, len(specs)) @@ -46,8 +47,8 @@ func (s *ParseLanguageTestSuite) TestAsterisk() { func (s *ParseLanguageTestSuite) TestOneLanguage() { assert := assert.New(s.T()) - s.header.Set(HeaderAcceptLanguage, "en") - specs := s.parser.parse(HeaderAcceptLanguage) + s.header.Set(headers.AcceptLanguage, "en") + specs := s.parser.parse(headers.AcceptLanguage) assert.Equal(1, len(specs)) @@ -57,8 +58,8 @@ func (s *ParseLanguageTestSuite) TestOneLanguage() { func (s *ParseLanguageTestSuite) TestOneLanguageWithQZero() { assert := assert.New(s.T()) - s.header.Set(HeaderAcceptLanguage, "*, en;q=0") - specs := s.parser.parse(HeaderAcceptLanguage) + s.header.Set(headers.AcceptLanguage, "*, en;q=0") + specs := s.parser.parse(headers.AcceptLanguage) assert.Equal(1, len(specs)) @@ -68,8 +69,8 @@ func (s *ParseLanguageTestSuite) TestOneLanguageWithQZero() { func (s *ParseLanguageTestSuite) TestSortByQ() { assert := assert.New(s.T()) - s.header.Set(HeaderAcceptLanguage, "*;q=0.8, en, es") - specs := s.parser.parse(HeaderAcceptLanguage) + s.header.Set(headers.AcceptLanguage, "*;q=0.8, en, es") + specs := s.parser.parse(headers.AcceptLanguage) assert.Equal(3, len(specs))