Skip to content

Commit 012c976

Browse files
committed
Fix test string constants
1 parent 4941bde commit 012c976

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

services/authz/server/server.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ const (
4242
defaultRetryMultiplier = 1.5
4343
defaultMaxInterval = 2 * time.Second
4444
emptyString = ""
45+
contentTypeHeader = "Content-Type"
46+
applicationJSON = "application/json"
4547
errDecodeRequest = "bad request"
4648
errMissingMFAParams = "missing MFA parameters"
4749
errMethodNotAllowed = "method not allowed"
@@ -155,7 +157,7 @@ func New(cfg Config) (*Server, error) {
155157
})
156158
})
157159

158-
useTLS := cfg.TLSCertPath != "" && cfg.TLSKeyPath != ""
160+
useTLS := cfg.TLSCertPath != emptyString && cfg.TLSKeyPath != emptyString
159161
if useTLS {
160162
cert, err := tls.LoadX509KeyPair(cfg.TLSCertPath, cfg.TLSKeyPath)
161163
if err != nil {
@@ -281,7 +283,7 @@ func (s *Server) healthHandler(w http.ResponseWriter, _ *http.Request) {
281283
"tailscale": s.getTailscaleInfo(),
282284
}
283285

284-
w.Header().Set("Content-Type", "application/json")
286+
w.Header().Set(contentTypeHeader, applicationJSON)
285287
w.WriteHeader(http.StatusOK)
286288
if err := json.NewEncoder(w).Encode(health); err != nil {
287289
log.Printf("failed to encode health response: %v", err)
@@ -394,7 +396,7 @@ func (s *Server) envoyAuthHandler(w http.ResponseWriter, r *http.Request) {
394396
}
395397
w.WriteHeader(http.StatusOK)
396398
case decisionStepUp:
397-
w.Header().Set("Content-Type", "application/json")
399+
w.Header().Set(contentTypeHeader, applicationJSON)
398400
w.WriteHeader(http.StatusForbidden)
399401
response := map[string]interface{}{
400402
"error": "mfa_required",
@@ -432,7 +434,7 @@ func (s *Server) evaluateOPA(ctx context.Context, claims map[string]any, deviceI
432434
if err != nil {
433435
return "", err
434436
}
435-
req.Header.Set("Content-Type", "application/json")
437+
req.Header.Set(contentTypeHeader, applicationJSON)
436438

437439
start := time.Now()
438440
var resp *http.Response
@@ -814,7 +816,7 @@ func (s *Server) tailscaleStatusHandler(w http.ResponseWriter, r *http.Request)
814816
}
815817

816818
status := s.getTailscaleInfo()
817-
w.Header().Set("Content-Type", "application/json")
819+
w.Header().Set(contentTypeHeader, applicationJSON)
818820
w.WriteHeader(http.StatusOK)
819821
if err := json.NewEncoder(w).Encode(status); err != nil {
820822
log.Printf("failed to encode tailscale status: %v", err)
@@ -927,7 +929,7 @@ func (s *Server) verifyMFACode(ctx context.Context, sessionID, code string) (boo
927929
if err != nil {
928930
return false, err
929931
}
930-
req.Header.Set("Content-Type", "application/json")
932+
req.Header.Set(contentTypeHeader, applicationJSON)
931933

932934
start := time.Now()
933935
var resp *http.Response

services/authz/server/server_test.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ import (
1414
)
1515

1616
const (
17-
testDeviceID = "test-device"
18-
testAllowPath = "/v1/data/keep/allow"
19-
testClientRemoteIP = "100.65.1.1:12345"
20-
resultKey = "result"
21-
decisionKey = "decision"
17+
testDeviceID = "test-device"
18+
testAllowPath = "/v1/data/keep/allow"
19+
testClientRemoteIP = "100.65.1.1:12345"
20+
resultKey = "result"
21+
decisionKey = "decision"
22+
testRejectsNonPOST = testRejectsNonPOST
23+
testRejectsInvalidJSON = testRejectsInvalidJSON
2224
)
2325

2426
// TestServer_healthHandler tests the health endpoint
@@ -52,7 +54,7 @@ func TestServer_healthHandler(t *testing.T) {
5254
func TestServer_verifyHandler(t *testing.T) {
5355
server := createTestServer(t)
5456

55-
t.Run("rejects non-POST methods", func(t *testing.T) {
57+
t.Run(testRejectsNonPOST, func(t *testing.T) {
5658
req := httptest.NewRequest(http.MethodGet, "/v1/auth/verify", nil)
5759
rr := httptest.NewRecorder()
5860

@@ -63,7 +65,7 @@ func TestServer_verifyHandler(t *testing.T) {
6365
}
6466
})
6567

66-
t.Run("rejects invalid JSON", func(t *testing.T) {
68+
t.Run(testRejectsInvalidJSON, func(t *testing.T) {
6769
req := httptest.NewRequest(http.MethodPost, "/v1/auth/verify", bytes.NewReader([]byte("invalid json")))
6870
rr := httptest.NewRecorder()
6971

@@ -187,7 +189,7 @@ func TestServer_envoyAuthHandler(t *testing.T) {
187189

188190
server := createTestServerWithMocks(t, mockOPA.URL, mockInventory.URL)
189191

190-
t.Run("rejects non-POST methods", func(t *testing.T) {
192+
t.Run(testRejectsNonPOST, func(t *testing.T) {
191193
req := httptest.NewRequest(http.MethodGet, "/v1/auth/check", nil)
192194
rr := httptest.NewRecorder()
193195

@@ -198,7 +200,7 @@ func TestServer_envoyAuthHandler(t *testing.T) {
198200
}
199201
})
200202

201-
t.Run("rejects invalid JSON", func(t *testing.T) {
203+
t.Run(testRejectsInvalidJSON, func(t *testing.T) {
202204
req := httptest.NewRequest(http.MethodPost, "/v1/auth/check", bytes.NewReader([]byte("invalid json")))
203205
rr := httptest.NewRecorder()
204206

@@ -513,7 +515,7 @@ func TestServer_lookupDevice(t *testing.T) {
513515
func TestServer_deviceCertHandler(t *testing.T) {
514516
server := createTestServer(t)
515517

516-
t.Run("rejects non-POST methods", func(t *testing.T) {
518+
t.Run(testRejectsNonPOST, func(t *testing.T) {
517519
req := httptest.NewRequest(http.MethodGet, "/v1/certs/device", nil)
518520
rr := httptest.NewRecorder()
519521

@@ -524,7 +526,7 @@ func TestServer_deviceCertHandler(t *testing.T) {
524526
}
525527
})
526528

527-
t.Run("rejects invalid JSON", func(t *testing.T) {
529+
t.Run(testRejectsInvalidJSON, func(t *testing.T) {
528530
req := httptest.NewRequest(http.MethodPost, "/v1/certs/device", bytes.NewReader([]byte("invalid json")))
529531
rr := httptest.NewRecorder()
530532

0 commit comments

Comments
 (0)