| 1234567891011121314151617181920212223242526272829303132333435363738394041 |
- package api
- import (
- "crypto/tls"
- "net/http"
- "net/http/httptest"
- "path/filepath"
- "testing"
- "debtik/internal/services"
- )
- func TestHTTPAndHTTPSServiceGatesAreIndependent(t *testing.T) {
- m := services.NewManager(filepath.Join(t.TempDir(), "services.json"))
- mux := http.NewServeMux()
- mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(204) })
- s := &Server{servicesMgr: m, mux: mux}
- for _, tc := range []struct{ httpDisabled, httpsDisabled bool }{{true, false}, {false, true}, {false, false}} {
- if err := m.SetServiceState("www", tc.httpDisabled); err != nil {
- t.Fatal(err)
- }
- if err := m.SetServiceState("www-ssl", tc.httpsDisabled); err != nil {
- t.Fatal(err)
- }
- for _, secure := range []bool{false, true} {
- r := httptest.NewRequest("GET", "/", nil)
- if secure {
- r.TLS = &tls.ConnectionState{}
- }
- w := httptest.NewRecorder()
- s.ServeHTTP(w, r)
- want := 204
- if (!secure && tc.httpDisabled) || (secure && tc.httpsDisabled) {
- want = 503
- }
- if w.Code != want {
- t.Fatalf("tls=%v httpDisabled=%v httpsDisabled=%v: got %d want %d", secure, tc.httpDisabled, tc.httpsDisabled, w.Code, want)
- }
- }
- }
- }
|