first commit
Some checks failed
CI/CD Pipeline / Test (push) Failing after 22m19s
CI/CD Pipeline / Security Scan (push) Failing after 5m57s
CI/CD Pipeline / Build (amd64, darwin) (push) Has been skipped
CI/CD Pipeline / Build (amd64, linux) (push) Has been skipped
CI/CD Pipeline / Build (amd64, windows) (push) Has been skipped
CI/CD Pipeline / Build (arm64, darwin) (push) Has been skipped
CI/CD Pipeline / Build (arm64, linux) (push) Has been skipped
CI/CD Pipeline / Build Docker Image (push) Has been skipped
CI/CD Pipeline / Create Release (push) Has been skipped
Some checks failed
CI/CD Pipeline / Test (push) Failing after 22m19s
CI/CD Pipeline / Security Scan (push) Failing after 5m57s
CI/CD Pipeline / Build (amd64, darwin) (push) Has been skipped
CI/CD Pipeline / Build (amd64, linux) (push) Has been skipped
CI/CD Pipeline / Build (amd64, windows) (push) Has been skipped
CI/CD Pipeline / Build (arm64, darwin) (push) Has been skipped
CI/CD Pipeline / Build (arm64, linux) (push) Has been skipped
CI/CD Pipeline / Build Docker Image (push) Has been skipped
CI/CD Pipeline / Create Release (push) Has been skipped
This commit is contained in:
223
tests/e2e/app_smoke_test.go
Normal file
223
tests/e2e/app_smoke_test.go
Normal file
@@ -0,0 +1,223 @@
|
||||
package e2e
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/subconverter-go/internal/service"
|
||||
)
|
||||
|
||||
type roundTripFunc func(*http.Request) (*http.Response, error)
|
||||
|
||||
func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
return f(req)
|
||||
}
|
||||
|
||||
func newSubscriptionMockClient() *http.Client {
|
||||
var counter int32
|
||||
return &http.Client{
|
||||
Timeout: 5 * time.Second,
|
||||
Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) {
|
||||
atomic.AddInt32(&counter, 1)
|
||||
body := subscriptionFixture(req.URL.String())
|
||||
status := http.StatusOK
|
||||
if body == "" {
|
||||
status = http.StatusNotFound
|
||||
}
|
||||
resp := &http.Response{
|
||||
StatusCode: status,
|
||||
Header: make(http.Header),
|
||||
Body: io.NopCloser(strings.NewReader(body)),
|
||||
}
|
||||
resp.Header.Set("Content-Type", "text/plain; charset=utf-8")
|
||||
return resp, nil
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
func subscriptionFixture(u string) string {
|
||||
switch {
|
||||
case strings.Contains(u, "mock-subscribe.example.com"):
|
||||
return "ss://aes-256-cfb:password@192.168.1.1:8388\nvmess://eyJhZGQiOiIxOTIuMTY4LjEuMiIsICJwcyI6IlRlc3QgVk1lc3MiLCAicG9ydCI6NDQzLCAiaWQiOiI2YjZkZTQ3Zi1kZjQ1LTQ1M2ItODI1MS1hZjM0ZTM0ODc1Y2UifQ==\ntrojan://password@192.168.1.3:443"
|
||||
case strings.Contains(u, "example.com/upload"):
|
||||
return "uploaded"
|
||||
case strings.Contains(u, "ss-subscribe"):
|
||||
return "ss://aes-256-cfb:password@192.168.1.1:8388"
|
||||
case strings.Contains(u, "vmess-subscribe"):
|
||||
return "vmess://eyJhZGQiOiIxOTIuMTY4LjEuMiIsICJwcyI6IlRlc3QgVk1lc3MiLCAicG9ydCI6NDQzLCAiaWQiOiI2YjZkZTQ3Zi1kZjQ1LTQ1M2ItODI1MS1hZjM0ZTM0ODc1Y2UifQ=="
|
||||
case strings.Contains(u, "multi-source.example.com/first"):
|
||||
return "ss://aes-256-gcm:password@198.51.100.20:8443#multi-ss"
|
||||
case strings.Contains(u, "multi-source.example.com/second"):
|
||||
return "vmess://eyJhZGQiOiJtdWx0aS5leGFtcGxlLmNvbSIsICJhaWQiOiIwIiwgImhvc3QiOiJtdWx0aS5leGFtcGxlLmNvbSIsICJpZCI6IjEyMzQ1Njc4LTEyMzQtMTIzNC0xMjM0LTEyMzQ1Njc4OWFiYyIsICJuZXQiOiJ0Y3AiLCAicGF0aCI6Ii8iLCAicHMiOiJtdWx0aS12bWVzcyIsICJ0bHMiOiJ0bHMiLCAidHlwZSI6Im5vbmUiLCAidXJsX3Rlc3QiOiJodHRwOi8vd3d3Lmdvb2dsZS5jb20vZ2VuZXJhdGVfMjA0IiwgInYiOiIyIiwgInBvcnQiOiI0NDMiLCAic2N5IjoiYXV0byJ9"
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplicationBaselineRoutes(t *testing.T) {
|
||||
app, _, cleanup := setupTestApplication(t)
|
||||
t.Cleanup(cleanup)
|
||||
|
||||
server := app.GetHTTPServer()
|
||||
|
||||
t.Run("health endpoint", func(t *testing.T) {
|
||||
resp := doRequest(t, server, http.MethodGet, "/health")
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("expected status 200, got %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
var body map[string]interface{}
|
||||
if err := json.NewDecoder(resp.Body).Decode(&body); err != nil {
|
||||
t.Fatalf("failed to decode body: %v", err)
|
||||
}
|
||||
|
||||
status, ok := body["status"].(string)
|
||||
if !ok || status == "" {
|
||||
t.Fatalf("expected status field in response, got: %v", body)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("version endpoint", func(t *testing.T) {
|
||||
resp := doRequest(t, server, http.MethodGet, "/version")
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("expected status 200, got %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
var body map[string]interface{}
|
||||
if err := json.NewDecoder(resp.Body).Decode(&body); err != nil {
|
||||
t.Fatalf("failed to decode body: %v", err)
|
||||
}
|
||||
|
||||
serviceName, ok := body["service"].(string)
|
||||
if !ok || serviceName == "" {
|
||||
t.Fatalf("expected service field in response, got: %v", body)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("subscription endpoint", func(t *testing.T) {
|
||||
resp := doRequest(t, server, http.MethodGet, "/sub?target=clash&url=https://mock-subscribe.example.com")
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("expected status 200, got %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
contentType := resp.Header.Get("Content-Type")
|
||||
if contentType == "" {
|
||||
t.Fatalf("expected content type header, got empty")
|
||||
}
|
||||
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to read body: %v", err)
|
||||
}
|
||||
if len(data) == 0 {
|
||||
t.Fatalf("expected non-empty body from /sub endpoint")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func setupTestApplication(t *testing.T) (*service.Application, string, func()) {
|
||||
t.Helper()
|
||||
|
||||
defaultConfig := `server:
|
||||
host: "127.0.0.1"
|
||||
port: 26500
|
||||
logging:
|
||||
level: "error"
|
||||
format: "text"
|
||||
`
|
||||
|
||||
return setupTestApplicationWithConfig(t, defaultConfig)
|
||||
}
|
||||
|
||||
func setupTestApplicationWithConfig(t *testing.T, configYAML string) (*service.Application, string, func()) {
|
||||
t.Helper()
|
||||
|
||||
originalWD, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatalf("failed to get working directory: %v", err)
|
||||
}
|
||||
|
||||
projectRoot, err := findProjectRoot(originalWD)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to locate project root: %v", err)
|
||||
}
|
||||
|
||||
if err := os.Chdir(projectRoot); err != nil {
|
||||
t.Fatalf("failed to change directory to project root: %v", err)
|
||||
}
|
||||
|
||||
t.Cleanup(func() {
|
||||
_ = os.Chdir(originalWD)
|
||||
})
|
||||
|
||||
configDir := t.TempDir()
|
||||
configPath := filepath.Join(configDir, "config.yaml")
|
||||
|
||||
if err := os.WriteFile(configPath, []byte(configYAML), 0o644); err != nil {
|
||||
t.Fatalf("failed to write temp config: %v", err)
|
||||
}
|
||||
|
||||
app, err := service.NewApplication(configPath)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create application: %v", err)
|
||||
}
|
||||
|
||||
if engine := app.GetConversionEngine(); engine != nil {
|
||||
engine.SetHTTPClient(newSubscriptionMockClient())
|
||||
}
|
||||
|
||||
cleanup := func() {
|
||||
_ = app.Stop()
|
||||
_ = os.Remove(configPath)
|
||||
}
|
||||
|
||||
return app, configPath, cleanup
|
||||
}
|
||||
|
||||
func doRequest(t *testing.T, app *fiber.App, method, target string) *http.Response {
|
||||
t.Helper()
|
||||
return doRequestWithBody(t, app, method, target, nil, nil)
|
||||
}
|
||||
|
||||
func doRequestWithBody(t *testing.T, app *fiber.App, method, target string, body io.Reader, headers map[string]string) *http.Response {
|
||||
t.Helper()
|
||||
req := httptest.NewRequest(method, target, body)
|
||||
for k, v := range headers {
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
resp, err := app.Test(req, -1)
|
||||
if err != nil {
|
||||
t.Fatalf("fiber test request failed: %v", err)
|
||||
}
|
||||
return resp
|
||||
}
|
||||
|
||||
func findProjectRoot(start string) (string, error) {
|
||||
dir := start
|
||||
for {
|
||||
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
|
||||
return dir, nil
|
||||
}
|
||||
parent := filepath.Dir(dir)
|
||||
if parent == dir {
|
||||
return "", fmt.Errorf("go.mod not found from %s", start)
|
||||
}
|
||||
dir = parent
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user