mirror of https://github.com/statping/statping
tests, http proxy in transport
parent
949747be70
commit
2ffbfca653
|
@ -4911,8 +4911,7 @@
|
||||||
" var first = jsonData[0];",
|
" var first = jsonData[0];",
|
||||||
" var id = pm.globals.get(\"checkin_id\");",
|
" var id = pm.globals.get(\"checkin_id\");",
|
||||||
" pm.expect(first.name).to.eql(\"Demo Checkin 1\");",
|
" pm.expect(first.name).to.eql(\"Demo Checkin 1\");",
|
||||||
" pm.expect(first.grace).to.eql(300);",
|
" pm.expect(first.interval).to.eql(1);",
|
||||||
" pm.expect(first.interval).to.eql(300);",
|
|
||||||
"});"
|
"});"
|
||||||
],
|
],
|
||||||
"type": "text/javascript"
|
"type": "text/javascript"
|
||||||
|
@ -4990,8 +4989,7 @@
|
||||||
" pm.expect(jsonData.status).to.eql(\"success\");",
|
" pm.expect(jsonData.status).to.eql(\"success\");",
|
||||||
" pm.expect(jsonData.type).to.eql(\"checkin\");",
|
" pm.expect(jsonData.type).to.eql(\"checkin\");",
|
||||||
" pm.expect(jsonData.output.name).to.eql(\"Server Checkin\");",
|
" pm.expect(jsonData.output.name).to.eql(\"Server Checkin\");",
|
||||||
" pm.expect(jsonData.output.grace).to.eql(60);",
|
" pm.expect(jsonData.output.interval).to.eql(3);",
|
||||||
" pm.expect(jsonData.output.interval).to.eql(900);",
|
|
||||||
" var id = jsonData.output.api_key;",
|
" var id = jsonData.output.api_key;",
|
||||||
" pm.globals.set(\"checkin_id\", id);",
|
" pm.globals.set(\"checkin_id\", id);",
|
||||||
"});"
|
"});"
|
||||||
|
@ -5022,7 +5020,7 @@
|
||||||
],
|
],
|
||||||
"body": {
|
"body": {
|
||||||
"mode": "raw",
|
"mode": "raw",
|
||||||
"raw": "{\n \"service_id\": 2,\n \"name\": \"Server Checkin\",\n \"interval\": 900,\n \"grace\": 60\n}",
|
"raw": "{\n \"service_id\": 2,\n \"name\": \"Server Checkin\",\n \"interval\": 3\n}",
|
||||||
"options": {
|
"options": {
|
||||||
"raw": {}
|
"raw": {}
|
||||||
}
|
}
|
||||||
|
@ -5182,8 +5180,7 @@
|
||||||
" var id = pm.globals.get(\"checkin_id\");",
|
" var id = pm.globals.get(\"checkin_id\");",
|
||||||
" pm.expect(jsonData.name).to.eql(\"Server Checkin\");",
|
" pm.expect(jsonData.name).to.eql(\"Server Checkin\");",
|
||||||
" pm.expect(jsonData.api_key).to.eql(id);",
|
" pm.expect(jsonData.api_key).to.eql(id);",
|
||||||
" pm.expect(jsonData.grace).to.eql(60);",
|
" pm.expect(jsonData.interval).to.eql(3);",
|
||||||
" pm.expect(jsonData.interval).to.eql(900);",
|
|
||||||
"});"
|
"});"
|
||||||
],
|
],
|
||||||
"type": "text/javascript"
|
"type": "text/javascript"
|
||||||
|
|
|
@ -251,6 +251,12 @@ func TestMainApiRoutes(t *testing.T) {
|
||||||
`go_threads`,
|
`go_threads`,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Name: "Index Page",
|
||||||
|
URL: "/",
|
||||||
|
Method: "GET",
|
||||||
|
ExpectedStatus: 200,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, v := range tests {
|
for _, v := range tests {
|
||||||
|
|
|
@ -26,7 +26,16 @@ func findIncident(r *http.Request) (*incidents.Incident, int64, error) {
|
||||||
|
|
||||||
func apiServiceIncidentsHandler(w http.ResponseWriter, r *http.Request) {
|
func apiServiceIncidentsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
incids := incidents.FindByService(utils.ToInt(vars["id"]))
|
id := vars["id"]
|
||||||
|
if utils.NotNumber(id) {
|
||||||
|
sendErrorJson(errors.NotNumber, w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
incids := incidents.FindByService(utils.ToInt(id))
|
||||||
|
if incids == nil {
|
||||||
|
sendErrorJson(errors.Missing(&incidents.Incident{}, id), w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
returnJson(incids, w, r)
|
returnJson(incids, w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,8 +63,7 @@ func apiCreateIncidentUpdateHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
update.IncidentId = incid.Id
|
update.IncidentId = incid.Id
|
||||||
|
|
||||||
err = update.Create()
|
if err := update.Create(); err != nil {
|
||||||
if err != nil {
|
|
||||||
sendErrorJson(err, w, r)
|
sendErrorJson(err, w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -74,8 +82,7 @@ func apiCreateIncidentHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
incident.ServiceId = service.Id
|
incident.ServiceId = service.Id
|
||||||
err = incident.Create()
|
if err := incident.Create(); err != nil {
|
||||||
if err != nil {
|
|
||||||
sendErrorJson(err, w, r)
|
sendErrorJson(err, w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -103,8 +110,7 @@ func apiDeleteIncidentHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
sendErrorJson(err, w, r)
|
sendErrorJson(err, w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = incident.Delete()
|
if err := incident.Delete(); err != nil {
|
||||||
if err != nil {
|
|
||||||
sendErrorJson(err, w, r)
|
sendErrorJson(err, w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -118,8 +124,7 @@ func apiDeleteIncidentUpdateHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
sendErrorJson(err, w, r)
|
sendErrorJson(err, w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = update.Delete()
|
if err := update.Delete(); err != nil {
|
||||||
if err != nil {
|
|
||||||
sendErrorJson(err, w, r)
|
sendErrorJson(err, w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,45 @@
|
||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestOAuthRoutes(t *testing.T) {
|
||||||
|
tests := []HTTPTest{
|
||||||
|
{
|
||||||
|
Name: "OAuth Save",
|
||||||
|
URL: "/api/oauth",
|
||||||
|
Body: `{
|
||||||
|
"gh_client_id": "githubid",
|
||||||
|
"gh_client_secret": "githubsecret",
|
||||||
|
"google_client_id": "googleid",
|
||||||
|
"google_client_secret": "googlesecret",
|
||||||
|
"oauth_domains": "gmail.com,yahoo.com,socialeck.com",
|
||||||
|
"oauth_providers": "local,slack,google,github",
|
||||||
|
"slack_client_id": "example.iddd",
|
||||||
|
"slack_client_secret": "exampleeesecret",
|
||||||
|
"slack_team": "dev"
|
||||||
|
}`,
|
||||||
|
Method: "POST",
|
||||||
|
ExpectedStatus: 200,
|
||||||
|
BeforeTest: SetTestENV,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "OAuth Values",
|
||||||
|
URL: "/api/oauth",
|
||||||
|
Method: "GET",
|
||||||
|
ExpectedStatus: 200,
|
||||||
|
ExpectedContains: []string{`"slack_client_id":"example.iddd"`},
|
||||||
|
AfterTest: UnsetTestENV,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, v := range tests {
|
||||||
|
t.Run(v.Name, func(t *testing.T) {
|
||||||
|
res, t, err := RunHTTPTest(v, t)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
t.Log(res)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
|
@ -180,7 +180,6 @@ func Router() *mux.Router {
|
||||||
// API Generic Routes
|
// API Generic Routes
|
||||||
r.Handle("/metrics", readOnly(promhttp.Handler(), false))
|
r.Handle("/metrics", readOnly(promhttp.Handler(), false))
|
||||||
r.Handle("/health", http.HandlerFunc(healthCheckHandler))
|
r.Handle("/health", http.HandlerFunc(healthCheckHandler))
|
||||||
r.Handle("/.well-known/", http.StripPrefix("/.well-known/", http.FileServer(http.Dir(dir+"/.well-known"))))
|
|
||||||
r.NotFoundHandler = http.HandlerFunc(error404Handler)
|
r.NotFoundHandler = http.HandlerFunc(error404Handler)
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
|
@ -79,16 +79,13 @@ func All() []*Incident {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Incident) Create() error {
|
func (i *Incident) Create() error {
|
||||||
q := db.Create(i)
|
return db.Create(i).Error()
|
||||||
return q.Error()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Incident) Update() error {
|
func (i *Incident) Update() error {
|
||||||
q := db.Update(i)
|
return db.Update(i).Error()
|
||||||
return q.Error()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Incident) Delete() error {
|
func (i *Incident) Delete() error {
|
||||||
q := db.Delete(i)
|
return db.Delete(i).Error()
|
||||||
return q.Error()
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,8 +32,10 @@ func TestInit(t *testing.T) {
|
||||||
db, err := database.OpenTester()
|
db, err := database.OpenTester()
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
db.AutoMigrate(&Incident{}, &IncidentUpdate{})
|
db.AutoMigrate(&Incident{}, &IncidentUpdate{})
|
||||||
db.Create(&example)
|
|
||||||
SetDB(db)
|
SetDB(db)
|
||||||
|
db.Create(&example)
|
||||||
|
db.Create(&update1)
|
||||||
|
db.Create(&update2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFind(t *testing.T) {
|
func TestFind(t *testing.T) {
|
||||||
|
|
|
@ -10,39 +10,25 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/ararog/timeago"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// Directory returns the current path or the STATPING_DIR environment variable
|
// Directory returns the current path or the STATPING_DIR environment variable
|
||||||
Directory string
|
Directory string
|
||||||
disableLogs bool
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type env struct {
|
|
||||||
data interface{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func NotNumber(val string) bool {
|
func NotNumber(val string) bool {
|
||||||
_, err := strconv.ParseInt(val, 10, 64)
|
_, err := strconv.ParseInt(val, 10, 64)
|
||||||
return err != nil
|
return err != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *env) Duration() time.Duration {
|
|
||||||
t, err := time.ParseDuration(e.data.(string))
|
|
||||||
if err != nil {
|
|
||||||
Log.Errorln(err)
|
|
||||||
}
|
|
||||||
return t
|
|
||||||
}
|
|
||||||
|
|
||||||
// ToInt converts a int to a string
|
// ToInt converts a int to a string
|
||||||
func ToInt(s interface{}) int64 {
|
func ToInt(s interface{}) int64 {
|
||||||
switch v := s.(type) {
|
switch v := s.(type) {
|
||||||
|
@ -91,17 +77,6 @@ func ToString(s interface{}) string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type Timestamp time.Time
|
|
||||||
type Timestamper interface {
|
|
||||||
Ago() string
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ago returns a human readable timestamp based on the Timestamp (time.Time) interface
|
|
||||||
func (t Timestamp) Ago() string {
|
|
||||||
got, _ := timeago.TimeAgoWithTime(time.Now(), time.Time(t))
|
|
||||||
return got
|
|
||||||
}
|
|
||||||
|
|
||||||
// Command will run a terminal command with 'sh -c COMMAND' and return stdout and errOut as strings
|
// Command will run a terminal command with 'sh -c COMMAND' and return stdout and errOut as strings
|
||||||
// in, out, err := Command("sass assets/scss assets/css/base.css")
|
// in, out, err := Command("sass assets/scss assets/css/base.css")
|
||||||
func Command(name string, args ...string) (string, string, error) {
|
func Command(name string, args ...string) (string, string, error) {
|
||||||
|
@ -187,14 +162,14 @@ func DurationReadable(d time.Duration) string {
|
||||||
// // body - The body or form data to send with HTTP request
|
// // body - The body or form data to send with HTTP request
|
||||||
// // timeout - Specific duration to timeout on. time.Duration(30 * time.Seconds)
|
// // timeout - Specific duration to timeout on. time.Duration(30 * time.Seconds)
|
||||||
// // You can use a HTTP Proxy if you HTTP_PROXY environment variable
|
// // You can use a HTTP Proxy if you HTTP_PROXY environment variable
|
||||||
func HttpRequest(url, method string, content interface{}, headers []string, body io.Reader, timeout time.Duration, verifySSL bool, customTLS *tls.Config) ([]byte, *http.Response, error) {
|
func HttpRequest(endpoint, method string, content interface{}, headers []string, body io.Reader, timeout time.Duration, verifySSL bool, customTLS *tls.Config) ([]byte, *http.Response, error) {
|
||||||
var err error
|
var err error
|
||||||
var req *http.Request
|
var req *http.Request
|
||||||
if method == "" {
|
if method == "" {
|
||||||
method = "GET"
|
method = "GET"
|
||||||
}
|
}
|
||||||
t1 := Now()
|
t1 := Now()
|
||||||
if req, err = http.NewRequest(method, url, body); err != nil {
|
if req, err = http.NewRequest(method, endpoint, body); err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
req.Header.Set("User-Agent", "Statping")
|
req.Header.Set("User-Agent", "Statping")
|
||||||
|
@ -243,6 +218,13 @@ func HttpRequest(url, method string, content interface{}, headers []string, body
|
||||||
return dialer.DialContext(ctx, network, addr)
|
return dialer.DialContext(ctx, network, addr)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
if Params.IsSet("HTTP_PROXY") {
|
||||||
|
proxyUrl, err := url.Parse(Params.GetString("HTTP_PROXY"))
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
transport.Proxy = http.ProxyURL(proxyUrl)
|
||||||
|
}
|
||||||
if customTLS != nil {
|
if customTLS != nil {
|
||||||
transport.TLSClientConfig.RootCAs = customTLS.RootCAs
|
transport.TLSClientConfig.RootCAs = customTLS.RootCAs
|
||||||
transport.TLSClientConfig.Certificates = customTLS.Certificates
|
transport.TLSClientConfig.Certificates = customTLS.Certificates
|
||||||
|
@ -269,8 +251,8 @@ func HttpRequest(url, method string, content interface{}, headers []string, body
|
||||||
}
|
}
|
||||||
|
|
||||||
// record HTTP metrics
|
// record HTTP metrics
|
||||||
metrics.Histo("bytes", float64(len(contents)), url, method)
|
metrics.Histo("bytes", float64(len(contents)), endpoint, method)
|
||||||
metrics.Histo("duration", Now().Sub(t1).Seconds(), url, method)
|
metrics.Histo("duration", Now().Sub(t1).Seconds(), endpoint, method)
|
||||||
|
|
||||||
return contents, resp, err
|
return contents, resp, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -123,11 +123,6 @@ func ExampleStringInt() {
|
||||||
// Output: 42
|
// Output: 42
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTimestamp_Ago(t *testing.T) {
|
|
||||||
now := Timestamp(time.Now())
|
|
||||||
assert.Equal(t, "Just now", now.Ago())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestHashPassword(t *testing.T) {
|
func TestHashPassword(t *testing.T) {
|
||||||
pass := HashPassword("password123")
|
pass := HashPassword("password123")
|
||||||
assert.Equal(t, 60, len(pass))
|
assert.Equal(t, 60, len(pass))
|
||||||
|
@ -135,10 +130,23 @@ func TestHashPassword(t *testing.T) {
|
||||||
assert.False(t, CheckHash("wrongpasswd", pass))
|
assert.False(t, CheckHash("wrongpasswd", pass))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHuman(t *testing.T) {
|
||||||
|
assert.Equal(t, "10 seconds", Duration{10 * time.Second}.Human())
|
||||||
|
assert.Equal(t, "1 day 12 hours", Duration{36 * time.Hour}.Human())
|
||||||
|
assert.Equal(t, "45 minutes", Duration{45 * time.Minute}.Human())
|
||||||
|
}
|
||||||
|
|
||||||
func TestSha256Hash(t *testing.T) {
|
func TestSha256Hash(t *testing.T) {
|
||||||
assert.Equal(t, "ef92b778bafe771e89245b89ecbc08a44a4e166c06659911881f383d4473e94f", Sha256Hash("password123"))
|
assert.Equal(t, "ef92b778bafe771e89245b89ecbc08a44a4e166c06659911881f383d4473e94f", Sha256Hash("password123"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNotNumbber(t *testing.T) {
|
||||||
|
assert.True(t, NotNumber("notint"))
|
||||||
|
assert.True(t, NotNumber("1293notanint922"))
|
||||||
|
assert.False(t, NotNumber("0"))
|
||||||
|
assert.False(t, NotNumber("5"))
|
||||||
|
}
|
||||||
|
|
||||||
func TestNewSHA1Hash(t *testing.T) {
|
func TestNewSHA1Hash(t *testing.T) {
|
||||||
hash := NewSHA256Hash()
|
hash := NewSHA256Hash()
|
||||||
assert.NotEmpty(t, hash)
|
assert.NotEmpty(t, hash)
|
||||||
|
|
Loading…
Reference in New Issue