Skip to content

Commit

Permalink
Merge pull request #3 from go-http-utils/issue/headers-constants
Browse files Browse the repository at this point in the history
remove headers dependence
  • Loading branch information
David Cai committed Jan 5, 2017
2 parents 387e361 + 2c54e08 commit 20eb27f
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 98 deletions.
35 changes: 18 additions & 17 deletions negotiator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,37 @@ package negotiator
import (
"net/http"
"strings"

"github.com/go-http-utils/headers"
)

// Version is this package's version
const Version = "0.1.0"
const Version = "0.2.0"

const (
headerAccept = "Accept"
headerAcceptLanguage = "Accept-Language"
headerAcceptEncoding = "Accept-Encoding"
headerAcceptCharset = "Accept-Charset"
)

type spec struct {
val string
q float64
}

// Specs it the shorthand for []Spec.
// Specs represents []Spec.
type specs []spec

// Len is used to impelement sort.Interface for Specs.
// Len is to impelement sort.Interface for Specs.
func (ss specs) Len() int {
return len(ss)
}

// Swap is used to impelement sort.Interface for Specs.
// Swap is to impelement sort.Interface for Specs.
func (ss specs) Swap(i, j int) {
ss[i], ss[j] = ss[j], ss[i]
}

// Less is used to impelement sort.Interface for Specs.
// Less is to impelement sort.Interface for Specs.
func (ss specs) Less(i, j int) bool {
if ss[i].q > ss[j].q {
return true
Expand Down Expand Up @@ -58,10 +63,6 @@ func (ss specs) hasVal(val string) bool {
return false
}

func formatHeaderVal(val string) string {
return strings.ToLower(strings.Replace(val, " ", "", -1))
}

// Negotiator repensents the HTTP negotiator.
type Negotiator struct {
header http.Header
Expand All @@ -76,26 +77,26 @@ func New(header http.Header) *Negotiator {
// If nothing accepted, then empty string is returned.
func (n *Negotiator) Type(offers ...string) (bestOffer string) {
parser := newHeaderParser(n.header, true)
return parser.selectOffer(offers, parser.parse(headers.Accept))
return parser.selectOffer(offers, parser.parse(headerAccept))
}

// Language returns the most preferred language from the HTTP Accept-Language
// header. If nothing accepted, then empty string is returned.
func (n *Negotiator) Language(offers ...string) (bestOffer string) {
parser := newHeaderParser(n.header, false)
return parser.selectOffer(offers, parser.parse(headers.AcceptLanguage))
return parser.selectOffer(offers, parser.parse(headerAcceptLanguage))
}

// Encoding returns the most preferred language from the HTTP Accept-Encoding
// Encoding returns the most preferred encoding from the HTTP Accept-Encoding
// header. If nothing accepted, then empty string is returned.
func (n *Negotiator) Encoding(offers ...string) (bestOffer string) {
parser := newHeaderParser(n.header, false)
return parser.selectOffer(offers, parser.parse(headers.AcceptEncoding))
return parser.selectOffer(offers, parser.parse(headerAcceptEncoding))
}

// Charset returns the most preferred language from the HTTP Accept-Charset
// Charset returns the most preferred charset from the HTTP Accept-Charset
// header. If nothing accepted, then empty string is returned.
func (n *Negotiator) Charset(offers ...string) (bestOffer string) {
parser := newHeaderParser(n.header, false)
return parser.selectOffer(offers, parser.parse(headers.AcceptCharset))
return parser.selectOffer(offers, parser.parse(headerAcceptCharset))
}
65 changes: 32 additions & 33 deletions negotiator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net/http/httptest"
"testing"

"github.com/go-http-utils/headers"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
Expand All @@ -28,52 +27,52 @@ type AcceptSuite struct {
}

func (s AcceptSuite) TestEmpty() {
n := setUpNegotiator(headers.Accept, "application/json;q=0.2, text/html")
n := setUpNegotiator(headerAccept, "application/json;q=0.2, text/html")
s.Equal("text/html", n.Type())
}

func (s AcceptSuite) TestCaseInsensitive() {
n := setUpNegotiator(headers.Accept, "text/html")
n := setUpNegotiator(headerAccept, "text/html")
s.Equal("TEXT/HTML", n.Type("TEXT/HTML"))
}

func (s AcceptSuite) TestUnMatched() {
n := setUpNegotiator(headers.Accept, "application/json;q=0.2, text/html")
n := setUpNegotiator(headerAccept, "application/json;q=0.2, text/html")
s.Equal("", n.Type("text/plain"))
}

func (s AcceptSuite) TestEmptyAccepts() {
n := setUpNegotiator(headers.Accept, "application/json;q=0")
n := setUpNegotiator(headerAccept, "application/json;q=0")
s.Equal("", n.Type("application/json"))
}

func (s AcceptSuite) TestOneMatch() {
n := setUpNegotiator(headers.Accept, "application/json;q=0.2")
n := setUpNegotiator(headerAccept, "application/json;q=0.2")
s.Equal("application/json", n.Type("application/json"))
}

func (s AcceptSuite) TestWithAsterisk() {
n := setUpNegotiator(headers.Accept, "text/*")
n := setUpNegotiator(headerAccept, "text/*")
s.Equal("text/*", n.Type("text/*"))
}

func (s AcceptSuite) TestMatchAsterisk() {
n := setUpNegotiator(headers.Accept, "text/*")
n := setUpNegotiator(headerAccept, "text/*")
s.Equal("text/html", n.Type("text/html"))
}

func (s AcceptSuite) TestFirstMatchAsterisk() {
n := setUpNegotiator(headers.Accept, "text/*")
n := setUpNegotiator(headerAccept, "text/*")
s.Equal("text/html", n.Type("text/html", "text/plain", "application/json"))
}

func (s AcceptSuite) TestFirstMatchAllAsterisk() {
n := setUpNegotiator(headers.Accept, "*/*, application/json;q=0.2")
n := setUpNegotiator(headerAccept, "*/*, application/json;q=0.2")
s.Equal("text/html", n.Type("text/html", "application/json", "text/plain"))
}

func (s AcceptSuite) TestWithAllAsterisk() {
n := setUpNegotiator(headers.Accept, "*/*")
n := setUpNegotiator(headerAccept, "*/*")
s.Equal("application/json", n.Type("application/json", "text/html", "text/plain"))
}

Expand All @@ -87,37 +86,37 @@ type LanguageSuite struct {
}

func (s LanguageSuite) TestEmpty() {
n := setUpNegotiator(headers.AcceptLanguage, "")
n := setUpNegotiator(headerAcceptLanguage, "")
s.Equal("*", n.Language())
}

func (s LanguageSuite) TestCaseInsensitive() {
n := setUpNegotiator(headers.AcceptLanguage, "En")
n := setUpNegotiator(headerAcceptLanguage, "En")
s.Equal("eN", n.Language("eN"))
}

func (s LanguageSuite) TestUnMatched() {
n := setUpNegotiator(headers.AcceptLanguage, "en,zh")
n := setUpNegotiator(headerAcceptLanguage, "en,zh")
s.Equal("", n.Language("ko"))
}

func (s LanguageSuite) TestEmptyLanguages() {
n := setUpNegotiator(headers.AcceptLanguage, "en;q=0")
n := setUpNegotiator(headerAcceptLanguage, "en;q=0")
s.Equal("", n.Language("en"))
}

func (s LanguageSuite) TestOneMatch() {
n := setUpNegotiator(headers.AcceptLanguage, "en;q=0.2")
n := setUpNegotiator(headerAcceptLanguage, "en;q=0.2")
s.Equal("en", n.Language("en"))
}

func (s LanguageSuite) TestMatchAsterisk() {
n := setUpNegotiator(headers.AcceptLanguage, "*")
n := setUpNegotiator(headerAcceptLanguage, "*")
s.Equal("ko", n.Language("ko", "en"))
}

func (s LanguageSuite) TestFirstMatchAllAsterisk() {
n := setUpNegotiator(headers.AcceptLanguage, "*, ko;q=0.5")
n := setUpNegotiator(headerAcceptLanguage, "*, ko;q=0.5")
s.Equal("en", n.Language("en", "ko", "zh"))
}

Expand All @@ -131,37 +130,37 @@ type EncodingSuite struct {
}

func (s EncodingSuite) TestEmpty() {
n := setUpNegotiator(headers.AcceptEncoding, "")
n := setUpNegotiator(headerAcceptEncoding, "")
s.Equal("*", n.Encoding())
}

func (s EncodingSuite) TestCaseInsensitive() {
n := setUpNegotiator(headers.AcceptEncoding, "GZip")
n := setUpNegotiator(headerAcceptEncoding, "GZip")
s.Equal("Gzip", n.Encoding("Gzip"))
}

func (s EncodingSuite) TestUnMatched() {
n := setUpNegotiator(headers.AcceptEncoding, "gzip,default")
n := setUpNegotiator(headerAcceptEncoding, "gzip,default")
s.Equal("", n.Encoding("zlib"))
}

func (s EncodingSuite) TestEmptyLanguages() {
n := setUpNegotiator(headers.AcceptEncoding, "gzip;q=0")
n := setUpNegotiator(headerAcceptEncoding, "gzip;q=0")
s.Equal("", n.Encoding("gzip"))
}

func (s EncodingSuite) TestOneMatch() {
n := setUpNegotiator(headers.AcceptEncoding, "gzip;q=0.2")
n := setUpNegotiator(headerAcceptEncoding, "gzip;q=0.2")
s.Equal("gzip", n.Encoding("gzip"))
}

func (s EncodingSuite) TestMatchAsterisk() {
n := setUpNegotiator(headers.AcceptEncoding, "*")
n := setUpNegotiator(headerAcceptEncoding, "*")
s.Equal("gzip", n.Encoding("gzip", "deflate"))
}

func (s EncodingSuite) TestFirstMatchAllAsterisk() {
n := setUpNegotiator(headers.AcceptEncoding, "*, gzip;q=0.5")
n := setUpNegotiator(headerAcceptEncoding, "*, gzip;q=0.5")
s.Equal("deflate", n.Encoding("gzip", "deflate", "zlib"))
}

Expand All @@ -175,42 +174,42 @@ type CharsetSuite struct {
}

func (s CharsetSuite) TestEmpty() {
n := setUpNegotiator(headers.AcceptCharset, "")
n := setUpNegotiator(headerAcceptCharset, "")
s.Equal("*", n.Charset())
}

func (s CharsetSuite) TestCaseInsensitive() {
n := setUpNegotiator(headers.AcceptCharset, "ISO-8859-1")
n := setUpNegotiator(headerAcceptCharset, "ISO-8859-1")
s.Equal("ISO-8859-1", n.Charset("ISO-8859-1"))
}

func (s CharsetSuite) TestUnMatched() {
n := setUpNegotiator(headers.AcceptCharset, "ISO-8859-1,UTF-8")
n := setUpNegotiator(headerAcceptCharset, "ISO-8859-1,UTF-8")
s.Equal("", n.Charset("ASCII"))
}

func (s CharsetSuite) TestEmptyCharset() {
n := setUpNegotiator(headers.AcceptCharset, "UTF-8;q=0")
n := setUpNegotiator(headerAcceptCharset, "UTF-8;q=0")
s.Equal("", n.Charset("UTF-8"))
}

func (s CharsetSuite) TestOneMatch() {
n := setUpNegotiator(headers.AcceptCharset, "UTF-8;q=0.2")
n := setUpNegotiator(headerAcceptCharset, "UTF-8;q=0.2")
s.Equal("UTF-8", n.Charset("UTF-8"))
}

func (s CharsetSuite) TestMatchAsterisk() {
n := setUpNegotiator(headers.AcceptCharset, "*")
n := setUpNegotiator(headerAcceptCharset, "*")
s.Equal("UTF-8", n.Charset("UTF-8", "ISO-8859-1"))
}

func (s CharsetSuite) TestFirstMatchAllAsterisk() {
n := setUpNegotiator(headers.AcceptCharset, "*, UTF-8;q=0.5")
n := setUpNegotiator(headerAcceptCharset, "*, UTF-8;q=0.5")
s.Equal("ISO-8859-1", n.Charset("UTF-8", "ISO-8859-1", "ASCII"))
}

func (s CharsetSuite) TestHighOrderPreferred() {
n := setUpNegotiator(headers.AcceptCharset, "UTF-8;q=0.6, ISO-8859-1;q=0.8, UTF-8;q=0.9")
n := setUpNegotiator(headerAcceptCharset, "UTF-8;q=0.6, ISO-8859-1;q=0.8, UTF-8;q=0.9")
s.Equal("UTF-8", n.Charset("UTF-8", "ISO-8859-1", "ASCII"))
}

Expand Down
4 changes: 4 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,7 @@ func (p headerParser) selectOffer(offers []string, specs specs) (bestOffer strin

return
}

func formatHeaderVal(val string) string {
return strings.ToLower(strings.Replace(val, " ", "", -1))
}
29 changes: 14 additions & 15 deletions parser_accept_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"net/http"

"github.com/go-http-utils/headers"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
Expand All @@ -25,8 +24,8 @@ func (s *ParseAcceptTestSuite) SetupTest() {
func (s *ParseAcceptTestSuite) TestEmpty() {
assert := assert.New(s.T())

s.header.Set(headers.Accept, "")
specs := s.parser.parse(headers.Accept)
s.header.Set(headerAccept, "")
specs := s.parser.parse(headerAccept)

assert.Equal(1, len(specs))

Expand All @@ -36,8 +35,8 @@ func (s *ParseAcceptTestSuite) TestEmpty() {
func (s *ParseAcceptTestSuite) TestAsterisk() {
assert := assert.New(s.T())

s.header.Set(headers.Accept, "*/*")
specs := s.parser.parse(headers.Accept)
s.header.Set(headerAccept, "*/*")
specs := s.parser.parse(headerAccept)

assert.Equal(1, len(specs))

Expand All @@ -47,8 +46,8 @@ func (s *ParseAcceptTestSuite) TestAsterisk() {
func (s *ParseAcceptTestSuite) TestOneType() {
assert := assert.New(s.T())

s.header.Set(headers.Accept, "application/json")
specs := s.parser.parse(headers.Accept)
s.header.Set(headerAccept, "application/json")
specs := s.parser.parse(headerAccept)

assert.Equal(1, len(specs))

Expand All @@ -58,17 +57,17 @@ func (s *ParseAcceptTestSuite) TestOneType() {
func (s *ParseAcceptTestSuite) TestOneTypeWithQZero() {
assert := assert.New(s.T())

s.header.Set(headers.Accept, "application/json;q=0")
specs := s.parser.parse(headers.Accept)
s.header.Set(headerAccept, "application/json;q=0")
specs := s.parser.parse(headerAccept)

assert.Equal(0, len(specs))
}

func (s *ParseAcceptTestSuite) TestSortByQ() {
assert := assert.New(s.T())

s.header.Set(headers.Accept, "application/json;q=0.2, text/html")
specs := s.parser.parse(headers.Accept)
s.header.Set(headerAccept, "application/json;q=0.2, text/html")
specs := s.parser.parse(headerAccept)

assert.Equal(2, len(specs))

Expand All @@ -79,8 +78,8 @@ func (s *ParseAcceptTestSuite) TestSortByQ() {
func (s *ParseAcceptTestSuite) TestSuffixAsterisk() {
assert := assert.New(s.T())

s.header.Set(headers.Accept, "text/*")
specs := s.parser.parse(headers.Accept)
s.header.Set(headerAccept, "text/*")
specs := s.parser.parse(headerAccept)

assert.Equal(1, len(specs))

Expand All @@ -90,8 +89,8 @@ func (s *ParseAcceptTestSuite) TestSuffixAsterisk() {
func (s *ParseAcceptTestSuite) TestSortWithAsterisk() {
assert := assert.New(s.T())

s.header.Set(headers.Accept, "text/plain, application/json;q=0.5, text/html, */*;q=0.1")
specs := s.parser.parse(headers.Accept)
s.header.Set(headerAccept, "text/plain, application/json;q=0.5, text/html, */*;q=0.1")
specs := s.parser.parse(headerAccept)

assert.Equal(4, len(specs))

Expand Down

0 comments on commit 20eb27f

Please sign in to comment.