Files
gluetun/gluetun-servers/pkg/updaters/providers/fastestvpn/api_test.go
T
Quentin McGaw d9cc7dcffb refactor(storage): new storage file structure
- new directory structure containing manifest.json and one json file per provider, by default.
- the manifest.json file can specify a filepath for each vpn provider
- each vpn provider json data file can contain the `"preferred": true` field to enforce it is used even if outdated, unless there is a version mismatch
- `STORAGE_SERVERS_DIRECTORY_PATH` replaces `STORAGE_FILEPATH` (which is now a migration source only). It sets the directory where server manifest and per-provider JSON files are stored (default: `/gluetun/servers/`).
- First-run migration: On startup, gluetun checks for the old /gluetun/servers.json file; if found and no new manifest exists, it automatically migrates all data to /gluetun/servers/ directory structure
- Silent fallback: If legacy file isn't found, uses the new directory path normally
- Legacy cleanup: After successful migration, attempts to remove the old fat JSON file (logs warning only if removal fails, e.g., read-only bind mounts)
2026-05-11 04:53:04 +00:00

158 lines
4.1 KiB
Go

package fastestvpn
import (
"context"
"errors"
"io"
"net/http"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
type roundTripFunc func(r *http.Request) (*http.Response, error)
func (f roundTripFunc) RoundTrip(r *http.Request) (*http.Response, error) {
return f(r)
}
func Test_fechAPIServers(t *testing.T) {
t.Parallel()
testCases := map[string]struct {
ctx context.Context
protocol string
requestBody string
responseStatus int
responseBody io.ReadCloser
transportErr error
servers []apiServer
errMessage string
}{
"transport_error": {
ctx: context.Background(),
protocol: "tcp",
requestBody: "action=vpn_servers&protocol=tcp",
responseStatus: http.StatusOK,
transportErr: errors.New("test error"),
errMessage: `sending request: Post ` +
`"https://support.fastestvpn.com/wp-admin/admin-ajax.php": ` +
`test error`,
},
"not_found_status_code": {
ctx: context.Background(),
protocol: "tcp",
requestBody: "action=vpn_servers&protocol=tcp",
responseStatus: http.StatusNotFound,
errMessage: "HTTP status code not OK: 404",
},
"empty_data": {
ctx: context.Background(),
protocol: "tcp",
requestBody: "action=vpn_servers&protocol=tcp",
responseStatus: http.StatusOK,
responseBody: io.NopCloser(strings.NewReader("")),
servers: []apiServer{},
},
"single_server": {
ctx: context.Background(),
protocol: "tcp",
requestBody: "action=vpn_servers&protocol=tcp",
responseStatus: http.StatusOK,
responseBody: io.NopCloser(strings.NewReader(
"irrelevant<tr><td>Australia</td><td>Sydney</td>" +
"<td>au-stream.jumptoserver.com</td></tr>irrelevant")),
servers: []apiServer{
{country: "Australia", city: "Sydney", hostname: "au-stream.jumptoserver.com"},
},
},
"two_servers": {
ctx: context.Background(),
protocol: "tcp",
requestBody: "action=vpn_servers&protocol=tcp",
responseStatus: http.StatusOK,
responseBody: io.NopCloser(strings.NewReader(
"<tr><td>Australia</td><td>Sydney</td><td>au-stream.jumptoserver.com</td></tr>" +
"<tr><td>Australia</td><td>Sydney</td><td>au-01.jumptoserver.com</td></tr>")),
servers: []apiServer{
{country: "Australia", city: "Sydney", hostname: "au-stream.jumptoserver.com"},
{country: "Australia", city: "Sydney", hostname: "au-01.jumptoserver.com"},
},
},
}
for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()
client := &http.Client{
Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) {
assert.Equal(t, apiURL, r.URL.String())
requestBody, err := io.ReadAll(r.Body)
assert.NoError(t, err)
assert.Equal(t, testCase.requestBody, string(requestBody))
if testCase.transportErr != nil {
return nil, testCase.transportErr
}
return &http.Response{
StatusCode: testCase.responseStatus,
Body: testCase.responseBody,
}, nil
}),
}
entries, err := fetchAPIServers(testCase.ctx, client, testCase.protocol)
if testCase.errMessage != "" {
assert.EqualError(t, err, testCase.errMessage)
} else {
assert.NoError(t, err)
}
assert.Equal(t, testCase.servers, entries)
})
}
}
func Test_getNextBlock(t *testing.T) {
t.Parallel()
testCases := map[string]struct {
data string
startToken string
endToken string
nextBlock []byte
}{
"empty_data": {
startToken: "<a>",
endToken: "</a>",
},
"start_token_not_found": {
data: "test</a>",
startToken: "<a>",
endToken: "</a>",
},
"end_token_not_found": {
data: "<a>test",
startToken: "<a>",
endToken: "</a>",
},
"block_found": {
data: "xy<a>test</a><a>test2</a>zx",
startToken: "<a>",
endToken: "</a>",
nextBlock: []byte("<a>test</a>"),
},
}
for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()
nextBlock := getNextBlock([]byte(testCase.data), testCase.startToken, testCase.endToken)
assert.Equal(t, testCase.nextBlock, nextBlock)
})
}
}