Files
cloud-services/pkg/utils/json_resp_test.go

77 lines
2.0 KiB
Go

package utils_test
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
m "github.com/fiskerinc/cloud-services/pkg/common"
th "github.com/fiskerinc/cloud-services/pkg/testhelper"
u "github.com/fiskerinc/cloud-services/pkg/utils"
)
func TestRespError(t *testing.T) {
errMessage := "TEST_ERROR"
response := httptest.NewRecorder()
result := m.JSONError{}
u.RespError(response, http.StatusBadRequest, errMessage)
err := json.Unmarshal(response.Body.Bytes(), &result)
if err != nil {
t.Error(err)
}
if response.Result().StatusCode != http.StatusBadRequest {
t.Errorf(th.TestErrorTemplate, "TestRespError", http.StatusBadRequest, response.Result().StatusCode)
}
if result.Error != http.StatusText(http.StatusBadRequest) {
t.Errorf(th.TestErrorTemplate, "TestRespError", http.StatusText(http.StatusBadRequest), result.Error)
}
if result.Message != errMessage {
t.Errorf(th.TestErrorTemplate, "TestRespError", errMessage, result.Message)
}
}
func TestRespLink(t *testing.T) {
linkResult := "TEST_LINK"
response := httptest.NewRecorder()
result := m.JSONLink{}
timestamp := time.Now().UnixNano() / int64(time.Millisecond)
u.RespLink(response, linkResult, timestamp)
err := json.Unmarshal(response.Body.Bytes(), &result)
if err != nil {
t.Error(err)
}
if response.Result().StatusCode != http.StatusOK {
t.Errorf(th.TestErrorTemplate, "TestRespLink", http.StatusOK, response.Result().StatusCode)
}
if result.Link == "" {
t.Errorf(th.TestErrorTemplate, "TestRespLink", "not empty", result.Link)
}
if result.Link != linkResult {
t.Errorf(th.TestErrorTemplate, "TestRespLink", linkResult, result.Link)
}
}
func TestLinkJSON(t *testing.T) {
link := "TEST_LINK"
timestamp := time.Now().Unix()
result := u.LinkResult(link, timestamp)
if result.Link == "" {
t.Errorf(th.TestErrorTemplate, "TestRespLink", "not empty", result.Link)
}
if result.Link != link {
t.Errorf(th.TestErrorTemplate, "TestRespLink", link, result.Link)
}
}