github.com/lusis/distribution@v2.0.1+incompatible/health/health_test.go (about)

     1  package health
     2  
     3  import (
     4  	"errors"
     5  	"net/http"
     6  	"net/http/httptest"
     7  	"testing"
     8  )
     9  
    10  // TestReturns200IfThereAreNoChecks ensures that the result code of the health
    11  // endpoint is 200 if there are not currently registered checks.
    12  func TestReturns200IfThereAreNoChecks(t *testing.T) {
    13  	recorder := httptest.NewRecorder()
    14  
    15  	req, err := http.NewRequest("GET", "https://fakeurl.com/debug/health", nil)
    16  	if err != nil {
    17  		t.Errorf("Failed to create request.")
    18  	}
    19  
    20  	StatusHandler(recorder, req)
    21  
    22  	if recorder.Code != 200 {
    23  		t.Errorf("Did not get a 200.")
    24  	}
    25  }
    26  
    27  // TestReturns500IfThereAreErrorChecks ensures that the result code of the
    28  // health endpoint is 500 if there are health checks with errors
    29  func TestReturns503IfThereAreErrorChecks(t *testing.T) {
    30  	recorder := httptest.NewRecorder()
    31  
    32  	req, err := http.NewRequest("GET", "https://fakeurl.com/debug/health", nil)
    33  	if err != nil {
    34  		t.Errorf("Failed to create request.")
    35  	}
    36  
    37  	// Create a manual error
    38  	Register("some_check", CheckFunc(func() error {
    39  		return errors.New("This Check did not succeed")
    40  	}))
    41  
    42  	StatusHandler(recorder, req)
    43  
    44  	if recorder.Code != 503 {
    45  		t.Errorf("Did not get a 503.")
    46  	}
    47  }