drone/internal/api/render/render_test.go
Johannes Batzill b7b9f53b0d Improve error handling to match go standards - don't wrap and rethrow, but log and return. Also adds some more validations for path creation and resource moving. Add accesslogging for git and api router (#14)
This commit contains the following:
- Improve and simplify error handling (remove unnecessary wrappers, make it feel like go)
- Add extra validation for path creation and resource moving (path has to be within same top space, no top space alias allowed)
- Add access logging for rest api and git api
2022-09-09 22:08:46 -07:00

139 lines
3.4 KiB
Go

// Copyright 2021 Harness Inc. All rights reserved.
// Use of this source code is governed by the Polyform Free Trial License
// that can be found in the LICENSE.md file for this repository.
package render
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
func TestWriteErrorf(t *testing.T) {
w := httptest.NewRecorder()
e := New("abc")
ErrorObject(w, 500, e)
if got, want := w.Code, 500; want != got {
t.Errorf("Want response code %d, got %d", want, got)
}
errjson := &Error{}
json.NewDecoder(w.Body).Decode(errjson)
if got, want := errjson.Message, e.Message; got != want {
t.Errorf("Want error message %s, got %s", want, got)
}
}
func TestWriteErrorCode(t *testing.T) {
w := httptest.NewRecorder()
ErrorMessagef(w, 418, "pc load letter %d", 1)
if got, want := w.Code, 418; want != got {
t.Errorf("Want response code %d, got %d", want, got)
}
errjson := &Error{}
json.NewDecoder(w.Body).Decode(errjson)
if got, want := errjson.Message, "pc load letter 1"; got != want {
t.Errorf("Want error message %s, got %s", want, got)
}
}
func TestWriteNotFound(t *testing.T) {
w := httptest.NewRecorder()
NotFound(w)
if got, want := w.Code, 404; want != got {
t.Errorf("Want response code %d, got %d", want, got)
}
errjson := &Error{}
json.NewDecoder(w.Body).Decode(errjson)
if got, want := errjson.Message, ErrNotFound.Message; got != want {
t.Errorf("Want error message %s, got %s", want, got)
}
}
func TestWriteUnauthorized(t *testing.T) {
w := httptest.NewRecorder()
Unauthorized(w)
if got, want := w.Code, 401; want != got {
t.Errorf("Want response code %d, got %d", want, got)
}
errjson := &Error{}
json.NewDecoder(w.Body).Decode(errjson)
if got, want := errjson.Message, ErrUnauthorized.Message; got != want {
t.Errorf("Want error message %s, got %s", want, got)
}
}
func TestWriteForbidden(t *testing.T) {
w := httptest.NewRecorder()
Forbidden(w)
if got, want := w.Code, 403; want != got {
t.Errorf("Want response code %d, got %d", want, got)
}
errjson := &Error{}
json.NewDecoder(w.Body).Decode(errjson)
if got, want := errjson.Message, ErrForbidden.Message; got != want {
t.Errorf("Want error message %s, got %s", want, got)
}
}
func TestWriteBadRequest(t *testing.T) {
w := httptest.NewRecorder()
BadRequest(w)
if got, want := w.Code, 400; want != got {
t.Errorf("Want response code %d, got %d", want, got)
}
errjson := &Error{}
json.NewDecoder(w.Body).Decode(errjson)
if got, want := errjson.Message, ErrBadRequest.Message; got != want {
t.Errorf("Want error message %s, got %s", want, got)
}
}
func TestWriteJSON(t *testing.T) {
// without indent
{
w := httptest.NewRecorder()
JSON(w, http.StatusTeapot, map[string]string{"hello": "world"})
if got, want := w.Body.String(), "{\"hello\":\"world\"}\n"; got != want {
t.Errorf("Want JSON body %q, got %q", want, got)
}
if got, want := w.HeaderMap.Get("Content-Type"), "application/json; charset=utf-8"; got != want {
t.Errorf("Want Content-Type %q, got %q", want, got)
}
if got, want := w.Code, http.StatusTeapot; got != want {
t.Errorf("Want status code %d, got %d", want, got)
}
}
// with indent
{
indent = true
defer func() {
indent = false
}()
w := httptest.NewRecorder()
JSON(w, http.StatusTeapot, map[string]string{"hello": "world"})
if got, want := w.Body.String(), "{\n \"hello\": \"world\"\n}\n"; got != want {
t.Errorf("Want JSON body %q, got %q", want, got)
}
}
}