mirror of
https://github.com/hay-kot/homebox.git
synced 2025-07-20 09:30:29 +00:00
refactor: http interfaces (#114)
* implement custom http handler interface * implement trace_id * normalize http method spacing for consistent logs * fix failing test * fix linter errors * cleanup old dead code * more route cleanup * cleanup some inconsistent errors * update and generate code * make taskfile more consistent * update task calls * run tidy * drop `@` tag for version * use relative paths * tidy * fix auto-setting variables * update build paths * add contributing guide * tidy
This commit is contained in:
parent
e2d93f8523
commit
6529549289
40 changed files with 984 additions and 808 deletions
|
@ -1,7 +1,6 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
@ -17,7 +16,8 @@ func Test_Respond_NoContent(t *testing.T) {
|
|||
Name: "dummy",
|
||||
}
|
||||
|
||||
Respond(recorder, http.StatusNoContent, dummystruct)
|
||||
err := Respond(recorder, http.StatusNoContent, dummystruct)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, http.StatusNoContent, recorder.Code)
|
||||
assert.Empty(t, recorder.Body.String())
|
||||
|
@ -31,48 +31,11 @@ func Test_Respond_JSON(t *testing.T) {
|
|||
Name: "dummy",
|
||||
}
|
||||
|
||||
Respond(recorder, http.StatusCreated, dummystruct)
|
||||
err := Respond(recorder, http.StatusCreated, dummystruct)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, http.StatusCreated, recorder.Code)
|
||||
assert.JSONEq(t, recorder.Body.String(), `{"name":"dummy"}`)
|
||||
assert.Equal(t, "application/json", recorder.Header().Get("Content-Type"))
|
||||
|
||||
}
|
||||
|
||||
func Test_RespondError(t *testing.T) {
|
||||
recorder := httptest.NewRecorder()
|
||||
var customError = errors.New("custom error")
|
||||
|
||||
RespondError(recorder, http.StatusBadRequest, customError)
|
||||
|
||||
assert.Equal(t, http.StatusBadRequest, recorder.Code)
|
||||
assert.JSONEq(t, recorder.Body.String(), `{"details":["custom error"], "message":"Bad Request", "error":true}`)
|
||||
|
||||
}
|
||||
func Test_RespondInternalServerError(t *testing.T) {
|
||||
recorder := httptest.NewRecorder()
|
||||
|
||||
RespondServerError(recorder)
|
||||
|
||||
assert.Equal(t, http.StatusInternalServerError, recorder.Code)
|
||||
assert.JSONEq(t, recorder.Body.String(), `{"details":["internal server error"], "message":"Internal Server Error", "error":true}`)
|
||||
|
||||
}
|
||||
func Test_RespondUnauthorized(t *testing.T) {
|
||||
recorder := httptest.NewRecorder()
|
||||
|
||||
RespondUnauthorized(recorder)
|
||||
|
||||
assert.Equal(t, http.StatusUnauthorized, recorder.Code)
|
||||
assert.JSONEq(t, recorder.Body.String(), `{"details":["unauthorized"], "message":"Unauthorized", "error":true}`)
|
||||
|
||||
}
|
||||
func Test_RespondForbidden(t *testing.T) {
|
||||
recorder := httptest.NewRecorder()
|
||||
|
||||
RespondForbidden(recorder)
|
||||
|
||||
assert.Equal(t, http.StatusForbidden, recorder.Code)
|
||||
assert.JSONEq(t, recorder.Body.String(), `{"details":["forbidden"], "message":"Forbidden", "error":true}`)
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue