mirror of
https://github.com/hay-kot/homebox.git
synced 2025-07-20 09:30:29 +00:00
Initial commit
This commit is contained in:
commit
29f583e936
135 changed files with 18463 additions and 0 deletions
78
backend/pkgs/server/response_test.go
Normal file
78
backend/pkgs/server/response_test.go
Normal file
|
@ -0,0 +1,78 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_Respond_NoContent(t *testing.T) {
|
||||
recorder := httptest.NewRecorder()
|
||||
dummystruct := struct {
|
||||
Name string
|
||||
}{
|
||||
Name: "dummy",
|
||||
}
|
||||
|
||||
Respond(recorder, http.StatusNoContent, dummystruct)
|
||||
|
||||
assert.Equal(t, http.StatusNoContent, recorder.Code)
|
||||
assert.Empty(t, recorder.Body.String())
|
||||
}
|
||||
|
||||
func Test_Respond_JSON(t *testing.T) {
|
||||
recorder := httptest.NewRecorder()
|
||||
dummystruct := struct {
|
||||
Name string `json:"name"`
|
||||
}{
|
||||
Name: "dummy",
|
||||
}
|
||||
|
||||
Respond(recorder, http.StatusCreated, dummystruct)
|
||||
|
||||
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()
|
||||
|
||||
RespondInternalServerError(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