Skip to content

Commit

Permalink
Explain errors
Browse files Browse the repository at this point in the history
  • Loading branch information
thrasher- committed Jan 24, 2024
1 parent c4e96d4 commit bc25b3e
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions engine/rpcserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4185,25 +4185,25 @@ func TestStartRPCRESTProxy(t *testing.T) {
certFile := filepath.Join(targetDir, "cert.pem")

caCert, err := os.ReadFile(certFile)
require.NoError(t, err)
require.NoError(t, err, "ReadFile should not error")
caCertPool := x509.NewCertPool()
ok := caCertPool.AppendCertsFromPEM(caCert)
require.True(t, ok)
require.True(t, ok, "AppendCertsFromPEM should return true")
client := &http.Client{Transport: &http.Transport{TLSClientConfig: &tls.Config{RootCAs: caCertPool, MinVersion: tls.VersionTLS12}}}

req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "https://localhost:"+strconv.Itoa(gRPCProxyPort)+"/v1/getinfo", http.NoBody)
require.NoError(t, err)
require.NoError(t, err, "NewRequestWithContext should not error")
req.SetBasicAuth("bobmarley", "Sup3rdup3rS3cr3t")
resp, err := client.Do(req)
require.NoError(t, err)
require.NoError(t, err, "client.Do should not error")
defer resp.Body.Close()

var info gctrpc.GetInfoResponse
err = json.NewDecoder(resp.Body).Decode(&info)
require.NoError(t, err)
require.NoError(t, err, "Decode should not error")

uptimeDuration, err := time.ParseDuration(info.Uptime)
require.NoError(t, err)
require.NoError(t, err, "ParseDuration should not error")
assert.InDelta(t, time.Since(fakeTime).Seconds(), uptimeDuration.Seconds(), 1.0, "uptime should be within 1 second of the expected duration")
}

Expand All @@ -4223,24 +4223,24 @@ func TestRPCProxyAuthClient(t *testing.T) {
dummyHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte("MEOW"))
require.NoError(t, err)
require.NoError(t, err, "Write should not error")
})

rr := httptest.NewRecorder()
handler := s.authClient(dummyHandler)
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "/", http.NoBody)
require.NoError(t, err)
require.NoError(t, err, "NewRequestWithContext should not error")

// Test with valid credentials
req.SetBasicAuth("bobmarley", "Sup3rdup3rS3cr3t")
handler.ServeHTTP(rr, req)
assert.Equal(t, http.StatusOK, rr.Code)
assert.Equal(t, "MEOW", rr.Body.String())
assert.Equal(t, http.StatusOK, rr.Code, "HTTP status code should be 200")
assert.Equal(t, "MEOW", rr.Body.String(), "Response body should be 'MEOW'")

// Test with wrong credentials
req.SetBasicAuth("wronguser", "wrongpass")
rr = httptest.NewRecorder()
handler.ServeHTTP(rr, req)
assert.Equal(t, http.StatusUnauthorized, rr.Code)
assert.Equal(t, "Access denied\n", rr.Body.String())
assert.Equal(t, http.StatusUnauthorized, rr.Code, "HTTP status code should be 401")
assert.Equal(t, "Access denied\n", rr.Body.String(), "Response body should be 'Access denied\n'")
}

0 comments on commit bc25b3e

Please sign in to comment.