Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions internal/auth/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,30 +335,33 @@ func TestServiceLoginAcceptsAutomaticLocalCallback(t *testing.T) {
t.Fatalf("Listen() error = %v", err)
}
addr := l.Addr().String()
_ = l.Close()
t.Cleanup(func() {
_ = l.Close()
})

state := "state-1"
store := &memoryTokenStore{}
browser := &stubBrowser{}
out := &bytes.Buffer{}
manualIn, manualWriter := io.Pipe()
defer manualWriter.Close()
listenerReady := make(chan struct{})

done := make(chan struct{})
go func() {
defer close(done)
for len(browser.urls) == 0 {
time.Sleep(10 * time.Millisecond)
}
<-listenerReady
callbackURL := "http://" + addr + "/callback?code=code-1&state=" + state
resp, err := http.Get(callbackURL)
resp, err := (&http.Client{Timeout: time.Second}).Get(callbackURL)
if err != nil {
t.Errorf("http.Get() error = %v", err)
return
}
_ = resp.Body.Close()
}()

ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
token, err := Service{
Exchanger: Exchanger{
HTTPClient: tokenServer.Client(),
Expand All @@ -369,7 +372,14 @@ func TestServiceLoginAcceptsAutomaticLocalCallback(t *testing.T) {
In: manualIn,
Out: out,
StateSource: func() string { return state },
}.Login(context.Background(), LoginInput{
Listen: func(network, address string) (net.Listener, error) {
if network != "tcp" || address != addr {
t.Fatalf("Listen(%q, %q), want tcp %s", network, address, addr)
}
close(listenerReady)
return l, nil
},
}.Login(ctx, LoginInput{
ClientID: "client-1",
ClientSecret: "secret-1",
RedirectURL: "http://" + addr + "/callback",
Expand Down
Loading