github.com/xmidt-org/webpa-common@v1.11.9/xhttp/gate/status_test.go (about) 1 package gate 2 3 import ( 4 "context" 5 "fmt" 6 "net/http" 7 "net/http/httptest" 8 "testing" 9 "time" 10 11 "github.com/stretchr/testify/assert" 12 "github.com/xmidt-org/webpa-common/logging" 13 ) 14 15 func testStatusServeHTTP(t *testing.T, state bool) { 16 var ( 17 assert = assert.New(t) 18 logger = logging.NewTestLogger(nil, t) 19 ctx = logging.WithLogger(context.Background(), logger) 20 expectedTimestamp = time.Now() 21 expectedStatus = fmt.Sprintf(`{"open": %t, "timestamp": "%s"}`, state, expectedTimestamp.UTC().Format(time.RFC3339)) 22 23 response = httptest.NewRecorder() 24 request = httptest.NewRequest("GET", "/", nil) 25 26 g = New(state) 27 status = Status{Gate: g} 28 ) 29 30 g.(*gate).now = func() time.Time { return expectedTimestamp } 31 32 status.ServeHTTP(response, request.WithContext(ctx)) 33 assert.Equal(http.StatusOK, response.Code) 34 assert.JSONEq( 35 expectedStatus, 36 response.Body.String(), 37 ) 38 } 39 40 func TestStatus(t *testing.T) { 41 t.Run("Open", func(t *testing.T) { 42 testStatusServeHTTP(t, true) 43 }) 44 45 t.Run("Closed", func(t *testing.T) { 46 testStatusServeHTTP(t, false) 47 }) 48 }