-
Notifications
You must be signed in to change notification settings - Fork 46
/
got_test.go
114 lines (77 loc) · 1.75 KB
/
got_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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package got_test
import (
"context"
"fmt"
"log"
"net/http"
"net/http/httptest"
"strings"
"time"
"github.com/melbahja/got"
)
func NewHttptestServer() *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.String() {
case "/ok_file":
http.ServeFile(w, r, "go.mod")
return
case "/found_and_head_not_allowed":
if r.Method == http.MethodHead {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
fmt.Fprint(w, "helloworld")
return
case "/not_found_and_method_not_allowed":
w.WriteHeader(http.StatusMethodNotAllowed)
return
case "/ok_file_with_range_delay":
if r.Method == http.MethodGet && strings.Contains(r.Header.Get("range"), "3-") {
time.Sleep(3 * time.Second)
}
http.ServeFile(w, r, "go.mod")
return
case "/file_name":
w.Header().Set("Content-Disposition", `attachment; filename="go.mod"`)
http.ServeFile(w, r, "go.mod")
return
case "/header_values":
if r.Header.Get("x-test-header") == "foobar" {
http.ServeFile(w, r, "go.mod")
return
}
w.WriteHeader(403)
return
case "/not_found":
}
w.WriteHeader(http.StatusNotFound)
}))
}
var testUrl = httpt.URL + "/ok_file"
func ExampleGot() {
// Just for testing
destPath := createTemp()
defer clean(destPath)
g := got.New()
err := g.Download(testUrl, destPath)
if err != nil {
log.Fatal(err)
return
}
fmt.Println("done")
// Output: done
}
func ExampleGot_withContext() {
// Just for testing
destPath := createTemp()
defer clean(destPath)
ctx := context.Background()
g := got.NewWithContext(ctx)
err := g.Download(testUrl, destPath)
if err != nil {
log.Fatal(err)
return
}
fmt.Println("done")
// Output: done
}