-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_request_test.go
52 lines (44 loc) · 1.06 KB
/
example_request_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
package grab
import (
"context"
"crypto/sha256"
"encoding/hex"
"fmt"
"time"
)
func ExampleRequest_WithContext() {
// create context with a 100ms timeout
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
// create download request with context
req, err := NewRequest("", "http://example.com/example.zip")
if err != nil {
panic(err)
}
req = req.WithContext(ctx)
// send download request
resp := DefaultClient.Do(req)
if err := resp.Err(); err != nil {
fmt.Println("error: request cancelled")
}
// Output:
// error: request cancelled
}
func ExampleRequest_SetChecksum() {
// create download request
req, err := NewRequest("", "http://example.com/example.zip")
if err != nil {
panic(err)
}
// set request checksum
sum, err := hex.DecodeString("33daf4c03f86120fdfdc66bddf6bfff4661c7ca11c5da473e537f4d69b470e57")
if err != nil {
panic(err)
}
req.SetChecksum(sha256.New(), sum, true)
// download and validate file
resp := DefaultClient.Do(req)
if err := resp.Err(); err != nil {
panic(err)
}
}