Skip to content

Commit

Permalink
use headers
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidCai1111 committed Nov 16, 2016
1 parent 9950d74 commit e676e2e
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 94 deletions.
8 changes: 4 additions & 4 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}))
Expand All @@ -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"}))
Expand All @@ -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"}))
Expand All @@ -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"}))
Expand Down
22 changes: 8 additions & 14 deletions negotiator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -82,29 +76,29 @@ 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
// header.
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
// header.
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
// header.
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))
}
65 changes: 33 additions & 32 deletions negotiator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -27,15 +28,15 @@ 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{})

s.False(matched)
}

func (s AcceptSuite) TestCaseInsensitive() {
n := setUpNegotiator(HeaderAccept, "text/html")
n := setUpNegotiator(headers.Accept, "text/html")

bestOffer, matched := n.Accept([]string{"TEXT/HTML"})

Expand All @@ -44,23 +45,23 @@ 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"})

s.False(matched)
}

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"})

s.False(matched)
}

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"})

Expand All @@ -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/*"})

Expand All @@ -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"})

Expand All @@ -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"})

Expand All @@ -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"})

Expand All @@ -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"})

Expand All @@ -123,15 +124,15 @@ type LanguageSuite struct {
}

func (s LanguageSuite) TestEmpty() {
n := setUpNegotiator(HeaderAcceptLanguage, "")
n := setUpNegotiator(headers.AcceptLanguage, "")

_, matched := n.Language([]string{})

s.False(matched)
}

func (s LanguageSuite) TestCaseInsensitive() {
n := setUpNegotiator(HeaderAcceptLanguage, "En")
n := setUpNegotiator(headers.AcceptLanguage, "En")

bestOffer, matched := n.Language([]string{"eN"})

Expand All @@ -140,23 +141,23 @@ func (s LanguageSuite) TestCaseInsensitive() {
}

func (s LanguageSuite) TestUnMatched() {
n := setUpNegotiator(HeaderAcceptLanguage, "en,zh")
n := setUpNegotiator(headers.AcceptLanguage, "en,zh")

_, matched := n.Language([]string{"ko"})

s.False(matched)
}

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

_, matched := n.Language([]string{"en"})

s.False(matched)
}

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"})

Expand All @@ -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"})

Expand All @@ -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"})

Expand All @@ -192,15 +193,15 @@ type EncodingSuite struct {
}

func (s EncodingSuite) TestEmpty() {
n := setUpNegotiator(HeaderAcceptEncoding, "")
n := setUpNegotiator(headers.AcceptEncoding, "")

_, matched := n.Encoding([]string{})

s.False(matched)
}

func (s EncodingSuite) TestCaseInsensitive() {
n := setUpNegotiator(HeaderAcceptEncoding, "GZip")
n := setUpNegotiator(headers.AcceptEncoding, "GZip")

bestOffer, matched := n.Encoding([]string{"Gzip"})

Expand All @@ -209,23 +210,23 @@ func (s EncodingSuite) TestCaseInsensitive() {
}

func (s EncodingSuite) TestUnMatched() {
n := setUpNegotiator(HeaderAcceptEncoding, "gzip,default")
n := setUpNegotiator(headers.AcceptEncoding, "gzip,default")

_, matched := n.Encoding([]string{"zlib"})

s.False(matched)
}

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

_, matched := n.Encoding([]string{"gzip"})

s.False(matched)
}

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"})

Expand All @@ -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"})

Expand All @@ -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"})

Expand All @@ -261,15 +262,15 @@ type CharsetSuite struct {
}

func (s CharsetSuite) TestEmpty() {
n := setUpNegotiator(HeaderAcceptCharset, "")
n := setUpNegotiator(headers.AcceptCharset, "")

_, matched := n.Charset([]string{})

s.False(matched)
}

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"})

Expand All @@ -278,23 +279,23 @@ 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"})

s.False(matched)
}

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"})

s.False(matched)
}

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"})

Expand All @@ -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"})

Expand All @@ -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"})

Expand All @@ -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"})

Expand Down
Loading

0 comments on commit e676e2e

Please sign in to comment.