Skip to content

Commit

Permalink
chore(pkg/provider): JSON marshalable Provider
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed May 25, 2023
1 parent e650986 commit 771220d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
22 changes: 11 additions & 11 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ import "net/netip"
const defaultDoTPort uint16 = 853

type Provider struct {
Name string
DNS DNSServer
DoT DoTServer
DoH DoHServer
Name string `json:"name"`
DNS DNSServer `json:"dns"`
DoT DoTServer `json:"dot"`
DoH DoHServer `json:"doh"`
}

type DNSServer struct {
IPv4 []netip.Addr
IPv6 []netip.Addr
IPv4 []netip.Addr `json:"ipv4"`
IPv6 []netip.Addr `json:"ipv6"`
}

type DoTServer struct {
IPv4 []netip.Addr
IPv6 []netip.Addr
Name string // for TLS verification
Port uint16
IPv4 []netip.Addr `json:"ipv4"`
IPv6 []netip.Addr `json:"ipv6"`
Name string `json:"name"` // for TLS verification
Port uint16 `json:"port"`
}

type DoHServer struct {
URL string
URL string `json:"url"`
}
30 changes: 30 additions & 0 deletions pkg/provider/provider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package provider

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func Test_Provider_JSON(t *testing.T) {
t.Parallel()

provider := Google()

providerJSON, err := json.Marshal(provider)
require.NoError(t, err)

const expectedProviderJSON = `{"name":"Google",` +
`"dns":{"ipv4":["8.8.8.8","8.8.4.4"],"ipv6":["2001:4860:4860::8888","2001:4860:4860::8844"]},` +
`"dot":{"ipv4":["8.8.8.8","8.8.4.4"],"ipv6":["2001:4860:4860::8888","2001:4860:4860::8844"],` +
`"name":"dns.google","port":853},` +
`"doh":{"url":"https://dns.google/dns-query"}}`
assert.Equal(t, expectedProviderJSON, string(providerJSON))

var decodedProvider Provider
err = json.Unmarshal(providerJSON, &decodedProvider)
require.NoError(t, err)
assert.Equal(t, provider, decodedProvider)
}

0 comments on commit 771220d

Please sign in to comment.