223 lines
6.0 KiB
Go
223 lines
6.0 KiB
Go
package v1
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gorilla/mux"
|
|
)
|
|
|
|
func TestDomainsRegister(t *testing.T) {
|
|
api, _ := setupTestAPI(t)
|
|
|
|
body, _ := json.Marshal(map[string]any{
|
|
"domain": "my-api.payne.io",
|
|
"source": "wild-works",
|
|
"backend": map[string]any{
|
|
"address": "192.168.8.60:9001",
|
|
"type": "http",
|
|
},
|
|
})
|
|
|
|
req := httptest.NewRequest("POST", "/api/v1/domains", bytes.NewReader(body))
|
|
w := httptest.NewRecorder()
|
|
api.DomainsRegister(w, req)
|
|
|
|
if w.Code != http.StatusCreated {
|
|
t.Fatalf("expected 201, got %d: %s", w.Code, w.Body.String())
|
|
}
|
|
|
|
var resp map[string]any
|
|
json.Unmarshal(w.Body.Bytes(), &resp)
|
|
|
|
dom := resp["domain"].(map[string]any)
|
|
if dom["domain"] != "my-api.payne.io" {
|
|
t.Errorf("expected domain my-api.payne.io, got %v", dom["domain"])
|
|
}
|
|
if dom["tls"] != "terminate" {
|
|
t.Errorf("expected tls=terminate default for http, got %v", dom["tls"])
|
|
}
|
|
if dom["source"] != "wild-works" {
|
|
t.Errorf("expected source wild-works, got %v", dom["source"])
|
|
}
|
|
}
|
|
|
|
func TestDomainsRegister_TCPPassthrough(t *testing.T) {
|
|
api, _ := setupTestAPI(t)
|
|
|
|
body, _ := json.Marshal(map[string]any{
|
|
"domain": "cloud.payne.io",
|
|
"source": "wild-cloud",
|
|
"backend": map[string]any{
|
|
"address": "192.168.8.240:443",
|
|
"type": "tcp-passthrough",
|
|
},
|
|
"subdomains": true,
|
|
"public": true,
|
|
})
|
|
|
|
req := httptest.NewRequest("POST", "/api/v1/domains", bytes.NewReader(body))
|
|
w := httptest.NewRecorder()
|
|
api.DomainsRegister(w, req)
|
|
|
|
if w.Code != http.StatusCreated {
|
|
t.Fatalf("expected 201, got %d: %s", w.Code, w.Body.String())
|
|
}
|
|
|
|
var resp map[string]any
|
|
json.Unmarshal(w.Body.Bytes(), &resp)
|
|
|
|
dom := resp["domain"].(map[string]any)
|
|
if dom["tls"] != "passthrough" {
|
|
t.Errorf("expected tls=passthrough for tcp-passthrough, got %v", dom["tls"])
|
|
}
|
|
if dom["subdomains"] != true {
|
|
t.Errorf("expected subdomains=true, got %v", dom["subdomains"])
|
|
}
|
|
}
|
|
|
|
func TestDomainsListAll_Empty(t *testing.T) {
|
|
api, _ := setupTestAPI(t)
|
|
|
|
req := httptest.NewRequest("GET", "/api/v1/domains", nil)
|
|
w := httptest.NewRecorder()
|
|
api.DomainsListAll(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("expected 200, got %d", w.Code)
|
|
}
|
|
|
|
var resp map[string]any
|
|
json.Unmarshal(w.Body.Bytes(), &resp)
|
|
doms := resp["domains"].([]any)
|
|
if len(doms) != 0 {
|
|
t.Errorf("expected empty, got %d", len(doms))
|
|
}
|
|
}
|
|
|
|
func TestDomainsGet(t *testing.T) {
|
|
api, _ := setupTestAPI(t)
|
|
|
|
// Register
|
|
body, _ := json.Marshal(map[string]any{
|
|
"domain": "get-test.example.com",
|
|
"source": "test",
|
|
"backend": map[string]any{"address": "127.0.0.1:9000", "type": "http"},
|
|
})
|
|
req := httptest.NewRequest("POST", "/api/v1/domains", bytes.NewReader(body))
|
|
w := httptest.NewRecorder()
|
|
api.DomainsRegister(w, req)
|
|
|
|
// Get by domain
|
|
req = httptest.NewRequest("GET", "/api/v1/domains/get-test.example.com", nil)
|
|
req = mux.SetURLVars(req, map[string]string{"domain": "get-test.example.com"})
|
|
w = httptest.NewRecorder()
|
|
api.DomainsGet(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())
|
|
}
|
|
|
|
var dom map[string]any
|
|
json.Unmarshal(w.Body.Bytes(), &dom)
|
|
if dom["domain"] != "get-test.example.com" {
|
|
t.Errorf("expected domain get-test.example.com, got %v", dom["domain"])
|
|
}
|
|
}
|
|
|
|
func TestDomainsGet_NotFound(t *testing.T) {
|
|
api, _ := setupTestAPI(t)
|
|
|
|
req := httptest.NewRequest("GET", "/api/v1/domains/nonexistent.com", nil)
|
|
req = mux.SetURLVars(req, map[string]string{"domain": "nonexistent.com"})
|
|
w := httptest.NewRecorder()
|
|
api.DomainsGet(w, req)
|
|
|
|
if w.Code != http.StatusNotFound {
|
|
t.Errorf("expected 404, got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestDomainsUpdate(t *testing.T) {
|
|
api, _ := setupTestAPI(t)
|
|
|
|
// Register
|
|
body, _ := json.Marshal(map[string]any{
|
|
"domain": "update-test.example.com",
|
|
"source": "test",
|
|
"backend": map[string]any{"address": "127.0.0.1:9000", "type": "http"},
|
|
})
|
|
req := httptest.NewRequest("POST", "/api/v1/domains", bytes.NewReader(body))
|
|
w := httptest.NewRecorder()
|
|
api.DomainsRegister(w, req)
|
|
|
|
// Update public to true
|
|
update, _ := json.Marshal(map[string]any{"public": true})
|
|
req = httptest.NewRequest("PATCH", "/api/v1/domains/update-test.example.com", bytes.NewReader(update))
|
|
req = mux.SetURLVars(req, map[string]string{"domain": "update-test.example.com"})
|
|
w = httptest.NewRecorder()
|
|
api.DomainsUpdate(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())
|
|
}
|
|
|
|
var resp map[string]any
|
|
json.Unmarshal(w.Body.Bytes(), &resp)
|
|
dom := resp["domain"].(map[string]any)
|
|
if dom["public"] != true {
|
|
t.Errorf("expected public=true, got %v", dom["public"])
|
|
}
|
|
}
|
|
|
|
func TestDomainsDeregister(t *testing.T) {
|
|
api, _ := setupTestAPI(t)
|
|
|
|
// Register
|
|
body, _ := json.Marshal(map[string]any{
|
|
"domain": "delete-me.example.com",
|
|
"source": "test",
|
|
"backend": map[string]any{"address": "127.0.0.1:9000", "type": "http"},
|
|
})
|
|
req := httptest.NewRequest("POST", "/api/v1/domains", bytes.NewReader(body))
|
|
w := httptest.NewRecorder()
|
|
api.DomainsRegister(w, req)
|
|
|
|
// Delete
|
|
req = httptest.NewRequest("DELETE", "/api/v1/domains/delete-me.example.com", nil)
|
|
req = mux.SetURLVars(req, map[string]string{"domain": "delete-me.example.com"})
|
|
w = httptest.NewRecorder()
|
|
api.DomainsDeregister(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("expected 200, got %d", w.Code)
|
|
}
|
|
|
|
// Verify gone
|
|
req = httptest.NewRequest("GET", "/api/v1/domains/delete-me.example.com", nil)
|
|
req = mux.SetURLVars(req, map[string]string{"domain": "delete-me.example.com"})
|
|
w = httptest.NewRecorder()
|
|
api.DomainsGet(w, req)
|
|
if w.Code != http.StatusNotFound {
|
|
t.Errorf("expected 404 after delete, got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestDomainsRegister_Validation(t *testing.T) {
|
|
api, _ := setupTestAPI(t)
|
|
|
|
// Missing domain
|
|
body, _ := json.Marshal(map[string]any{
|
|
"backend": map[string]any{"address": "127.0.0.1:80", "type": "http"},
|
|
})
|
|
req := httptest.NewRequest("POST", "/api/v1/domains", bytes.NewReader(body))
|
|
w := httptest.NewRecorder()
|
|
api.DomainsRegister(w, req)
|
|
if w.Code != http.StatusBadRequest {
|
|
t.Errorf("expected 400 for missing domain, got %d", w.Code)
|
|
}
|
|
}
|