forked from melbahja/got
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgot_test.go
More file actions
132 lines (92 loc) 路 2.25 KB
/
Copy pathgot_test.go
File metadata and controls
132 lines (92 loc) 路 2.25 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package got_test
import (
"context"
"fmt"
"log"
"net/http"
"net/http/httptest"
"strings"
"time"
"github.com/bitrise-io/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 "/1_byte_file":
w.Header().Set("Content-Length", "1")
w.Header().Set("Content-Range", "bytes 0-0/1")
http.ServeFile(w, r, "testdata/1-byte.txt")
return
case "/2_byte_file":
w.Header().Set("Content-Length", "1")
w.Header().Set("Content-Range", "bytes 0-0/2")
http.ServeFile(w, r, "testdata/2-byte.txt")
return
case "/3_byte_file":
w.Header().Set("Content-Length", "1")
w.Header().Set("Content-Range", "bytes 0-0/3")
http.ServeFile(w, r, "testdata/3-byte.txt")
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
}