Skip to content

Commit

Permalink
0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidCai1111 committed Nov 20, 2016
1 parent ef2e7f4 commit a03506f
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 1 deletion.
11 changes: 11 additions & 0 deletions .travis.yml
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
32 changes: 31 additions & 1 deletion README.md
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
```
50 changes: 50 additions & 0 deletions compressible.go
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
}
30 changes: 30 additions & 0 deletions compressible_test.go
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))
}
2 changes: 2 additions & 0 deletions doc.go
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

0 comments on commit a03506f

Please sign in to comment.