-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
72 lines (69 loc) · 1.76 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package diskcache_test
import (
"bytes"
"fmt"
"log"
"net/http"
"net/http/httptest"
"net/http/httputil"
"time"
"github.com/kenshaw/diskcache"
)
// Example demonstrates setting up a simple diskcache for use with a
// http.Client.
func Example() {
// set up simple test server for demonstration
s := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
res.Header().Set("content-type", "text/html")
res.Header().Set("X-Header", "test")
fmt.Fprintf(res, `<!doctype html>
<html lang="en">
<head>
</head>
<body attribute="value">
<p> hello %s! </p>
<div> something </div>
<a href="http://example.com/full/path">a link!</a>
</body>
</html>
`, req.URL.Query().Get("name"))
}))
defer s.Close()
// create disk cache
d, err := diskcache.New(
// diskcache.WithBasePathFs("/path/to/cacheDir"),
diskcache.WithAppCacheDir("diskcache-test"),
diskcache.WithHeaderBlacklist("Set-Cookie", "Date"),
diskcache.WithMinifier(),
diskcache.WithErrorTruncator(),
diskcache.WithGzipCompression(),
diskcache.WithTTL(365*24*time.Hour),
)
if err != nil {
log.Fatal(err)
}
// build and execute request
cl := &http.Client{Transport: d}
req, err := http.NewRequest("GET", s.URL+"/hello?name=ken", nil)
if err != nil {
log.Fatal(err)
}
res, err := cl.Do(req)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
// dump
buf, err := httputil.DumpResponse(res, true)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(bytes.ReplaceAll(buf, []byte("\r\n"), []byte("\n"))))
// Output:
// HTTP/1.1 200 OK
// Connection: close
// Content-Type: text/html
// X-Header: test
//
// <!doctype html><html lang=en><body attribute=value><p>hello ken!<div>something</div><a href=//example.com/full/path>a link!</a>
}