websvc: add tests; imp names

This commit is contained in:
Ainar Garipov
2022-08-31 19:11:00 +03:00
parent dbdae5b4fc
commit b018e150e7
8 changed files with 58 additions and 36 deletions

View File

@@ -17,23 +17,26 @@ import (
// Main is the entry point of application.
func Main(clientBuildFS fs.FS) {
// # Initial Configuration
// Initial Configuration
start := time.Now()
rand.Seed(start.UnixNano())
// TODO(a.garipov): Set up logging.
// # Web Service
// Web Service
// TODO(a.garipov): Use in the Web service.
_ = clientBuildFS
// TODO(a.garipov): Make configurable.
web := websvc.New(&websvc.Config{
Addresses: []netip.AddrPort{netip.MustParseAddrPort("127.0.0.1:3001")},
Start: start,
Timeout: 60 * time.Second,
// TODO(a.garipov): Use an actual implementation.
ConfigManager: nil,
Addresses: []netip.AddrPort{netip.MustParseAddrPort("127.0.0.1:3001")},
Start: start,
Timeout: 60 * time.Second,
ForceHTTPS: false,
})
err := web.Start()

View File

@@ -25,8 +25,18 @@ type ReqPatchSettingsDNS struct {
UpstreamTimeout timeutil.Duration `json:"upstream_timeout"`
}
// handlePatchSettingsDNS is the handler for the PATCH /api/v1/settings/http
// HTTP API.
// httpAPIDNSSettings are the DNS settings as used by the HTTP API.
type httpAPIDNSSettings struct {
// TODO(a.garipov): Add more as we go.
Addresses []netip.AddrPort `json:"addresses"`
BootstrapServers []string `json:"bootstrap_servers"`
UpstreamServers []string `json:"upstream_servers"`
UpstreamTimeout timeutil.Duration `json:"upstream_timeout"`
}
// handlePatchSettingsDNS is the handler for the PATCH /api/v1/settings/dns HTTP
// API.
func (svc *Service) handlePatchSettingsDNS(w http.ResponseWriter, r *http.Request) {
req := &ReqPatchSettingsDNS{
Addresses: []netip.AddrPort{},
@@ -66,7 +76,7 @@ func (svc *Service) handlePatchSettingsDNS(w http.ResponseWriter, r *http.Reques
return
}
writeJSONResponse(w, r, &respGetV1SettingsAllDNS{
writeJSONResponse(w, r, &httpAPIDNSSettings{
Addresses: newConf.Addresses,
BootstrapServers: newConf.BootstrapServers,
UpstreamServers: newConf.UpstreamServers,

View File

@@ -26,6 +26,14 @@ type ReqPatchSettingsHTTP struct {
SecureAddresses []netip.AddrPort `json:"secure_addresses"`
}
// httpAPIDNSSettings are the HTTP settings as used by the HTTP API.
type httpAPIHTTPSettings struct {
// TODO(a.garipov): Add more as we go.
Addresses []netip.AddrPort `json:"addresses"`
SecureAddresses []netip.AddrPort `json:"secure_addresses"`
}
// handlePatchSettingsHTTP is the handler for the PATCH /api/v1/settings/http
// HTTP API.
func (svc *Service) handlePatchSettingsHTTP(w http.ResponseWriter, r *http.Request) {
@@ -52,7 +60,7 @@ func (svc *Service) handlePatchSettingsHTTP(w http.ResponseWriter, r *http.Reque
ForceHTTPS: svc.forceHTTPS,
}
writeJSONResponse(w, r, &respGetV1SettingsAllHTTP{
writeJSONResponse(w, r, &httpAPIHTTPSettings{
Addresses: newConf.Addresses,
SecureAddresses: newConf.SecureAddresses,
})

View File

@@ -2,7 +2,6 @@ package websvc
import (
"net/http"
"net/netip"
"github.com/AdguardTeam/golibs/timeutil"
)
@@ -16,28 +15,8 @@ import (
type RespGetV1SettingsAll struct {
// TODO(a.garipov): Add more as we go.
DNS *respGetV1SettingsAllDNS `json:"dns"`
HTTP *respGetV1SettingsAllHTTP `json:"http"`
}
// respGetV1SettingsAllDNS describes the DNS part of the response of the GET
// /api/v1/settings/all HTTP API.
type respGetV1SettingsAllDNS struct {
// TODO(a.garipov): Add more as we go.
Addresses []netip.AddrPort `json:"addresses"`
BootstrapServers []string `json:"bootstrap_servers"`
UpstreamServers []string `json:"upstream_servers"`
UpstreamTimeout timeutil.Duration `json:"upstream_timeout"`
}
// respGetV1SettingsAllHTTP describes the HTTP part of the response of the GET
// /api/v1/settings/all HTTP API.
type respGetV1SettingsAllHTTP struct {
// TODO(a.garipov): Add more as we go.
Addresses []netip.AddrPort `json:"addresses"`
SecureAddresses []netip.AddrPort `json:"secure_addresses"`
DNS *httpAPIDNSSettings `json:"dns"`
HTTP *httpAPIHTTPSettings `json:"http"`
}
// handleGetSettingsAll is the handler for the GET /api/v1/settings/all HTTP
@@ -49,13 +28,13 @@ func (svc *Service) handleGetSettingsAll(w http.ResponseWriter, r *http.Request)
httpConf := svc.Config()
writeJSONResponse(w, r, &RespGetV1SettingsAll{
DNS: &respGetV1SettingsAllDNS{
DNS: &httpAPIDNSSettings{
Addresses: dnsConf.Addresses,
BootstrapServers: dnsConf.BootstrapServers,
UpstreamServers: dnsConf.UpstreamServers,
UpstreamTimeout: timeutil.Duration{Duration: dnsConf.UpstreamTimeout},
},
HTTP: &respGetV1SettingsAllHTTP{
HTTP: &httpAPIHTTPSettings{
Addresses: httpConf.Addresses,
SecureAddresses: httpConf.SecureAddresses,
},

View File

@@ -1,12 +1,15 @@
package websvc
import (
"fmt"
"net"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/AdguardTeam/AdGuardHome/internal/aghtest"
"github.com/stretchr/testify/assert"
)
func TestWaitListener_Accept(t *testing.T) {
@@ -29,6 +32,17 @@ func TestWaitListener_Accept(t *testing.T) {
wg := &sync.WaitGroup{}
wg.Add(1)
done := make(chan struct{})
a := time.After(testTimeout)
go func() {
select {
case <-a:
panic(fmt.Errorf("did not finish after %s", testTimeout))
case <-done:
// Success.
}
}()
go func() {
var wrapper net.Listener = &waitListener{
Listener: l,
@@ -39,4 +53,7 @@ func TestWaitListener_Accept(t *testing.T) {
}()
wg.Wait()
close(done)
assert.Equal(t, uint32(1), atomic.LoadUint32(&numAcceptCalls))
}

View File

@@ -22,8 +22,6 @@ import (
)
// ConfigManager is the configuration manager interface.
//
// See internal/v1/svc/ for the main implementation.
type ConfigManager interface {
DNS() (svc *dnssvc.Service)
Web() (svc *Service)

View File

@@ -0,0 +1,6 @@
package websvc
import "time"
// testTimeout is the common timeout for tests.
const testTimeout = 1 * time.Second

View File

@@ -15,6 +15,7 @@ import (
"github.com/stretchr/testify/require"
)
// testTimeout is the common timeout for tests.
const testTimeout = 1 * time.Second
// testStart is the server start value for tests.