Openstatus www.openstatus.dev
6
fork

Configure Feed

Select the types of activity you want to include in your feed.

at main 117 lines 3.1 kB view raw
1package handlers_test 2 3import ( 4 "encoding/json" 5 "fmt" 6 "io" 7 "net/http" 8 "net/http/httptest" 9 "strings" 10 "testing" 11 12 "github.com/openstatushq/openstatus/apps/checker/handlers" 13 "github.com/openstatushq/openstatus/apps/checker/pkg/tinybird" 14 "github.com/openstatushq/openstatus/apps/checker/request" 15 16 "github.com/gin-gonic/gin" 17 "github.com/stretchr/testify/assert" 18) 19 20type RoundTripFunc func(req *http.Request) *http.Response 21 22func (f RoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) { 23 return f(req), nil 24} 25 26func TestHandler_PingRegion(t *testing.T) { 27 28 hclient := &http.Client{Transport: RoundTripFunc(func(req *http.Request) *http.Response { 29 return &http.Response{ 30 StatusCode: http.StatusAccepted, 31 Body: io.NopCloser(strings.NewReader(`Status Accepted`)), 32 } 33 })} 34 client := tinybird.NewClient(hclient, "apiKey") 35 36 t.Run("it should return 401 if there's no auth", func(t *testing.T) { 37 38 region := "local" 39 h := handlers.Handler{ 40 TbClient: client, 41 Secret: "", 42 CloudProvider: "fly", 43 Region: region, 44 } 45 router := gin.New() 46 router.POST("/checker/:region", h.PingRegionHandler) 47 48 w := httptest.NewRecorder() 49 50 data := request.PingRequest{ 51 URL: "https://www.openstatus.dev", 52 } 53 dataJson, _ := json.Marshal(data) 54 req, _ := http.NewRequest(http.MethodPost, "/checker/"+region, strings.NewReader(string(dataJson))) 55 router.ServeHTTP(w, req) 56 57 assert.Equal(t, 401, w.Code) 58 }) 59 60 t.Run("it should return 400 if the payload is not ok", func(t *testing.T) { 61 region := "local" 62 63 h := handlers.Handler{ 64 TbClient: client, 65 Secret: "test", 66 CloudProvider: "fly", 67 Region: region, 68 } 69 router := gin.New() 70 router.POST("/checker/:region", h.PingRegionHandler) 71 72 w := httptest.NewRecorder() 73 74 data := request.HttpCheckerRequest{ 75 URL: "https://www.openstatus.dev", 76 } 77 dataJson, _ := json.Marshal(data) 78 req, _ := http.NewRequest(http.MethodPost, "/checker/"+region, strings.NewReader(string(dataJson))) 79 req.Header.Set("Authorization", "Basic test") 80 router.ServeHTTP(w, req) 81 82 assert.Equal(t, 400, w.Code) 83 assert.Contains(t, w.Body.String(), "{\"error\":\"invalid request\"}") 84 }) 85 86 t.Run("it should return 200 if the payload is ok", func(t *testing.T) { 87 region := "local" 88 89 httptest.NewRequest(http.MethodGet, "http://www.openstatus.dev", nil) 90 httptest.NewRecorder() 91 92 h := handlers.Handler{ 93 TbClient: client, 94 Secret: "test", 95 CloudProvider: "fly", 96 Region: region, 97 } 98 router := gin.New() 99 router.POST("/checker/:region", h.PingRegionHandler) 100 101 w := httptest.NewRecorder() 102 103 data := request.PingRequest{ 104 URL: "https://www.openstatus.dev", 105 Method: "GET", 106 Headers: map[string]string{}, 107 Body: "", 108 } 109 dataJson, _ := json.Marshal(data) 110 req, _ := http.NewRequest(http.MethodPost, "/checker/"+region, strings.NewReader(string(dataJson))) 111 req.Header.Set("Authorization", "Basic test") 112 router.ServeHTTP(w, req) 113 114 assert.Equal(t, 200, w.Code) 115 fmt.Println(w.Body.String()) 116 }) 117}