lox / httpcache

An RFC7234 compliant golang http.Handler for caching HTTP responses
MIT License
264 stars 28 forks source link

Don't get it working with max-age #37

Closed StarpTech closed 7 years ago

StarpTech commented 7 years ago

Hi, II just found you library and would like to implement an reserve proxy with HTTP caching but the example in the bench_test.go dies not work. I expect that the payload is cached.

    backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        w.Header().Add("Cache-Control", "max-age=100000")
        fmt.Fprintf(w, "cache server payload")
        fmt.Print("Inbound\n")
    }))
    defer backend.Close()

    u, err := url.Parse(backend.URL)

    if err != nil {
        panic(err)
    }

    handler := httpcache.NewHandler(httpcache.NewMemoryCache(), httputil.NewSingleHostReverseProxy(u))
    handler.Shared = true
    cacheServer := httptest.NewServer(handler)
    defer cacheServer.Close()

    for n := 0; n < 5; n++ {
        client := http.Client{}
        resp, err := client.Get(fmt.Sprintf("%s/llamas/%d", cacheServer.URL, n))
        if err != nil {
            panic(err)
        }
        resp.Body.Close()
    }
Inbound
Inbound
Inbound
Inbound
Inbound
StarpTech commented 7 years ago

This was my mistake I didnt notice that the loop variable is appended to the url.