Initial cloud-services repo - gateway service + pkg modules
This commit is contained in:
76
pkg/utils/json_resp_test.go
Normal file
76
pkg/utils/json_resp_test.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package utils_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
m "fiskerinc.com/modules/common"
|
||||
th "fiskerinc.com/modules/testhelper"
|
||||
u "fiskerinc.com/modules/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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user