json_dns_controller_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package sub
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "strings"
  6. "testing"
  7. "github.com/gin-gonic/gin"
  8. )
  9. // The panel setting must survive the controller wiring, not just the service
  10. // API: sub.go passes it as a controller option.
  11. func TestJsonEndpointServesPanelDnsServers(t *testing.T) {
  12. seedSubDB(t)
  13. seedSubInbound(t, "s1", "tcpin", 4910, 1, dnsTestStream)
  14. gin.SetMode(gin.TestMode)
  15. router := gin.New()
  16. NewSUBController(
  17. router.Group("/"),
  18. WithSUBJsonEnabled(true),
  19. WithSUBJsonAlwaysArray(true),
  20. WithSUBJsonDns(`["https://dns.google/dns-query", "tls://1.1.1.1"]`),
  21. )
  22. req := httptest.NewRequest(http.MethodGet, "http://sub.example.com/json/s1", nil)
  23. resp := httptest.NewRecorder()
  24. router.ServeHTTP(resp, req)
  25. if resp.Code != http.StatusOK {
  26. t.Fatalf("status = %d, want 200; body=%s", resp.Code, resp.Body.String())
  27. }
  28. docs := parseSubJsonDocs(t, resp.Body.String())
  29. if len(docs) != 1 {
  30. t.Fatalf("docs = %d, want 1", len(docs))
  31. }
  32. servers, _ := docDnsBlock(t, docs[0])["servers"].([]any)
  33. if len(servers) != 2 || servers[0] != "https://dns.google/dns-query" || servers[1] != "tls://1.1.1.1" {
  34. t.Fatalf("dns servers = %v", servers)
  35. }
  36. if _, hasTemplate := docDnsBlock(t, docs[0])["tag"]; hasTemplate {
  37. t.Fatalf("template dns keys leaked: %v", docDnsBlock(t, docs[0]))
  38. }
  39. if body := resp.Body.String(); strings.Contains(body, "8.8.8.8") {
  40. t.Fatalf("template resolver survived the override:\n%s", body)
  41. }
  42. }