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
28 changes: 28 additions & 0 deletions hcloud/exp/mockutil/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,31 @@ func validateJSONSyntax(raw string) error {

return fmt.Errorf("%w %s", err, b.String())
}

// NewErrorRequest returns a new generic error request for the given HTTP method and
// path and status.
func NewErrorRequest(method string, path string, status int, errorCode, errorMessage string) Request {
return Request{
Method: method, Path: path,
Status: status,
JSON: map[string]any{
"error": map[string]any{
"code": errorCode,
"message": errorMessage,
"details": nil,
},
},
}
}

// NotFoundRequest returns a generic `Not found` (404) request for the given HTTP method
// and path.
func NotFoundRequest(method string, path string) Request {
return NewErrorRequest(method, path, http.StatusNotFound, "not_found", "entity not found")
}

// ServerErrorRequest returns a generic `Internal Server Error` (500) request for the
// given HTTP method and path.
func ServerErrorRequest(method string, path string) Request {
return NewErrorRequest(method, path, http.StatusInternalServerError, "server_error", "server error")
}
18 changes: 18 additions & 0 deletions hcloud/exp/mockutil/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,21 @@ func TestValidateJSONSyntax(t *testing.T) {
5 | "meta": { "pagination": { "page": 1 }}`,
)
}

func TestNewErrorRequest(t *testing.T) {
t.Run("server error", func(t *testing.T) {
r := ServerErrorRequest(http.MethodPost, "/path")
assert.Equal(t, "POST", r.Method)
assert.Equal(t, "/path", r.Path)
assert.Equal(t, 500, r.Status)
assert.Equal(t, map[string]any{"error": map[string]any{"code": "server_error", "message": "server error", "details": nil}}, r.JSON)
})

t.Run("not found", func(t *testing.T) {
r := NotFoundRequest(http.MethodPost, "/path")
assert.Equal(t, "POST", r.Method)
assert.Equal(t, "/path", r.Path)
assert.Equal(t, 404, r.Status)
assert.Equal(t, map[string]any{"error": map[string]any{"code": "not_found", "message": "entity not found", "details": nil}}, r.JSON)
})
}
Loading