Added webserver tests
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
cfg "scm.yoorie.de/go-lib/micro/config"
|
||||
)
|
||||
|
||||
type MyConfig struct {
|
||||
Value string `default:"Bar" yaml:"value"`
|
||||
Value1 bool `default:"false" yaml:"value1"`
|
||||
Value2 int `default:"8080" yaml:"value2"`
|
||||
}
|
||||
|
||||
func (config *MyConfig) appName() string {
|
||||
return "myconfig"
|
||||
}
|
||||
|
||||
func TestIsStructPointerStruct(t *testing.T) {
|
||||
assert.False(t, cfg.IsStructPointer(MyConfig{}))
|
||||
}
|
||||
func TestIsStructPointerString(t *testing.T) {
|
||||
assert.False(t, cfg.IsStructPointer("Bar"))
|
||||
}
|
||||
|
||||
func TestIsStructPointerSimpleArray(t *testing.T) {
|
||||
assert.False(t, cfg.IsStructPointer([2]int{292, 2}))
|
||||
}
|
||||
|
||||
func TestIsStructPointerInt(t *testing.T) {
|
||||
assert.False(t, cfg.IsStructPointer(12))
|
||||
}
|
||||
|
||||
func TestIsStructPointerBool(t *testing.T) {
|
||||
assert.False(t, cfg.IsStructPointer(false))
|
||||
}
|
||||
|
||||
func TestIsStructPointerStructPointer(t *testing.T) {
|
||||
assert.True(t, cfg.IsStructPointer(&MyConfig{}))
|
||||
}
|
||||
|
||||
func TestIsStructPointerStructPointerArray(t *testing.T) {
|
||||
cfg1 := &MyConfig{
|
||||
Value: "Foo",
|
||||
}
|
||||
cfg2 := &MyConfig{
|
||||
Value: "Bar",
|
||||
}
|
||||
array := [2]*MyConfig{cfg1, cfg2}
|
||||
assert.False(t, cfg.IsStructPointer(array))
|
||||
}
|
||||
|
||||
func TestLoadConfigurationFromFileWithNoPointerInvalidType(t *testing.T) {
|
||||
myConfig := MyConfig{}
|
||||
err := cfg.LoadConfigurationFromFile(myConfig, "testdata/config.yml")
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
|
||||
func TestLoadConfigurationFromFile(t *testing.T) {
|
||||
myConfig := &MyConfig{}
|
||||
assert.Equal(t, "myconfig", myConfig.appName())
|
||||
cfg.LoadConfigurationFromFile(myConfig, "testdata/config.yml")
|
||||
assert.Equal(t, "Foo", myConfig.Value)
|
||||
assert.Equal(t, true, myConfig.Value1)
|
||||
assert.Equal(t, 8080, myConfig.Value2)
|
||||
}
|
||||
func TestLoadConfigurationFromFileNotExist(t *testing.T) {
|
||||
myConfig := &MyConfig{}
|
||||
assert.Equal(t, "myconfig", myConfig.appName())
|
||||
err := cfg.LoadConfigurationFromFile(myConfig, "testdata/config1.yml")
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
|
||||
func TestGetConfigurationFiles(t *testing.T) {
|
||||
fileNames := cfg.GetConfigurationFiles("myapp")
|
||||
assert.Equal(t, 4, len(fileNames))
|
||||
|
||||
for _, fileName := range fileNames {
|
||||
t.Log(fileName)
|
||||
}
|
||||
}
|
||||
Vendored
+2
@@ -0,0 +1,2 @@
|
||||
value : 'Foo'
|
||||
value1 : true
|
||||
@@ -0,0 +1,155 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/go-chi/render"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"scm.yoorie.de/go-lib/micro/web"
|
||||
"scm.yoorie.de/go-lib/util"
|
||||
)
|
||||
|
||||
var (
|
||||
internalCount int
|
||||
httpPort int
|
||||
sslPort int
|
||||
)
|
||||
|
||||
type MyData struct {
|
||||
Message string `json:"message"`
|
||||
Count int `json:"count"`
|
||||
}
|
||||
|
||||
func MyTestEndpoint(w http.ResponseWriter, r *http.Request) {
|
||||
internalCount++
|
||||
render.JSON(w, r, &MyData{
|
||||
Message: "A message",
|
||||
Count: internalCount,
|
||||
})
|
||||
}
|
||||
|
||||
func CreateTestRouter() *chi.Mux {
|
||||
router := chi.NewRouter()
|
||||
router.Get("/myendpoint", MyTestEndpoint)
|
||||
return router
|
||||
}
|
||||
|
||||
// GetFreePort asks the kernel for a free open port that is ready to use.
|
||||
func GetFreePort() (int, error) {
|
||||
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
l, err := net.ListenTCP("tcp", addr)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer l.Close()
|
||||
return l.Addr().(*net.TCPAddr).Port, nil
|
||||
}
|
||||
|
||||
func getServerURL(ssl bool, path string) string {
|
||||
var scheme string
|
||||
var port int
|
||||
if ssl {
|
||||
scheme = "https"
|
||||
port = sslPort
|
||||
} else {
|
||||
scheme = "http"
|
||||
port = httpPort
|
||||
}
|
||||
return util.JoiningSlash(fmt.Sprintf("%s://localhost:%d", scheme, port), path)
|
||||
}
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
internalCount = 0
|
||||
var err error
|
||||
httpPort, err = GetFreePort()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
sslPort, err = GetFreePort()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
config := &web.WebServerConfiguration{
|
||||
Port: httpPort,
|
||||
SslPort: sslPort,
|
||||
}
|
||||
server, err := web.NewWebServer(config)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
server.Mount("/api", CreateTestRouter())
|
||||
server.Start()
|
||||
|
||||
// Allow insecure calls to https
|
||||
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
||||
|
||||
exitVal := m.Run()
|
||||
server.Stop()
|
||||
os.Exit(exitVal)
|
||||
}
|
||||
|
||||
func TestReady(t *testing.T) {
|
||||
uri := getServerURL(false, "/readyz")
|
||||
resp, err := http.Get(uri)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
healthData := web.HealthData{}
|
||||
err = json.Unmarshal(body, &healthData)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "service is ready", healthData.Message)
|
||||
assert.NotEmpty(t, healthData.LastChecked)
|
||||
}
|
||||
|
||||
func TestReadySSL(t *testing.T) {
|
||||
uri := getServerURL(true, "/health/readyz")
|
||||
resp, err := http.Get(uri)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
healthData := web.HealthData{}
|
||||
err = json.Unmarshal(body, &healthData)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "service is ready", healthData.Message)
|
||||
assert.NotEmpty(t, healthData.LastChecked)
|
||||
}
|
||||
|
||||
func TestHealthy(t *testing.T) {
|
||||
resp, err := http.Get(getServerURL(false, "/healthz"))
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
healthData := web.HealthData{}
|
||||
err = json.Unmarshal(body, &healthData)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "service up and running", healthData.Message)
|
||||
assert.NotEmpty(t, healthData.LastChecked)
|
||||
}
|
||||
|
||||
func TestSslEndpoint(t *testing.T) {
|
||||
uri := getServerURL(true, "/api/myendpoint")
|
||||
resp, err := http.Get(uri)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
assert.Nil(t, err)
|
||||
t.Log(fmt.Sprintf("Body: %s", string(body)))
|
||||
myData := MyData{}
|
||||
err = json.Unmarshal(body, &myData)
|
||||
assert.Nil(t, err)
|
||||
assert.NotNil(t, myData)
|
||||
assert.Equal(t, "A message", myData.Message)
|
||||
assert.Greater(t, myData.Count, 0)
|
||||
}
|
||||
Reference in New Issue
Block a user