You've already forked Go-Template
123 lines
3.0 KiB
Go
123 lines
3.0 KiB
Go
package index
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"gitea.siteworxpro.com/Siteworxpro/Go-Template/logger"
|
|
"github.com/labstack/echo/v4"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
type mockLogger struct {
|
|
logger.Logger
|
|
infoCalled bool
|
|
infoMsg string
|
|
}
|
|
|
|
func (m *mockLogger) Info(format string, args ...interface{}) {
|
|
m.infoCalled = true
|
|
m.infoMsg = format
|
|
}
|
|
|
|
func setupContext(method, path string, l *mockLogger) echo.Context {
|
|
e := echo.New()
|
|
req := httptest.NewRequest(method, path, nil)
|
|
rec := httptest.NewRecorder()
|
|
c := e.NewContext(req, rec)
|
|
c.Set("logger", l)
|
|
return c
|
|
}
|
|
|
|
func TestIndexGet(t *testing.T) {
|
|
l := &mockLogger{}
|
|
c := setupContext(http.MethodGet, "/", l)
|
|
idx := &index{}
|
|
|
|
err := idx.Get(c)
|
|
assert.NoError(t, err)
|
|
assert.True(t, l.infoCalled)
|
|
assert.Equal(t, http.StatusOK, c.Response().Status)
|
|
|
|
var resp map[string]string
|
|
e := json.Unmarshal(c.Response().Writer.(*httptest.ResponseRecorder).Body.Bytes(), &resp)
|
|
assert.NoError(t, e)
|
|
assert.Equal(t, "Hello, World!", resp["message"])
|
|
}
|
|
|
|
func TestIndexPost(t *testing.T) {
|
|
l := &mockLogger{}
|
|
c := setupContext(http.MethodPost, "/", l)
|
|
idx := &index{}
|
|
|
|
err := idx.Post(c)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, http.StatusBadRequest, c.Response().Status)
|
|
|
|
var resp map[string]interface{}
|
|
e := json.Unmarshal(c.Response().Writer.(*httptest.ResponseRecorder).Body.Bytes(), &resp)
|
|
assert.NoError(t, e)
|
|
assert.Equal(t, "Bad Request", resp["error"])
|
|
fields := resp["fields"].(map[string]interface{})
|
|
assert.Equal(t, "Test is a required field", fields["test"])
|
|
}
|
|
|
|
func TestIndexPut(t *testing.T) {
|
|
l := &mockLogger{}
|
|
c := setupContext(http.MethodPut, "/", l)
|
|
idx := &index{}
|
|
|
|
err := idx.Put(c)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, http.StatusOK, c.Response().Status)
|
|
|
|
var resp map[string]string
|
|
e := json.Unmarshal(c.Response().Writer.(*httptest.ResponseRecorder).Body.Bytes(), &resp)
|
|
assert.NoError(t, e)
|
|
assert.Equal(t, "Hello, World!", resp["message"])
|
|
}
|
|
|
|
func TestIndexDelete(t *testing.T) {
|
|
l := &mockLogger{}
|
|
c := setupContext(http.MethodDelete, "/", l)
|
|
idx := &index{}
|
|
|
|
err := idx.Delete(c)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, http.StatusOK, c.Response().Status)
|
|
|
|
var resp map[string]string
|
|
e := json.Unmarshal(c.Response().Writer.(*httptest.ResponseRecorder).Body.Bytes(), &resp)
|
|
assert.NoError(t, e)
|
|
assert.Equal(t, "Hello, World!", resp["message"])
|
|
}
|
|
|
|
func TestIndexPatch(t *testing.T) {
|
|
l := &mockLogger{}
|
|
c := setupContext(http.MethodPatch, "/", l)
|
|
idx := &index{}
|
|
|
|
err := idx.Patch(c)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, http.StatusNotFound, c.Response().Status)
|
|
|
|
var resp map[string]string
|
|
e := json.Unmarshal(c.Response().Writer.(*httptest.ResponseRecorder).Body.Bytes(), &resp)
|
|
assert.NoError(t, e)
|
|
assert.Equal(t, "Not Found", resp["error"])
|
|
}
|
|
|
|
func TestRegister(t *testing.T) {
|
|
l := &mockLogger{}
|
|
e := echo.New()
|
|
g := e.Group("/")
|
|
Register(g)
|
|
c := setupContext(http.MethodGet, "/", l)
|
|
idx := &index{}
|
|
err := idx.Get(c)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, http.StatusOK, c.Response().Status)
|
|
}
|