-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathlargetfile_test.go
56 lines (52 loc) · 1.3 KB
/
largetfile_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
package netemx
import (
"context"
"crypto/rand"
"errors"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
func TestLargeFileHandler(t *testing.T) {
t.Run("we get 500 if reading fails", func(t *testing.T) {
req := &http.Request{
URL: &url.URL{Path: "/"},
Body: http.NoBody,
Close: false,
Host: "largefile.com",
}
rr := httptest.NewRecorder()
handler := LargeFileHandler(func(b []byte) (n int, err error) {
return 0, errors.New("cannot read large file")
})
handler.ServeHTTP(rr, req)
result := rr.Result()
if result.StatusCode != http.StatusInternalServerError {
t.Fatal("unexpected status code", result.StatusCode)
}
})
t.Run("otherwise we get a large file", func(t *testing.T) {
req := &http.Request{
URL: &url.URL{Path: "/"},
Body: http.NoBody,
Close: false,
Host: "yandex.com",
}
rr := httptest.NewRecorder()
handler := LargeFileHandler(rand.Read)
handler.ServeHTTP(rr, req)
result := rr.Result()
if result.StatusCode != http.StatusOK {
t.Fatal("unexpected status code", result.StatusCode)
}
data, err := netxlite.ReadAllContext(context.Background(), result.Body)
if err != nil {
t.Fatal(err)
}
if len(data) != 1<<25 {
t.Fatal("cannot read the whole response body")
}
})
}