-
Notifications
You must be signed in to change notification settings - Fork 0
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
ef2e7f4
commit a03506f
Showing
5 changed files
with
124 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
language: go | ||
go: | ||
- 1.7 | ||
before_install: | ||
- go get -t -v ./... | ||
- go get github.com/modocache/gover | ||
- go get github.com/mattn/goveralls | ||
script: | ||
- go test -v -coverprofile=negotiator.coverprofile | ||
- gover | ||
- goveralls -coverprofile=gover.coverprofile -service=travis-ci |
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 +1,31 @@ | ||
# compressible | ||
# compressible | ||
[![Build Status](https://travis-ci.org/go-http-utils/compressible.svg?branch=master)](https://travis-ci.org/go-http-utils/compressible) | ||
[![Coverage Status](https://coveralls.io/repos/github/go-http-utils/compressible/badge.svg?branch=master)](https://coveralls.io/github/go-http-utils/compressible?branch=master) | ||
|
||
MIME type compressible checking for Go | ||
|
||
## Installation | ||
|
||
``` | ||
go get -u github.com/go-http-utils/compressible | ||
``` | ||
|
||
## Documentation | ||
|
||
API documentation can be found here: https://godoc.org/github.com/go-http-utils/compressible | ||
|
||
## Usage | ||
|
||
```go | ||
import ( | ||
"github.com/go-http-utils/compressible" | ||
) | ||
``` | ||
|
||
```go | ||
fmt.Println(compressible.Test("text/html")) | ||
// -> true | ||
|
||
fmt.Println(compressible.Test("image/jpeg")) | ||
// -> false | ||
``` |
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,50 @@ | ||
package compressible | ||
|
||
import ( | ||
"mime" | ||
"regexp" | ||
|
||
"github.com/GitbookIO/mimedb" | ||
) | ||
|
||
func init() { | ||
// ensure all extensions and their associated content type of "mimedb" package | ||
// are stored in "mime" package, ignore the potential returned error of | ||
// mime.AddExtensionType. | ||
for ext, mimeEntry := range mimedb.DB { | ||
mime.AddExtensionType("."+ext, mimeEntry.ContentType) | ||
} | ||
} | ||
|
||
var compressibleTypeRegExp = regexp.MustCompile(`^text\/|\+json$|\+text$|\+xml$`) | ||
|
||
// Test checks whether the given contentType is compressible, using | ||
// https://github.com/GitbookIO/mimedb as mime database. All types that not in // mimedb but have the scheme of "text/*", "*/*+json", "*/*+text", "*/*+xml" are | ||
// considered as compressible. | ||
func Test(contentType string) bool { | ||
dbMatched := false | ||
|
||
exts, err := mime.ExtensionsByType(contentType) | ||
|
||
if err != nil { | ||
return false | ||
} | ||
|
||
for _, ext := range exts { | ||
// all exts returned by mime.ExtensionsByType are always | ||
// start with "." | ||
if entry, ok := mimedb.DB[ext[1:]]; ok { | ||
dbMatched = true | ||
|
||
if entry.Compressible { | ||
return true | ||
} | ||
} | ||
} | ||
|
||
if !dbMatched && compressibleTypeRegExp.MatchString(contentType) { | ||
return true | ||
} | ||
|
||
return false | ||
} |
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,30 @@ | ||
package compressible | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/GitbookIO/mimedb" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type CompressibleSuite struct { | ||
suite.Suite | ||
} | ||
|
||
func (s CompressibleSuite) TestMimeDbKeys() { | ||
for _, mimeEntry := range mimedb.DB { | ||
s.Equal(mimeEntry.Compressible, Test(mimeEntry.ContentType)) | ||
} | ||
} | ||
|
||
func (s CompressibleSuite) TestInvalidType() { | ||
s.False(Test("foo/bar")) | ||
} | ||
|
||
func (s CompressibleSuite) TestMatchScheme() { | ||
s.True(Test("text/foobar")) | ||
} | ||
|
||
func TestCompressible(t *testing.T) { | ||
suite.Run(t, new(CompressibleSuite)) | ||
} |
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,2 @@ | ||
// Package compressible provides MIME type compressible checking for Go | ||
package compressible |