management_services_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package api
  2. import (
  3. "crypto/tls"
  4. "net/http"
  5. "net/http/httptest"
  6. "path/filepath"
  7. "testing"
  8. "debtik/internal/services"
  9. )
  10. func TestHTTPAndHTTPSServiceGatesAreIndependent(t *testing.T) {
  11. m := services.NewManager(filepath.Join(t.TempDir(), "services.json"))
  12. mux := http.NewServeMux()
  13. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(204) })
  14. s := &Server{servicesMgr: m, mux: mux}
  15. for _, tc := range []struct{ httpDisabled, httpsDisabled bool }{{true, false}, {false, true}, {false, false}} {
  16. if err := m.SetServiceState("www", tc.httpDisabled); err != nil {
  17. t.Fatal(err)
  18. }
  19. if err := m.SetServiceState("www-ssl", tc.httpsDisabled); err != nil {
  20. t.Fatal(err)
  21. }
  22. for _, secure := range []bool{false, true} {
  23. r := httptest.NewRequest("GET", "/", nil)
  24. if secure {
  25. r.TLS = &tls.ConnectionState{}
  26. }
  27. w := httptest.NewRecorder()
  28. s.ServeHTTP(w, r)
  29. want := 204
  30. if (!secure && tc.httpDisabled) || (secure && tc.httpsDisabled) {
  31. want = 503
  32. }
  33. if w.Code != want {
  34. t.Fatalf("tls=%v httpDisabled=%v httpsDisabled=%v: got %d want %d", secure, tc.httpDisabled, tc.httpsDisabled, w.Code, want)
  35. }
  36. }
  37. }
  38. }