Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit652ebde

Browse files
committed
test(oauth2provider): deflake OAuth2 metadata tests with readiness wait
Fix flake in TestOAuth2AuthorizationServerMetadata by polling the .well-known endpoints until they return 200 and valid JSON, using testutil.Eventually.Root cause: the test server can accept requests before the real handler is installed in coderdtest, returning 200 with an empty body which causes JSON decode EOF. The readiness loop avoids this race without sleeps and holds under t.Parallel().This fixes the following issue:coder/internal#996Change-Id: I466815d35f03bb75ef448d6f3431cd4b6efe2570Signed-off-by: Thomas Kosiewski <tk@coder.com>
1 parent6d39077 commit652ebde

File tree

1 file changed

+42
-24
lines changed

1 file changed

+42
-24
lines changed

‎coderd/oauth2provider/metadata_test.go‎

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,29 @@ func TestOAuth2AuthorizationServerMetadata(t *testing.T) {
2323
ctx,cancel:=context.WithTimeout(context.Background(),testutil.WaitLong)
2424
defercancel()
2525

26-
// Use a plain HTTP client since this endpoint doesn't require authentication
26+
// Use a plain HTTP client since this endpoint doesn't require authentication.
27+
// Add a short readiness wait to avoid rare races with server startup.
2728
endpoint:=serverURL.ResolveReference(&url.URL{Path:"/.well-known/oauth-authorization-server"}).String()
28-
req,err:=http.NewRequestWithContext(ctx,http.MethodGet,endpoint,nil)
29-
require.NoError(t,err)
30-
31-
resp,err:=http.DefaultClient.Do(req)
32-
require.NoError(t,err)
33-
deferresp.Body.Close()
34-
35-
require.Equal(t,http.StatusOK,resp.StatusCode)
36-
3729
varmetadata codersdk.OAuth2AuthorizationServerMetadata
38-
err=json.NewDecoder(resp.Body).Decode(&metadata)
39-
require.NoError(t,err)
30+
ok:=testutil.Eventually(ctx,t,func(ctx context.Context) (donebool) {
31+
req,err:=http.NewRequestWithContext(ctx,http.MethodGet,endpoint,nil)
32+
iferr!=nil {
33+
returnfalse
34+
}
35+
resp,err:=http.DefaultClient.Do(req)
36+
iferr!=nil {
37+
returnfalse
38+
}
39+
deferresp.Body.Close()
40+
ifresp.StatusCode!=http.StatusOK {
41+
returnfalse
42+
}
43+
iferr:=json.NewDecoder(resp.Body).Decode(&metadata);err!=nil {
44+
returnfalse
45+
}
46+
returntrue
47+
},testutil.IntervalFast)
48+
require.True(t,ok,"authorization server metadata endpoint not ready in time")
4049

4150
// Verify the metadata
4251
require.NotEmpty(t,metadata.Issuer)
@@ -57,20 +66,29 @@ func TestOAuth2ProtectedResourceMetadata(t *testing.T) {
5766
ctx,cancel:=context.WithTimeout(context.Background(),testutil.WaitLong)
5867
defercancel()
5968

60-
// Use a plain HTTP client since this endpoint doesn't require authentication
69+
// Use a plain HTTP client since this endpoint doesn't require authentication.
70+
// Add a short readiness wait to avoid rare races with server startup.
6171
endpoint:=serverURL.ResolveReference(&url.URL{Path:"/.well-known/oauth-protected-resource"}).String()
62-
req,err:=http.NewRequestWithContext(ctx,http.MethodGet,endpoint,nil)
63-
require.NoError(t,err)
64-
65-
resp,err:=http.DefaultClient.Do(req)
66-
require.NoError(t,err)
67-
deferresp.Body.Close()
68-
69-
require.Equal(t,http.StatusOK,resp.StatusCode)
70-
7172
varmetadata codersdk.OAuth2ProtectedResourceMetadata
72-
err=json.NewDecoder(resp.Body).Decode(&metadata)
73-
require.NoError(t,err)
73+
ok:=testutil.Eventually(ctx,t,func(ctx context.Context) (donebool) {
74+
req,err:=http.NewRequestWithContext(ctx,http.MethodGet,endpoint,nil)
75+
iferr!=nil {
76+
returnfalse
77+
}
78+
resp,err:=http.DefaultClient.Do(req)
79+
iferr!=nil {
80+
returnfalse
81+
}
82+
deferresp.Body.Close()
83+
ifresp.StatusCode!=http.StatusOK {
84+
returnfalse
85+
}
86+
iferr:=json.NewDecoder(resp.Body).Decode(&metadata);err!=nil {
87+
returnfalse
88+
}
89+
returntrue
90+
},testutil.IntervalFast)
91+
require.True(t,ok,"protected resource metadata endpoint not ready in time")
7492

7593
// Verify the metadata
7694
require.NotEmpty(t,metadata.Resource)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp