github.com/npaton/distribution@v2.3.1-rc.0+incompatible/health/api/api_test.go (about) 1 package api 2 3 import ( 4 "net/http" 5 "net/http/httptest" 6 "testing" 7 8 "github.com/docker/distribution/health" 9 ) 10 11 // TestGETDownHandlerDoesNotChangeStatus ensures that calling the endpoint 12 // /debug/health/down with METHOD GET returns a 404 13 func TestGETDownHandlerDoesNotChangeStatus(t *testing.T) { 14 recorder := httptest.NewRecorder() 15 16 req, err := http.NewRequest("GET", "https://fakeurl.com/debug/health/down", nil) 17 if err != nil { 18 t.Errorf("Failed to create request.") 19 } 20 21 DownHandler(recorder, req) 22 23 if recorder.Code != 404 { 24 t.Errorf("Did not get a 404.") 25 } 26 } 27 28 // TestGETUpHandlerDoesNotChangeStatus ensures that calling the endpoint 29 // /debug/health/down with METHOD GET returns a 404 30 func TestGETUpHandlerDoesNotChangeStatus(t *testing.T) { 31 recorder := httptest.NewRecorder() 32 33 req, err := http.NewRequest("GET", "https://fakeurl.com/debug/health/up", nil) 34 if err != nil { 35 t.Errorf("Failed to create request.") 36 } 37 38 DownHandler(recorder, req) 39 40 if recorder.Code != 404 { 41 t.Errorf("Did not get a 404.") 42 } 43 } 44 45 // TestPOSTDownHandlerChangeStatus ensures the endpoint /debug/health/down changes 46 // the status code of the response to 503 47 // This test is order dependent, and should come before TestPOSTUpHandlerChangeStatus 48 func TestPOSTDownHandlerChangeStatus(t *testing.T) { 49 recorder := httptest.NewRecorder() 50 51 req, err := http.NewRequest("POST", "https://fakeurl.com/debug/health/down", nil) 52 if err != nil { 53 t.Errorf("Failed to create request.") 54 } 55 56 DownHandler(recorder, req) 57 58 if recorder.Code != 200 { 59 t.Errorf("Did not get a 200.") 60 } 61 62 if len(health.CheckStatus()) != 1 { 63 t.Errorf("DownHandler didn't add an error check.") 64 } 65 } 66 67 // TestPOSTUpHandlerChangeStatus ensures the endpoint /debug/health/up changes 68 // the status code of the response to 200 69 func TestPOSTUpHandlerChangeStatus(t *testing.T) { 70 recorder := httptest.NewRecorder() 71 72 req, err := http.NewRequest("POST", "https://fakeurl.com/debug/health/up", nil) 73 if err != nil { 74 t.Errorf("Failed to create request.") 75 } 76 77 UpHandler(recorder, req) 78 79 if recorder.Code != 200 { 80 t.Errorf("Did not get a 200.") 81 } 82 83 if len(health.CheckStatus()) != 0 { 84 t.Errorf("UpHandler didn't remove the error check.") 85 } 86 }