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 id = pm.globals.get(\"checkin_id\");",
|
||||
" pm.expect(first.name).to.eql(\"Demo Checkin 1\");",
|
||||
" pm.expect(first.grace).to.eql(300);",
|
||||
" pm.expect(first.interval).to.eql(300);",
|
||||
" pm.expect(first.interval).to.eql(1);",
|
||||
"});"
|
||||
],
|
||||
"type": "text/javascript"
|
||||
|
@ -4990,8 +4989,7 @@
|
|||
" pm.expect(jsonData.status).to.eql(\"success\");",
|
||||
" pm.expect(jsonData.type).to.eql(\"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(900);",
|
||||
" pm.expect(jsonData.output.interval).to.eql(3);",
|
||||
" var id = jsonData.output.api_key;",
|
||||
" pm.globals.set(\"checkin_id\", id);",
|
||||
"});"
|
||||
|
@ -5022,7 +5020,7 @@
|
|||
],
|
||||
"body": {
|
||||
"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": {
|
||||
"raw": {}
|
||||
}
|
||||
|
@ -5182,8 +5180,7 @@
|
|||
" var id = pm.globals.get(\"checkin_id\");",
|
||||
" pm.expect(jsonData.name).to.eql(\"Server Checkin\");",
|
||||
" pm.expect(jsonData.api_key).to.eql(id);",
|
||||
" pm.expect(jsonData.grace).to.eql(60);",
|
||||
" pm.expect(jsonData.interval).to.eql(900);",
|
||||
" pm.expect(jsonData.interval).to.eql(3);",
|
||||
"});"
|
||||
],
|
||||
"type": "text/javascript"
|
||||
|
|
|
@ -251,6 +251,12 @@ func TestMainApiRoutes(t *testing.T) {
|
|||
`go_threads`,
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "Index Page",
|
||||
URL: "/",
|
||||
Method: "GET",
|
||||
ExpectedStatus: 200,
|
||||
},
|
||||
}
|
||||
|
||||
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) {
|
||||
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)
|
||||
}
|
||||
|
||||
|
@ -54,8 +63,7 @@ func apiCreateIncidentUpdateHandler(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
update.IncidentId = incid.Id
|
||||
|
||||
err = update.Create()
|
||||
if err != nil {
|
||||
if err := update.Create(); err != nil {
|
||||
sendErrorJson(err, w, r)
|
||||
return
|
||||
}
|
||||
|
@ -74,8 +82,7 @@ func apiCreateIncidentHandler(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
incident.ServiceId = service.Id
|
||||
err = incident.Create()
|
||||
if err != nil {
|
||||
if err := incident.Create(); err != nil {
|
||||
sendErrorJson(err, w, r)
|
||||
return
|
||||
}
|
||||
|
@ -103,8 +110,7 @@ func apiDeleteIncidentHandler(w http.ResponseWriter, r *http.Request) {
|
|||
sendErrorJson(err, w, r)
|
||||
return
|
||||
}
|
||||
err = incident.Delete()
|
||||
if err != nil {
|
||||
if err := incident.Delete(); err != nil {
|
||||
sendErrorJson(err, w, r)
|
||||
return
|
||||
}
|
||||
|
@ -118,8 +124,7 @@ func apiDeleteIncidentUpdateHandler(w http.ResponseWriter, r *http.Request) {
|
|||
sendErrorJson(err, w, r)
|
||||
return
|
||||
}
|
||||
err = update.Delete()
|
||||
if err != nil {
|
||||
if err := update.Delete(); err != nil {
|
||||
sendErrorJson(err, w, r)
|
||||
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
|
||||
r.Handle("/metrics", readOnly(promhttp.Handler(), false))
|
||||
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)
|
||||
return r
|
||||
}
|
||||
|
|
|
@ -79,16 +79,13 @@ func All() []*Incident {
|
|||
}
|
||||
|
||||
func (i *Incident) Create() error {
|
||||
q := db.Create(i)
|
||||
return q.Error()
|
||||
return db.Create(i).Error()
|
||||
}
|
||||
|
||||
func (i *Incident) Update() error {
|
||||
q := db.Update(i)
|
||||
return q.Error()
|
||||
return db.Update(i).Error()
|
||||
}
|
||||
|
||||
func (i *Incident) Delete() error {
|
||||
q := db.Delete(i)
|
||||
return q.Error()
|
||||
return db.Delete(i).Error()
|
||||
}
|
||||
|
|
|
@ -32,8 +32,10 @@ func TestInit(t *testing.T) {
|
|||
db, err := database.OpenTester()
|
||||
require.Nil(t, err)
|
||||
db.AutoMigrate(&Incident{}, &IncidentUpdate{})
|
||||
db.Create(&example)
|
||||
SetDB(db)
|
||||
db.Create(&example)
|
||||
db.Create(&update1)
|
||||
db.Create(&update2)
|
||||
}
|
||||
|
||||
func TestFind(t *testing.T) {
|
||||
|
|
|
@ -10,39 +10,25 @@ import (
|
|||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/ararog/timeago"
|
||||
)
|
||||
|
||||
var (
|
||||
// Directory returns the current path or the STATPING_DIR environment variable
|
||||
Directory string
|
||||
disableLogs bool
|
||||
Directory string
|
||||
)
|
||||
|
||||
type env struct {
|
||||
data interface{}
|
||||
}
|
||||
|
||||
func NotNumber(val string) bool {
|
||||
_, err := strconv.ParseInt(val, 10, 64)
|
||||
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
|
||||
func ToInt(s interface{}) int64 {
|
||||
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
|
||||
// in, out, err := Command("sass assets/scss assets/css/base.css")
|
||||
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
|
||||
// // timeout - Specific duration to timeout on. time.Duration(30 * time.Seconds)
|
||||
// // 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 req *http.Request
|
||||
if method == "" {
|
||||
method = "GET"
|
||||
}
|
||||
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
|
||||
}
|
||||
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)
|
||||
},
|
||||
}
|
||||
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 {
|
||||
transport.TLSClientConfig.RootCAs = customTLS.RootCAs
|
||||
transport.TLSClientConfig.Certificates = customTLS.Certificates
|
||||
|
@ -269,8 +251,8 @@ func HttpRequest(url, method string, content interface{}, headers []string, body
|
|||
}
|
||||
|
||||
// record HTTP metrics
|
||||
metrics.Histo("bytes", float64(len(contents)), url, method)
|
||||
metrics.Histo("duration", Now().Sub(t1).Seconds(), url, method)
|
||||
metrics.Histo("bytes", float64(len(contents)), endpoint, method)
|
||||
metrics.Histo("duration", Now().Sub(t1).Seconds(), endpoint, method)
|
||||
|
||||
return contents, resp, err
|
||||
}
|
||||
|
|
|
@ -123,11 +123,6 @@ func ExampleStringInt() {
|
|||
// Output: 42
|
||||
}
|
||||
|
||||
func TestTimestamp_Ago(t *testing.T) {
|
||||
now := Timestamp(time.Now())
|
||||
assert.Equal(t, "Just now", now.Ago())
|
||||
}
|
||||
|
||||
func TestHashPassword(t *testing.T) {
|
||||
pass := HashPassword("password123")
|
||||
assert.Equal(t, 60, len(pass))
|
||||
|
@ -135,10 +130,23 @@ func TestHashPassword(t *testing.T) {
|
|||
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) {
|
||||
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) {
|
||||
hash := NewSHA256Hash()
|
||||
assert.NotEmpty(t, hash)
|
||||
|
|
Loading…
Reference in New Issue