Repository navigation
Expand file tree
/
Copy pathreadonly_test.go
More file actions
106 lines (95 loc) · 2.72 KB
/
Copy pathreadonly_test.go
File metadata and controls
106 lines (95 loc) · 2.72 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
package forges
import (
"errors"
"io"
"net/http"
"net/http/httptest"
"strings"
"sync/atomic"
"testing"
"time"
)
func TestReadOnlyHTTPClientAllowsReads(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = io.WriteString(w, r.Method)
}))
defer srv.Close()
hc := ReadOnlyHTTPClient(srv.Client())
for _, method := range []string{"", http.MethodGet, http.MethodHead, http.MethodOptions} {
req, err := http.NewRequest(method, srv.URL, nil)
if err != nil {
t.Fatal(err)
}
resp, err := hc.Do(req)
if err != nil {
t.Fatalf("%q: unexpected error: %v", method, err)
}
_ = resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Errorf("%q: status = %d, want 200", method, resp.StatusCode)
}
}
}
type closeRecorder struct {
io.Reader
closed bool
}
func (c *closeRecorder) Close() error {
c.closed = true
return nil
}
func TestReadOnlyHTTPClientRefusesWrites(t *testing.T) {
var hits atomic.Int32
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
hits.Add(1)
}))
defer srv.Close()
hc := ReadOnlyHTTPClient(srv.Client())
methods := []string{http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodDelete, http.MethodConnect, http.MethodTrace, "PROPFIND"}
for _, method := range methods {
body := &closeRecorder{Reader: strings.NewReader(`{"title":"x"}`)}
req, err := http.NewRequest(method, srv.URL+"/repos/o/r/issues", body)
if err != nil {
t.Fatal(err)
}
resp, err := hc.Do(req)
if resp != nil {
_ = resp.Body.Close()
}
if !errors.Is(err, ErrReadOnly) {
t.Errorf("%s: err = %v, want ErrReadOnly", method, err)
}
if !body.closed {
t.Errorf("%s: request body was not closed", method)
}
}
if n := hits.Load(); n != 0 {
t.Errorf("server received %d requests, want 0", n)
}
}
func TestReadOnlyHTTPClientCopiesClient(t *testing.T) {
base := &http.Client{Timeout: time.Minute}
hc := ReadOnlyHTTPClient(base)
if hc == base {
t.Fatal("ReadOnlyHTTPClient returned the client it was given")
}
if base.Transport != nil {
t.Errorf("original client's transport was changed to %T", base.Transport)
}
if hc.Timeout != time.Minute {
t.Errorf("Timeout = %v, want %v", hc.Timeout, time.Minute)
}
}
func TestReadOnlyHTTPClientNil(t *testing.T) {
hc := ReadOnlyHTTPClient(nil)
if hc == http.DefaultClient {
t.Fatal("ReadOnlyHTTPClient(nil) returned http.DefaultClient")
}
if http.DefaultClient.Transport != nil {
t.Errorf("http.DefaultClient's transport was changed to %T", http.DefaultClient.Transport)
}
_, err := hc.Post("http://127.0.0.1:0/", "application/json", strings.NewReader("{}"))
if !errors.Is(err, ErrReadOnly) {
t.Errorf("err = %v, want ErrReadOnly", err)
}
}