Skip to content
Merged
Show file tree
Hide file tree
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
65 changes: 65 additions & 0 deletions pkg/daemon/platform/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package daemon

import (
"encoding/json"
"errors"
"fmt"
"net/http"
)

// statusError is an error that carries the HTTP status code to respond with.
type statusError struct {
code int
err error
}

func (e statusError) Error() string {
return e.err.Error()
}

func (e statusError) Unwrap() error {
return e.err
}

func badRequest(err error) error {
return statusError{code: http.StatusBadRequest, err: err}
}

func forbidden(err error) error {
return statusError{code: http.StatusForbidden, err: err}
}

func missingQueryParam(name string) error {
return badRequest(fmt.Errorf("missing required query parameter %q", name))
}

// handlerFunc is an HTTP handler that reports failures by returning them.
// A statusError supplies the response code; any other error is a 500.
// The error text is written as the response body.
type handlerFunc func(w http.ResponseWriter, r *http.Request) error

func (f handlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) {
err := f(w, r)
if err == nil {
return
}

code := http.StatusInternalServerError
if se, ok := errors.AsType[statusError](err); ok {
code = se.code
}
http.Error(w, err.Error(), code)
}

// writeJSON writes obj as the JSON response body. Marshal failures are
// returned before anything is written so the caller can report them.
func writeJSON(w http.ResponseWriter, obj any) error {
out, err := json.Marshal(obj)
if err != nil {
return fmt.Errorf("marshal: %w", err)
}

w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(out)
return nil
}
108 changes: 108 additions & 0 deletions pkg/daemon/platform/handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package daemon

import (
"errors"
"fmt"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/require"
)

func TestHandlerFuncServeHTTP(t *testing.T) {
tests := []struct {
name string
handler handlerFunc
wantCode int
wantBody string
}{
{
name: "plain error is internal server error",
handler: func(http.ResponseWriter, *http.Request) error {
return errors.New("boom")
},
wantCode: http.StatusInternalServerError,
wantBody: "boom\n",
},
{
name: "bad request status error keeps its code",
handler: func(http.ResponseWriter, *http.Request) error {
return badRequest(errors.New("bad input"))
},
wantCode: http.StatusBadRequest,
wantBody: "bad input\n",
},
{
name: "forbidden status error keeps its code",
handler: func(http.ResponseWriter, *http.Request) error {
return forbidden(errors.New("no access"))
},
wantCode: http.StatusForbidden,
wantBody: "no access\n",
},
{
name: "wrapped status error keeps its code and message",
handler: func(http.ResponseWriter, *http.Request) error {
return fmt.Errorf("create workspace: %w", badRequest(errors.New("bad input")))
},
wantCode: http.StatusBadRequest,
wantBody: "create workspace: bad input\n",
},
{
name: "nil error leaves handler response untouched",
handler: func(w http.ResponseWriter, _ *http.Request) error {
w.WriteHeader(http.StatusCreated)
_, _ = w.Write([]byte("ok"))
return nil
},
wantCode: http.StatusCreated,
wantBody: "ok",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rec := httptest.NewRecorder()
tt.handler.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/", nil))

require.Equal(t, tt.wantCode, rec.Code)
require.Equal(t, tt.wantBody, rec.Body.String())
})
}
}

func TestLocalServerMissingQueryParamIsBadRequest(t *testing.T) {
l := &localServer{}
tests := []struct {
name string
handler handlerFunc
wantBody string
}{
{
name: "getWorkspace requires uid",
handler: l.getWorkspace,
wantBody: "missing required query parameter \"uid\"\n",
},
{
name: "getGitCredentials requires host",
handler: l.getGitCredentials,
wantBody: "missing required query parameter \"host\"\n",
},
{
name: "getDockerCredentials requires server",
handler: l.getDockerCredentials,
wantBody: "missing required query parameter \"server\"\n",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rec := httptest.NewRecorder()
tt.handler.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/", nil))

require.Equal(t, http.StatusBadRequest, rec.Code)
require.Equal(t, tt.wantBody, rec.Body.String())
})
}
}
Loading
Loading