Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e7ececa

Browse files
committed
add tests
1 parent 4572257 commit e7ececa

File tree

4 files changed

+39
-14
lines changed

4 files changed

+39
-14
lines changed

coderd/httpmw/apikey.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,20 @@ type OAuth2Configs struct {
4343
func ExtractAPIKey(db database.Store, oauth *OAuth2Configs) func(http.Handler) http.Handler {
4444
return func(next http.Handler) http.Handler {
4545
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
46+
var cookieValue string
4647
cookie, err := r.Cookie(AuthCookie)
4748
if err != nil {
49+
cookieValue = r.URL.Query().Get(AuthCookie)
50+
} else {
51+
cookieValue = cookie.Value
52+
}
53+
if cookieValue == "" {
4854
httpapi.Write(rw, http.StatusUnauthorized, httpapi.Response{
49-
Message: fmt.Sprintf("%q cookie must be provided", AuthCookie),
55+
Message: fmt.Sprintf("%q cookie or query parameter must be provided", AuthCookie),
5056
})
5157
return
5258
}
53-
parts := strings.Split(cookie.Value, "-")
59+
parts := strings.Split(cookieValue, "-")
5460
// APIKeys are formatted: ID-SECRET
5561
if len(parts) != 2 {
5662
httpapi.Write(rw, http.StatusUnauthorized, httpapi.Response{

coderd/workspaces_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,3 +612,28 @@ func mustLocation(t *testing.T, location string) *time.Location {
612612

613613
return loc
614614
}
615+
616+
func TestWorkspaceWatcher(t *testing.T) {
617+
t.Parallel()
618+
client := coderdtest.New(t, nil)
619+
user := coderdtest.CreateFirstUser(t, client)
620+
coderdtest.NewProvisionerDaemon(t, client)
621+
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
622+
coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
623+
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
624+
workspace := coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID)
625+
w, err := client.Workspace(context.Background(), workspace.ID)
626+
require.NoError(t, err)
627+
628+
ww, err := client.WatchWorkspace(context.Background(), w.ID)
629+
require.NoError(t, err)
630+
defer ww.Close()
631+
for i := 0; i < 5; i++ {
632+
_, err := ww.Read(context.Background())
633+
require.NoError(t, err)
634+
}
635+
err = ww.Close()
636+
require.NoError(t, err)
637+
_, err = ww.Read(context.Background())
638+
require.Error(t, err)
639+
}

codersdk/client.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ func (c *Client) Request(ctx context.Context, method, path string, body interfac
8181
return resp, err
8282
}
8383

84-
// request performs an HTTP request with the body provided.
85-
// The caller is responsible for closing the response body.
84+
// websocket opens a websocket connection on that path provided.
85+
// The caller is responsible for closing the websocket.Conn.
8686
func (c *Client) websocket(ctx context.Context, path string) (*websocket.Conn, error) {
8787
serverURL, err := c.URL.Parse(path)
8888
if err != nil {
@@ -95,15 +95,9 @@ func (c *Client) websocket(ctx context.Context, path string) (*websocket.Conn, e
9595
apiURL.Scheme = "wss"
9696
}
9797
apiURL.Path = path
98-
99-
client := &http.Client{
100-
Jar: c.HTTPClient.Jar,
101-
}
102-
cookies := append(client.Jar.Cookies(c.URL), &http.Cookie{
103-
Name: httpmw.AuthCookie,
104-
Value: c.SessionToken,
105-
})
106-
client.Jar.SetCookies(c.URL, cookies)
98+
q := apiURL.Query()
99+
q.Add(httpmw.AuthCookie, c.SessionToken)
100+
apiURL.RawQuery = q.Encode()
107101

108102
//nolint:bodyclose
109103
conn, _, err := websocket.Dial(context.Background(), apiURL.String(), &websocket.DialOptions{

codersdk/workspaces.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func (w *WorkspaceWatcher) Close() error {
117117
return nil
118118
}
119119

120-
func (c *Client) WorkspaceWatcher(ctx context.Context, id uuid.UUID) (*WorkspaceWatcher, error) {
120+
func (c *Client) WatchWorkspace(ctx context.Context, id uuid.UUID) (*WorkspaceWatcher, error) {
121121
conn, err := c.websocket(ctx, fmt.Sprintf("/api/v2/workspaces/%s/watch", id))
122122
if err != nil {
123123
return nil, err

0 commit comments

Comments
 (0)