Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
109 lines (87 loc) · 2.42 KB

File metadata and controls

109 lines (87 loc) · 2.42 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package main
import (
"context"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/cockroachdb/errors"
"github.com/prometheus/client_golang/prometheus"
)
func TestInstrumentHandler(t *testing.T) {
h := http.Handler(nil)
instrumentHandler(prometheus.DefaultRegisterer, h)
}
func TestGitHubProxy(t *testing.T) {
ch := make(chan struct{})
blocking := make(chan struct{})
p := &githubProxy{client: doerFunc(func(r *http.Request) (*http.Response, error) {
switch r.URL.Path {
case "/block":
select {
case <-ch:
default:
close(blocking)
<-ch
}
}
return &http.Response{
StatusCode: 200,
Header: make(http.Header),
Body: io.NopCloser(strings.NewReader("body")),
}, nil
})}
srv := httptest.NewServer(p)
t.Cleanup(srv.Close)
go func() {
req, _ := http.NewRequest("GET", srv.URL+"/block", nil)
req.Header.Add("Authorization", "user1")
http.DefaultClient.Do(req) // blocks
}()
t.Run("locked", func(t *testing.T) {
<-blocking
req, _ := http.NewRequest("GET", srv.URL+"/ok", nil)
req.Header.Add("Authorization", "user1")
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
_, err := http.DefaultClient.Do(req.WithContext(ctx))
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatal(err)
}
})
t.Run("different user", func(t *testing.T) {
req, _ := http.NewRequest("GET", srv.URL+"/ok", nil)
// Different user request can go through
req.Header.Set("Authorization", "Bearer user2")
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
resp, err := http.DefaultClient.Do(req.WithContext(ctx))
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != 200 {
t.Fatalf("want status code 200, got %v", resp.StatusCode)
}
})
t.Run("unlocked", func(t *testing.T) {
// Now the first user's request will finish, we can go through.
close(ch)
req, _ := http.NewRequest("GET", srv.URL+"/ok", nil)
req.Header.Set("Authorization", "Bearer user1")
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
resp, err := http.DefaultClient.Do(req.WithContext(ctx))
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != 200 {
t.Fatalf("want status code 200, got %v", resp.StatusCode)
}
})
}
type doerFunc func(*http.Request) (*http.Response, error)
func (do doerFunc) Do(r *http.Request) (*http.Response, error) {
return do(r)
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.