Skip to content
This repository has been archived by the owner on Apr 18, 2021. It is now read-only.

Commit

Permalink
Fixes #2 Change net.http/Headers.set in favor of Add()
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasazrak committed Oct 25, 2016
1 parent 52d15a8 commit 25084ff
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
4 changes: 1 addition & 3 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type CacheHandler struct {
func respond(response * storage.CachedResponse, w http.ResponseWriter) {
for k, values := range response.HeaderMap {
for _, v := range values {
w.Header().Set(k, v)
w.Header().Add(k, v)
}
}
w.WriteHeader(response.Code)
Expand All @@ -32,7 +32,6 @@ func respond(response * storage.CachedResponse, w http.ResponseWriter) {
func shouldUseCache(r *http.Request, config *Config) bool {
// TODO Add more logic like get params, ?nocache=true


if r.Method != "GET" && r.Method != "HEAD" {
// Only cache Get and head request
return false
Expand Down Expand Up @@ -79,7 +78,6 @@ func getCacheableStatus(req *http.Request, res *httptest.ResponseRecorder, confi
cacheobject.ExpirationObject(&obj, &rv)

isCacheable := len(rv.OutReasons) == 0

expiration := rv.OutExpirationTime
if expiration.Before(time.Now().UTC().Add(time.Duration(1) * time.Second)) {
// If expiration is before now use default MaxAge
Expand Down
35 changes: 30 additions & 5 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,22 @@ type TestHandler struct {

func (h *TestHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
h.timesCalled = h.timesCalled + 1
w.Write(h.ResponseBody)
w.WriteHeader(h.ResponseCode)
for k, values := range h.ResponseHeaders {
for _, v := range values {
w.Header().Set(k, v)
w.Header().Add(k, v)
}
}
w.WriteHeader(h.ResponseCode)
w.Write(h.ResponseBody)
return h.ResponseCode, h.ResponseError
}

func buildBasicHandler(cacheablePaths string) (*CacheHandler, *TestHandler) {
memory := storage.MemoryStorage{}
memory.Setup()
backend := TestHandler{}
backend := TestHandler{
ResponseCode: 200,
}

return &CacheHandler{
Config: &Config {
Expand All @@ -53,6 +55,8 @@ func buildGetRequest(path string) *http.Request {

}


// TODO avoid code duplication, use r.Run
func TestBasicCache(t *testing.T) {
handler, backend := buildBasicHandler("/assets")
rec := httptest.NewRecorder()
Expand Down Expand Up @@ -106,7 +110,7 @@ func TestNotCacheableCacheControl(t *testing.T) {
handler, backend := buildBasicHandler("/assets")
rec := httptest.NewRecorder()

responseHeaders := make(map[string][]string)
responseHeaders := make(http.Header)
responseHeaders["Cache-control"] = []string { "private" }
backend.ResponseHeaders = responseHeaders

Expand All @@ -121,6 +125,27 @@ func TestNotCacheableCacheControl(t *testing.T) {
assert.Equal(t, 2, backend.timesCalled, "Backend should have been called 2 but it was called", backend.timesCalled)
}

func TestAddHeaders(t *testing.T) {
handler, backend := buildBasicHandler("/assets")

responseHeaders := make(http.Header)
responseHeaders["Content-Type"] = []string { "text/plain; charset=utf-8" }
responseHeaders["X-Custom-2"] = []string { "bar", "baz" }
responseHeaders["X-Custom"] = []string { "foo", "bar", "baz" }
backend.ResponseHeaders = responseHeaders

req := buildGetRequest("http://somehost.com/assets/1")

rec := httptest.NewRecorder()
_, err := handler.ServeHTTP(rec, req)

if err != nil {
assert.Fail(t, "Error processing request", err)
}

assert.Equal(t, responseHeaders, rec.HeaderMap, "Cache didn't send same headers that backend originally sent")
}

func TestDefaultCacheTime(t *testing.T) {
// TODO test this
// isCacheable, expiration := getCacheableStatus(req, res, config)
Expand Down

0 comments on commit 25084ff

Please sign in to comment.