-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathinit_test.go
41 lines (33 loc) · 1.06 KB
/
init_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
package imdb
import (
"net/http"
"os"
"time"
"github.com/StalkR/httpcache"
)
const userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"
const cacheTTL = 24 * time.Hour
// client is used by tests to perform cached requests.
// If cache directory exists it is used as a persistent cache.
// Otherwise a volatile memory cache is used.
var client *http.Client
func init() {
if _, err := os.Stat("cache"); err == nil {
client, err = httpcache.NewPersistentClient("cache", cacheTTL)
if err != nil {
panic(err)
}
} else {
client = httpcache.NewVolatileClient(cacheTTL, 1024)
}
client.Transport = &customTransport{client.Transport}
}
// customTransport implements http.RoundTripper interface to add some headers.
type customTransport struct {
http.RoundTripper
}
func (e *customTransport) RoundTrip(r *http.Request) (*http.Response, error) {
r.Header.Set("Accept-Language", "en") // avoid IP-based language detection
r.Header.Set("User-Agent", userAgent)
return e.RoundTripper.RoundTrip(r)
}