mirror of
https://github.com/btcsuite/btcd.git
synced 2026-08-13 12:32:51 +02:00
rpcclient: add tests for DisableAuth header behavior
Add table-driven tests that verify: - Authorization header is omitted when DisableAuth is true - Authorization header is present when DisableAuth is false - Default (zero value) behavior includes Authorization header Suggested by @TechLateef in #2514.
This commit is contained in:
parent
5d22b395b8
commit
9b849c1738
1 changed files with 109 additions and 0 deletions
109
rpcclient/disableauth_test.go
Normal file
109
rpcclient/disableauth_test.go
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
package rpcclient
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// TestDisableAuth verifies that the DisableAuth field correctly controls
|
||||
// whether the Authorization header is sent on RPC requests.
|
||||
func TestDisableAuth(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
t.Run("DisableAuth true omits Authorization header", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var gotAuth string
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
gotAuth = r.Header.Get("Authorization")
|
||||
// Return a valid JSON-RPC response so the client doesn't retry.
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write([]byte(`{"result":null,"error":null,"id":1}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
addr := strings.TrimPrefix(srv.URL, "http://")
|
||||
client, err := New(&ConnConfig{
|
||||
Host: addr,
|
||||
HTTPPostMode: true,
|
||||
DisableAuth: true,
|
||||
DisableTLS: true,
|
||||
}, nil)
|
||||
require.NoError(t, err)
|
||||
defer client.Shutdown()
|
||||
|
||||
// The client is now connected; issue a simple request to trigger
|
||||
// handleSendPostMessage.
|
||||
_, err = client.RawRequest("getblockchaininfo", nil)
|
||||
// We don't care if the RPC itself errors — we only care about
|
||||
// the Authorization header.
|
||||
_ = err
|
||||
|
||||
require.Empty(t, gotAuth, "Authorization header should be empty when DisableAuth is true")
|
||||
})
|
||||
|
||||
t.Run("DisableAuth false includes Authorization header", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var gotAuth string
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
gotAuth = r.Header.Get("Authorization")
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write([]byte(`{"result":null,"error":null,"id":1}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
addr := strings.TrimPrefix(srv.URL, "http://")
|
||||
client, err := New(&ConnConfig{
|
||||
Host: addr,
|
||||
HTTPPostMode: true,
|
||||
DisableAuth: false,
|
||||
DisableTLS: true,
|
||||
User: "testuser",
|
||||
Pass: "testpass",
|
||||
}, nil)
|
||||
require.NoError(t, err)
|
||||
defer client.Shutdown()
|
||||
|
||||
_, err = client.RawRequest("getblockchaininfo", nil)
|
||||
_ = err
|
||||
|
||||
expected := "Basic " + base64.StdEncoding.EncodeToString([]byte("testuser:testpass"))
|
||||
require.Equal(t, expected, gotAuth, "Authorization header should be set when DisableAuth is false")
|
||||
})
|
||||
|
||||
t.Run("DisableAuth default (zero value) includes Authorization header", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var gotAuth string
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
gotAuth = r.Header.Get("Authorization")
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write([]byte(`{"result":null,"error":null,"id":1}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
addr := strings.TrimPrefix(srv.URL, "http://")
|
||||
client, err := New(&ConnConfig{
|
||||
Host: addr,
|
||||
HTTPPostMode: true,
|
||||
// DisableAuth left as default (false)
|
||||
DisableTLS: true,
|
||||
User: "myuser",
|
||||
Pass: "mypass",
|
||||
}, nil)
|
||||
require.NoError(t, err)
|
||||
defer client.Shutdown()
|
||||
|
||||
_, err = client.RawRequest("getblockchaininfo", nil)
|
||||
_ = err
|
||||
|
||||
expected := "Basic " + base64.StdEncoding.EncodeToString([]byte("myuser:mypass"))
|
||||
require.Equal(t, expected, gotAuth, "Authorization header should be set by default (DisableAuth is false)")
|
||||
})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue