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

Commit1312cd9

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 parente189088 commit1312cd9

File tree

2 files changed

+45
-26
lines changed

2 files changed

+45
-26
lines changed

‎coderd/oauth2provider/metadata_test.go‎

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package oauth2provider_test
22

33
import (
44
"context"
5-
"encoding/json"
6-
"net/http"
75
"net/url"
86
"testing"
97

@@ -23,20 +21,11 @@ func TestOAuth2AuthorizationServerMetadata(t *testing.T) {
2321
ctx,cancel:=context.WithTimeout(context.Background(),testutil.WaitLong)
2422
defercancel()
2523

26-
// Use a plain HTTP client since this endpoint doesn't require authentication
24+
// Use a plain HTTP client since this endpoint doesn't require authentication.
25+
// Add a short readiness wait to avoid rare races with server startup.
2726
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-
3727
varmetadata codersdk.OAuth2AuthorizationServerMetadata
38-
err=json.NewDecoder(resp.Body).Decode(&metadata)
39-
require.NoError(t,err)
28+
testutil.RequireEventuallyResponseOK(ctx,t,endpoint,&metadata)
4029

4130
// Verify the metadata
4231
require.NotEmpty(t,metadata.Issuer)
@@ -57,20 +46,11 @@ func TestOAuth2ProtectedResourceMetadata(t *testing.T) {
5746
ctx,cancel:=context.WithTimeout(context.Background(),testutil.WaitLong)
5847
defercancel()
5948

60-
// Use a plain HTTP client since this endpoint doesn't require authentication
49+
// Use a plain HTTP client since this endpoint doesn't require authentication.
50+
// Add a short readiness wait to avoid rare races with server startup.
6151
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-
7152
varmetadata codersdk.OAuth2ProtectedResourceMetadata
72-
err=json.NewDecoder(resp.Body).Decode(&metadata)
73-
require.NoError(t,err)
53+
testutil.RequireEventuallyResponseOK(ctx,t,endpoint,&metadata)
7454

7555
// Verify the metadata
7656
require.NotEmpty(t,metadata.Resource)

‎testutil/http.go‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package testutil
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"net/http"
7+
"testing"
8+
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
// RequireEventuallyResponseOK makes HTTP GET requests to the given endpoint until it returns
13+
// 200 OK with a valid JSON response that can be decoded into target, or until the context
14+
// times out. This is useful for waiting for HTTP servers to become ready during tests,
15+
// especially for metadata endpoints that may not be immediately available.
16+
funcRequireEventuallyResponseOK(ctx context.Context,t testing.TB,endpointstring,targetinterface{}) {
17+
t.Helper()
18+
19+
ok:=Eventually(ctx,t,func(ctx context.Context) (donebool) {
20+
req,err:=http.NewRequestWithContext(ctx,http.MethodGet,endpoint,nil)
21+
iferr!=nil {
22+
returnfalse
23+
}
24+
resp,err:=http.DefaultClient.Do(req)
25+
iferr!=nil {
26+
returnfalse
27+
}
28+
deferresp.Body.Close()
29+
ifresp.StatusCode!=http.StatusOK {
30+
returnfalse
31+
}
32+
iferr:=json.NewDecoder(resp.Body).Decode(target);err!=nil {
33+
returnfalse
34+
}
35+
returntrue
36+
},IntervalFast)
37+
38+
require.True(t,ok,"endpoint %s not ready in time",endpoint)
39+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp