-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
945e97e
commit c9a42ab
Showing
8 changed files
with
251 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,71 @@ | ||
# negotiator | ||
[![Build Status](https://travis-ci.org/DavidCai1993/negotiator.svg?branch=master)](https://travis-ci.org/DavidCai1993/negotiator) | ||
[![Coverage Status](https://coveralls.io/repos/github/DavidCai1993/negotiator/badge.svg)](https://coveralls.io/github/DavidCai1993/negotiator) | ||
[![Coverage Status](https://coveralls.io/repos/github/DavidCai1993/negotiator/badge.svg)](https://coveralls.io/github/DavidCai1993/negotiator) | ||
An HTTP content negotiator for Go | ||
|
||
## Installation | ||
|
||
```sh | ||
go get -u github.com/DavidCai1993/negotiator | ||
``` | ||
|
||
## Usage | ||
|
||
```go | ||
import ( | ||
"github.com/DavidCai1993/negotiator" | ||
) | ||
|
||
negotiator := negotiator.New(req) | ||
``` | ||
|
||
### Accept | ||
|
||
```go | ||
// Assume that the Accept header is "text/html, application/*;q=0.9, image/jpeg;q=0.8" | ||
|
||
negotiator.Accept([]string{"text/html", "application/json", "image/jpeg"}) | ||
// -> "text/html" | ||
|
||
negotiator.Accept([]string{"application/json", "image/jpeg", "text/plain"}) | ||
// -> "application/json" | ||
|
||
negotiator.Accept([]string{"text/plain"}) | ||
// -> "" | ||
``` | ||
|
||
### Encoding | ||
|
||
```go | ||
// Assume that the Accept-Encoding header is "gzip, compress;q=0.2, identity;q=0.5" | ||
|
||
negotiator.Encoding([]string{"identity", "gzip"}) | ||
// -> "gzip" | ||
|
||
negotiator.Encoding([]string{"compress", "identity"}) | ||
// -> "identity" | ||
``` | ||
|
||
### Language | ||
|
||
```go | ||
// Assume that the Accept-Language header is "en;q=0.8, es, pt" | ||
|
||
negotiator.Language([]string{"en", "es", "fr"}) | ||
// -> "es" | ||
|
||
negotiator.Language([]string{"es", "pt"}) | ||
// -> "es" | ||
``` | ||
|
||
### Charset | ||
|
||
```go | ||
// Assume that the Accept-Charset header is "utf-8, iso-8859-1;q=0.8, utf-7;q=0.2" | ||
|
||
negotiator.Charset([]string{"utf-8", "iso-8859-1", "iso-8859-5"}) | ||
// -> "utf-8" | ||
|
||
negotiator.Charset([]string{"iso-8859-5"}) | ||
// -> "" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package negotiator | ||
|
||
import ( | ||
"testing" | ||
|
||
"net/http" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type ParseCharsetTestSuite struct { | ||
suite.Suite | ||
|
||
parser *headerParser | ||
header http.Header | ||
} | ||
|
||
func (s *ParseCharsetTestSuite) SetupTest() { | ||
s.header = make(http.Header) | ||
s.parser = newHeaderParser(s.header, false) | ||
} | ||
|
||
func (s *ParseCharsetTestSuite) TestEmpty() { | ||
assert := assert.New(s.T()) | ||
|
||
s.header.Set(HeaderAcceptCharset, "") | ||
specs := s.parser.parse(HeaderAcceptCharset) | ||
|
||
assert.Equal(1, len(specs)) | ||
|
||
equalSpec(assert, specs[0], "*", 1.0) | ||
} | ||
|
||
func (s *ParseCharsetTestSuite) TestAsterisk() { | ||
assert := assert.New(s.T()) | ||
|
||
s.header.Set(HeaderAcceptCharset, "*") | ||
specs := s.parser.parse(HeaderAcceptCharset) | ||
|
||
assert.Equal(1, len(specs)) | ||
|
||
equalSpec(assert, specs[0], "*", 1.0) | ||
} | ||
|
||
func (s *ParseCharsetTestSuite) TestOneLanguage() { | ||
assert := assert.New(s.T()) | ||
|
||
s.header.Set(HeaderAcceptCharset, "UTF-8;level=1.0") | ||
specs := s.parser.parse(HeaderAcceptCharset) | ||
|
||
assert.Equal(1, len(specs)) | ||
|
||
equalSpec(assert, specs[0], "utf-8", 1.0) | ||
} | ||
|
||
func (s *ParseCharsetTestSuite) TestOneLanguageWithQZero() { | ||
assert := assert.New(s.T()) | ||
|
||
s.header.Set(HeaderAcceptCharset, "*, ISO-8859-1;level=0") | ||
specs := s.parser.parse(HeaderAcceptCharset) | ||
|
||
assert.Equal(1, len(specs)) | ||
|
||
equalSpec(assert, specs[0], "*", 1.0) | ||
} | ||
|
||
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) | ||
|
||
assert.Equal(3, len(specs)) | ||
|
||
equalSpec(assert, specs[0], "iso-8859-1", 1.0) | ||
equalSpec(assert, specs[1], "utf-8", 1.0) | ||
equalSpec(assert, specs[2], "*", 0.8) | ||
} | ||
|
||
func TestParseCharset(t *testing.T) { | ||
suite.Run(t, new(ParseCharsetTestSuite)) | ||
} |
File renamed without changes.
File renamed without changes.