2022-10-04 16:02:55 +03:00
|
|
|
package websvc_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"net/netip"
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-10-10 14:05:24 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/next/agh"
|
2022-10-04 16:02:55 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/next/websvc"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestService_HandlePatchSettingsHTTP(t *testing.T) {
|
|
|
|
wantWeb := &websvc.HTTPAPIHTTPSettings{
|
|
|
|
Addresses: []netip.AddrPort{netip.MustParseAddrPort("127.0.1.1:80")},
|
|
|
|
SecureAddresses: []netip.AddrPort{netip.MustParseAddrPort("127.0.1.1:443")},
|
|
|
|
Timeout: websvc.JSONDuration(10 * time.Second),
|
|
|
|
ForceHTTPS: false,
|
|
|
|
}
|
|
|
|
|
2023-06-13 13:41:13 +03:00
|
|
|
svc, err := websvc.New(&websvc.Config{
|
|
|
|
TLS: &tls.Config{
|
|
|
|
Certificates: []tls.Certificate{{}},
|
|
|
|
},
|
|
|
|
Addresses: []netip.AddrPort{netip.MustParseAddrPort("127.0.0.1:80")},
|
|
|
|
SecureAddresses: []netip.AddrPort{netip.MustParseAddrPort("127.0.0.1:443")},
|
|
|
|
Timeout: 5 * time.Second,
|
|
|
|
ForceHTTPS: true,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-10-04 16:02:55 +03:00
|
|
|
confMgr := newConfigManager()
|
2023-06-13 13:41:13 +03:00
|
|
|
confMgr.onWeb = func() (s agh.ServiceWithConfig[*websvc.Config]) { return svc }
|
|
|
|
confMgr.onUpdateWeb = func(ctx context.Context, c *websvc.Config) (err error) { return nil }
|
2022-10-04 16:02:55 +03:00
|
|
|
|
|
|
|
_, addr := newTestServer(t, confMgr)
|
|
|
|
u := &url.URL{
|
|
|
|
Scheme: "http",
|
|
|
|
Host: addr.String(),
|
|
|
|
Path: websvc.PathV1SettingsHTTP,
|
|
|
|
}
|
|
|
|
|
|
|
|
req := jobj{
|
|
|
|
"addresses": wantWeb.Addresses,
|
|
|
|
"secure_addresses": wantWeb.SecureAddresses,
|
|
|
|
"timeout": wantWeb.Timeout,
|
|
|
|
"force_https": wantWeb.ForceHTTPS,
|
|
|
|
}
|
|
|
|
|
|
|
|
respBody := httpPatch(t, u, req, http.StatusOK)
|
|
|
|
resp := &websvc.HTTPAPIHTTPSettings{}
|
2023-06-13 13:41:13 +03:00
|
|
|
err = json.Unmarshal(respBody, resp)
|
2022-10-04 16:02:55 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, wantWeb, resp)
|
|
|
|
}
|