From 87ebc96f80e4ddf364a5e89dd7ee2d11c6dff7f3 Mon Sep 17 00:00:00 2001 From: Geno Date: Wed, 23 Jun 2021 15:00:03 +0200 Subject: [PATCH] test(web): improve --- web/main_test.go | 30 ++++++++++++++++++++++++++++++ web/request_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 web/request_test.go diff --git a/web/main_test.go b/web/main_test.go index efb3895..e1871c0 100644 --- a/web/main_test.go +++ b/web/main_test.go @@ -1 +1,31 @@ package web + +import ( + "testing" + + "github.com/gin-gonic/gin" + "github.com/stretchr/testify/assert" +) + +func TestRun(t *testing.T) { + assert := assert.New(t) + + ModuleRegister(func(_ *gin.Engine, _ *Service) { + }) + + s := &Service{AccessLog: true, Listen: ":80"} + // HTTP - failed + err := s.Run() + assert.Error(err) + + s.ACME.Enable = true + // acme with listen port - panic + assert.Panics(func() { + s.Run() + }) + + s.Listen = "" + // httpS - failed + err = s.Run() + assert.Error(err) +} diff --git a/web/request_test.go b/web/request_test.go new file mode 100644 index 0000000..733f00b --- /dev/null +++ b/web/request_test.go @@ -0,0 +1,26 @@ +package web + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestJSONRequest(t *testing.T) { + assert := assert.New(t) + + data := struct { + IP string `json:"ip"` + }{} + err := JSONRequest("http://ip.jsontest.com/", &data) + assert.NoError(err) + assert.NotEqual("", data.IP) + + wrongData := "" + err = JSONRequest("http://ip.jsontest.com/", &wrongData) + assert.Error(err) + + wrongData = "" + err = JSONRequest("http://no.example.org/", &wrongData) + assert.Error(err) +}