forked from earthboundkid/requests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redirects_example_test.go
55 lines (49 loc) · 1.18 KB
/
redirects_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
package requests_test
import (
"context"
"fmt"
"net/http"
"github.com/carlmjohnson/requests"
)
func ExampleCheckRedirectPolicy() {
cl := *http.DefaultClient
cl.CheckRedirect = requests.NoFollow
if err := requests.
URL("https://httpbingo.org/redirect/1").
Client(&cl).
CheckStatus(http.StatusFound).
Handle(func(res *http.Response) error {
fmt.Println("Status", res.Status)
fmt.Println("From", res.Request.URL)
fmt.Println("To", res.Header.Get("Location"))
return nil
}).
Fetch(context.Background()); err != nil {
panic(err)
}
// Output:
// Status 302 Found
// From https://httpbingo.org/redirect/1
// To /get
}
func ExampleMaxFollow() {
cl := *http.DefaultClient
cl.CheckRedirect = requests.MaxFollow(1)
if err := requests.
URL("https://httpbingo.org/redirect/2").
Client(&cl).
CheckStatus(http.StatusFound).
Handle(func(res *http.Response) error {
fmt.Println("Status", res.Status)
fmt.Println("From", res.Request.URL)
fmt.Println("To", res.Header.Get("Location"))
return nil
}).
Fetch(context.Background()); err != nil {
panic(err)
}
// Output:
// Status 302 Found
// From https://httpbingo.org/relative-redirect/1
// To /get
}