internal/controld: add support for parsing client id from raw UID

This commit is contained in:
Cuong Manh Le
2023-07-26 18:23:15 +07:00
committed by Cuong Manh Le
parent 19bc44a7f3
commit 72d2f4e7e3
3 changed files with 67 additions and 19 deletions

View File

@@ -8,6 +8,7 @@ import (
"fmt"
"net"
"net/http"
"strings"
"time"
"github.com/Control-D-Inc/ctrld"
@@ -53,12 +54,18 @@ func (u UtilityErrorResponse) Error() string {
}
type utilityRequest struct {
UID string `json:"uid"`
UID string `json:"uid"`
ClientID string `json:"client_id,omitempty"`
}
// FetchResolverConfig fetch Control D config for given uid.
func FetchResolverConfig(uid, version string, cdDev bool) (*ResolverConfig, error) {
body, _ := json.Marshal(utilityRequest{UID: uid})
func FetchResolverConfig(rawUID, version string, cdDev bool) (*ResolverConfig, error) {
uid, clientID := ParseRawUID(rawUID)
uReq := utilityRequest{UID: uid}
if clientID != "" {
uReq.ClientID = clientID
}
body, _ := json.Marshal(uReq)
apiUrl := resolverDataURLCom
if cdDev {
apiUrl = resolverDataURLDev
@@ -120,3 +127,13 @@ func FetchResolverConfig(uid, version string, cdDev bool) (*ResolverConfig, erro
}
return &ur.Body.Resolver, nil
}
// ParseRawUID parse the input raw UID, returning real UID and ClientID.
// The raw UID can have 2 forms:
//
// - <uid>
// - <uid>/<client_id>
func ParseRawUID(rawUID string) (string, string) {
uid, clientID, _ := strings.Cut(rawUID, "/")
return uid, clientID
}

View File

@@ -1,34 +1,31 @@
//go:build controld
package controld
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestFetchResolverConfig(t *testing.T) {
func Test_parseUID(t *testing.T) {
tests := []struct {
name string
uid string
dev bool
wantErr bool
name string
uid string
wantUID string
wantClientID string
}{
{"valid com", "p2", false, false},
{"valid dev", "p2", true, false},
{"invalid uid", "abcd1234", false, true},
{"empty", "", "", ""},
{"only uid", "abcd1234", "abcd1234", ""},
{"with client id", "abcd1234/clientID", "abcd1234", "clientID"},
{"with empty clientID", "abcd1234/", "abcd1234", ""},
}
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
got, err := FetchResolverConfig(tc.uid, "dev-test", tc.dev)
require.False(t, (err != nil) != tc.wantErr, err)
if !tc.wantErr {
assert.NotEmpty(t, got.DOH)
}
gotUID, gotClientID := ParseRawUID(tc.uid)
assert.Equal(t, tc.wantUID, gotUID)
assert.Equal(t, tc.wantClientID, gotClientID)
})
}
}

View File

@@ -0,0 +1,34 @@
//go:build controld
package controld
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestFetchResolverConfig(t *testing.T) {
tests := []struct {
name string
uid string
dev bool
wantErr bool
}{
{"valid com", "p2", false, false},
{"valid dev", "p2", true, false},
{"invalid uid", "abcd1234", false, true},
}
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
got, err := FetchResolverConfig(tc.uid, "dev-test", tc.dev)
require.False(t, (err != nil) != tc.wantErr, err)
if !tc.wantErr {
assert.NotEmpty(t, got.DOH)
}
})
}
}