Skip to content
Snippets Groups Projects
handlers_test.go 1.07 KiB
Newer Older
  • Learn to ignore specific revisions
  • Eric Chiang's avatar
    Eric Chiang committed
    package server
    
    	"net/http"
    	"net/http/httptest"
    	"testing"
    
    )
    
    func TestHandleHealth(t *testing.T) {
    
    	ctx, cancel := context.WithCancel(context.Background())
    	defer cancel()
    
    
    Eric Chiang's avatar
    Eric Chiang committed
    	httpServer, server := newTestServer(ctx, t, nil)
    
    	defer httpServer.Close()
    
    	rr := httptest.NewRecorder()
    
    	server.ServeHTTP(rr, httptest.NewRequest("GET", "/healthz", nil))
    
    	if rr.Code != http.StatusOK {
    		t.Errorf("expected 200 got %d", rr.Code)
    	}
    
    }
    
    
    type badStorage struct {
    	storage.Storage
    }
    
    func (b *badStorage) CreateAuthRequest(r storage.AuthRequest) error {
    	return errors.New("storage unavailable")
    }
    
    func TestHandleHealthFailure(t *testing.T) {
    	ctx, cancel := context.WithCancel(context.Background())
    	defer cancel()
    
    	httpServer, server := newTestServer(ctx, t, func(c *Config) {
    		c.Storage = &badStorage{c.Storage}
    	})
    	defer httpServer.Close()
    
    	rr := httptest.NewRecorder()
    	server.ServeHTTP(rr, httptest.NewRequest("GET", "/healthz", nil))
    	if rr.Code != http.StatusInternalServerError {
    		t.Errorf("expected 500 got %d", rr.Code)
    	}
    }