Skip to content

Commit

Permalink
use go-http-utils/headers
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidCai1111 committed Nov 16, 2016
1 parent 2cdc18e commit 4adc01d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 53 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ import (
"net/http"

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

```go
reqHeader, resHeader := make(http.Header), make(http.Header)

reqHeader.Set("if-none-match", "foo")
resHeader.Set("etag", "bar")
reqHeader.Set(headers.IfNoneMatch, "foo")
resHeader.Set(headers.ETag, "bar")

fresh.IsFresh(reqHeader, resHeader)
// -> false
Expand All @@ -37,8 +38,8 @@ fresh.IsFresh(reqHeader, resHeader)
```go
reqHeader, resHeader := make(http.Header), make(http.Header)

reqHeader.Set("if-modified-since", "Mon, 14 Nov 2016 22:05:49 GMT")
resHeader.Set("last-modified", "Mon, 14 Nov 2016 22:05:47 GMT")
reqHeader.Set(headers.IfModifiedSince, "Mon, 14 Nov 2016 22:05:49 GMT")
resHeader.Set(headers.LastModified, "Mon, 14 Nov 2016 22:05:47 GMT")

fresh.IsFresh(reqHeader, resHeader)
// -> true
Expand Down
23 changes: 8 additions & 15 deletions fresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,23 @@ import (
"net/http"
"strings"
"time"

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

// Version is this package's verison
const Version = "0.1.0"

// HTTP header fileds
const (
HeaderIfModifiedSince = "if-modified-since"
HeaderIfNoneMatch = "if-none-match"
HeaderCacheControl = "cache-control"
HeaderETag = "etag"
HeaderLastModified = "last-modified"
)
const Version = "0.2.0"

// IsFresh check whether cache can be used in this HTTP request
func IsFresh(reqHeader http.Header, resHeader http.Header) bool {
isEtagMatched, isModifiedMatched := false, false

ifModifiedSince := reqHeader.Get(HeaderIfModifiedSince)
ifNoneMatch := reqHeader.Get(HeaderIfNoneMatch)
cacheControl := reqHeader.Get(HeaderCacheControl)
ifModifiedSince := reqHeader.Get(headers.IfModifiedSince)
ifNoneMatch := reqHeader.Get(headers.IfNoneMatch)
cacheControl := reqHeader.Get(headers.CacheControl)

etag := resHeader.Get(HeaderETag)
lastModified := resHeader.Get(HeaderLastModified)
etag := resHeader.Get(headers.ETag)
lastModified := resHeader.Get(headers.LastModified)

if ifModifiedSince == "" && ifNoneMatch == "" {
return false
Expand Down
69 changes: 35 additions & 34 deletions fresh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"
"time"

"github.com/go-http-utils/headers"
"github.com/stretchr/testify/suite"
)

Expand All @@ -25,109 +26,109 @@ func (s FreshSuite) TestEtagEmpty() {
}

func (s FreshSuite) TestEtagMatch() {
s.reqHeader.Set(HeaderIfNoneMatch, "foo")
s.resHeader.Set(HeaderETag, "foo")
s.reqHeader.Set(headers.IfNoneMatch, "foo")
s.resHeader.Set(headers.ETag, "foo")

s.True(IsFresh(s.reqHeader, s.resHeader))
}

func (s FreshSuite) TestEtagMismatch() {
s.reqHeader.Set(HeaderIfNoneMatch, "foo")
s.resHeader.Set(HeaderETag, "bar")
s.reqHeader.Set(headers.IfNoneMatch, "foo")
s.resHeader.Set(headers.ETag, "bar")

s.False(IsFresh(s.reqHeader, s.resHeader))
}

func (s FreshSuite) TestEtagMissing() {
s.reqHeader.Set(HeaderIfNoneMatch, "foo")
s.reqHeader.Set(headers.IfNoneMatch, "foo")

s.False(IsFresh(s.reqHeader, s.resHeader))
}

func (s FreshSuite) TestWeakEtagMatch() {
s.reqHeader.Set(HeaderIfNoneMatch, `W/"foo"`)
s.resHeader.Set(HeaderETag, `W/"foo"`)
s.reqHeader.Set(headers.IfNoneMatch, `W/"foo"`)
s.resHeader.Set(headers.ETag, `W/"foo"`)

s.True(IsFresh(s.reqHeader, s.resHeader))
}

func (s FreshSuite) TestEtagStrongMatch() {
s.reqHeader.Set(HeaderIfNoneMatch, `W/"foo"`)
s.resHeader.Set(HeaderETag, `"foo"`)
s.reqHeader.Set(headers.IfNoneMatch, `W/"foo"`)
s.resHeader.Set(headers.ETag, `"foo"`)

s.True(IsFresh(s.reqHeader, s.resHeader))
}

func (s FreshSuite) TestStaleOnEtagWeakMatch() {
s.reqHeader.Set(HeaderIfNoneMatch, `"foo"`)
s.resHeader.Set(HeaderETag, `W/"foo"`)
s.reqHeader.Set(headers.IfNoneMatch, `"foo"`)
s.resHeader.Set(headers.ETag, `W/"foo"`)

s.False(IsFresh(s.reqHeader, s.resHeader))
}

func (s FreshSuite) TestEtagAsterisk() {
s.reqHeader.Set(HeaderIfNoneMatch, "*")
s.resHeader.Set(HeaderETag, `"foo"`)
s.reqHeader.Set(headers.IfNoneMatch, "*")
s.resHeader.Set(headers.ETag, `"foo"`)

s.True(IsFresh(s.reqHeader, s.resHeader))
}

func (s FreshSuite) TestModifiedFresh() {
s.reqHeader.Set(HeaderIfModifiedSince, getFormattedTime(4*time.Second))
s.resHeader.Set(HeaderLastModified, getFormattedTime(2*time.Second))
s.reqHeader.Set(headers.IfModifiedSince, getFormattedTime(4*time.Second))
s.resHeader.Set(headers.LastModified, getFormattedTime(2*time.Second))

s.True(IsFresh(s.reqHeader, s.resHeader))
}

func (s FreshSuite) TestModifiedStale() {
s.reqHeader.Set(HeaderIfModifiedSince, getFormattedTime(2*time.Second))
s.resHeader.Set(HeaderLastModified, getFormattedTime(4*time.Second))
s.reqHeader.Set(headers.IfModifiedSince, getFormattedTime(2*time.Second))
s.resHeader.Set(headers.LastModified, getFormattedTime(4*time.Second))

s.False(IsFresh(s.reqHeader, s.resHeader))
}

func (s FreshSuite) TestEmptyLastModified() {
s.reqHeader.Set(HeaderIfModifiedSince, getFormattedTime(4*time.Second))
s.reqHeader.Set(headers.IfModifiedSince, getFormattedTime(4*time.Second))

s.False(IsFresh(s.reqHeader, s.resHeader))
}

func (s FreshSuite) TestBoshAndModifiedFresh() {
s.reqHeader.Set(HeaderIfNoneMatch, "foo")
s.reqHeader.Set(HeaderIfModifiedSince, getFormattedTime(4*time.Second))
s.reqHeader.Set(headers.IfNoneMatch, "foo")
s.reqHeader.Set(headers.IfModifiedSince, getFormattedTime(4*time.Second))

s.resHeader.Set(HeaderETag, "bar")
s.resHeader.Set(HeaderLastModified, getFormattedTime(2*time.Second))
s.resHeader.Set(headers.ETag, "bar")
s.resHeader.Set(headers.LastModified, getFormattedTime(2*time.Second))

s.True(IsFresh(s.reqHeader, s.resHeader))
}

func (s FreshSuite) TestBoshAndETagFresh() {
s.reqHeader.Set(HeaderIfNoneMatch, "foo")
s.reqHeader.Set(HeaderIfModifiedSince, getFormattedTime(2*time.Second))
s.reqHeader.Set(headers.IfNoneMatch, "foo")
s.reqHeader.Set(headers.IfModifiedSince, getFormattedTime(2*time.Second))

s.resHeader.Set(HeaderETag, "foo")
s.resHeader.Set(HeaderLastModified, getFormattedTime(4*time.Second))
s.resHeader.Set(headers.ETag, "foo")
s.resHeader.Set(headers.LastModified, getFormattedTime(4*time.Second))

s.True(IsFresh(s.reqHeader, s.resHeader))
}

func (s FreshSuite) TestBoshFresh() {
s.reqHeader.Set(HeaderIfNoneMatch, "foo")
s.reqHeader.Set(HeaderIfModifiedSince, getFormattedTime(4*time.Second))
s.reqHeader.Set(headers.IfNoneMatch, "foo")
s.reqHeader.Set(headers.IfModifiedSince, getFormattedTime(4*time.Second))

s.resHeader.Set(HeaderETag, "foo")
s.resHeader.Set(HeaderLastModified, getFormattedTime(2*time.Second))
s.resHeader.Set(headers.ETag, "foo")
s.resHeader.Set(headers.LastModified, getFormattedTime(2*time.Second))

s.True(IsFresh(s.reqHeader, s.resHeader))
}

func (s FreshSuite) TestBoshStale() {
s.reqHeader.Set(HeaderIfNoneMatch, "foo")
s.reqHeader.Set(HeaderIfModifiedSince, getFormattedTime(2*time.Second))
s.reqHeader.Set(headers.IfNoneMatch, "foo")
s.reqHeader.Set(headers.IfModifiedSince, getFormattedTime(2*time.Second))

s.resHeader.Set(HeaderETag, "bar")
s.resHeader.Set(HeaderLastModified, getFormattedTime(4*time.Second))
s.resHeader.Set(headers.ETag, "bar")
s.resHeader.Set(headers.LastModified, getFormattedTime(4*time.Second))

s.False(IsFresh(s.reqHeader, s.resHeader))
}
Expand Down

0 comments on commit 4adc01d

Please sign in to comment.